Tuesday, April 8, 2014

[ Level 2 ] Test Singleton Implementation in Python.

Days ago, just search how to use implement "Singleton" design pattern. (http://stackoverflow.com/questions/6760685/crea(ing-a-singleton-in-python) And today, I just need to implement for it. In my case, I need to create mutliple loggers and I also want to use "Singleton" to reduce system resource usage. Therefore, I create a Singleton meta class and also use a parameter called "singleton_id" to define the instance category. Source Code:
#!/bin/env python
class Singleton(object):
    _singleton_key = 'singleton_id'
    _instance = {}
    def __new__(class_, *args, **kwargs):
        if class_._singleton_key not in kwargs.keys():
            kwargs[class_._singleton_key] = ''
        if class_._singleton_key in kwargs and kwargs[class_._singleton_key] not in class_._instance.keys():
            class_._instance[kwargs[class_._singleton_key]] = object.__new__(class_)
        return class_._instance[kwargs[class_._singleton_key]]

class mySingleton(Singleton):
    def __init__(self, name, singleton_id=None):
        self.name = name

a0 = mySingleton('a')
print a0
print a0.name

a1 = mySingleton('a', singleton_id='a')
print a1
print a1.name

a2 = mySingleton('a', singleton_id='a')
print a2
print a2.name

a3 = mySingleton('b', singleton_id='a')
print a3
print a3.name

a4 = mySingleton('b', singleton_id='a')
print a4
print a4.name

print '----'
print a0
print a0.name
print a1
print a1.name
print a2
print a2.name
<__main__.mySingleton object at 0x7f2248065f10>
<__main__.mySingleton object at 0x7f2248065f50>
<__main__.mySingleton object at 0x7f2248065f50>
<__main__.mySingleton object at 0x7f2248065f50>
<__main__.mySingleton object at 0x7f2248065f50>
<__main__.mySingleton object at 0x7f2248065f10>
<__main__.mySingleton object at 0x7f2248065f50>
<__main__.mySingleton object at 0x7f2248065f50>

Wish this helps.

Stanley Huang

Monday, April 7, 2014

[Level 2] pstree implementation with Python.

Because I couldn't find pstree command in our company's product. Therefore, I just search from internet and found someone implement pstree layout with Python. (http://stackoverflow.com/questions/16395207/create-a-process-tree-like-pstree-command-with-python-in-linux) And I just modify it and create a pstree.py system utility.
#!/bin/env python
## Implement pstree in Python

## data structure of tree, cmd_list
tree = {
  0: [1],
  1: [2, 3],
  2: [5, 6, 7, 8],

cmd_list = {
  0: '/sbin/init',
  1: '[kthreadd]',

import os
import sys
import re
tree = {}
cmd_list = {}
def printTree(parent, tree, cmd_list, indent=''):
  print '%s:%s' % (parent, cmd_list[parent])
  if parent not in tree:
  for child in tree[parent][:-1]:
    sys.stdout.write(indent + '|-')
    printTree(child, tree, cmd_list, indent + '| ')
  child = tree[parent][-1]
  sys.stdout.write(indent + '`-')
  printTree(child, tree, cmd_list, indent + '  ')

## __main__
for l in [ re.sub('[ ]+', ' ', l.lstrip().rstrip()) for l in os.popen('ps -e -o pid,ppid,command --no-heading').readlines() ]:
    pid = int(l.split(' ')[0])
    ppid = int(l.split(' ')[1])
    cmd = ' '.join(l.split(' ')[2:])
    if ppid not in tree.keys():
        tree[ppid] = []
    cmd_list[pid] = cmd

# print tree
# print cmd_list
pid = 1
if len(sys.argv) > 1:
  pid = int(sys.argv[1])
printTree(pid, tree, cmd_list)

Wish this helps.

Stanley Huang

Wednesday, April 2, 2014

[Level 1] Create secure web for iPython notebook.

The default protocol for iPython notebook is http and you didn't passphrase to enter notebook. If you want your notebook be secure, you could follow the steps to enable SSL and passphrase for it. 1. create profile:
In [1]: ## create profile for secure web
        !ipython profile create secureweb
2. create passphrase:
In [2]: ## create passphrase
        from IPython.lib import passwd
Out[2]: 'sha1:24be7c5ab59a:b8b7d3c691b2db67a5ef855b625cb560e125e5e1' 3. Create SSL certificate
$ cd /home/stanley/iPython_notebook/certs
$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
Generating a 1024 bit RSA private key
writing new private key to 'mycert.pem'
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:TW
State or Province Name (full name) [Some-State]:Taiwan
Locality Name (eg, city) []:Taipei
Organization Name (eg, company) [Internet Widgits Pty Ltd]:StanleyHuang
Organizational Unit Name (eg, section) []:Stanley
Common Name (e.g. server FQDN or YOUR name) []:Stanley
Email Address []:wenlien1001@gmail.com
4. Modify iPython notebook config.
$ cat ~/.config/ipython/profile_secureweb/ipython_notebook_config.py
c = get_config()
c.IPKernelApp.pylab = 'inline'
c.NotebookApp.certfile = u'/home/stanley/iPython_notebook/certs/mycert.pem'
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:24be7c5ab59a:b8b7d3c691b2db67a5ef855b625cb560e125e5e1'
c.NotebookApp.port = 9999
5. Restart iPython notebook, login with https protocol and enter the passphrase.
$ ipython notebook --profile=secureweb

Wish this helps.

Stanley Huang

[Level 1] Install slideshow support in iPython notebook.

Just found an iPython notebook extension support slideshow and you could install by the following steps. Precondition: Because this introduction would try to clone a github project, you have install git utility first.
$ sudo apt-get -y install git
1. Use the following commands to install slideshow support.
## get porfile directory
profile_dir = get_ipython().profile_dir.location

## clone extension from github
import os
tgt = os.path.join( profile_dir, 'static', 'custom')
!git clone https://github.com/ipython-contrib/IPython-notebook-extensions.git $tgt
%cd $tgt

## create a javascript for supporting slideshow
%%writefile custom.js
// we want strict javascript that fails
// on ambiguous syntax
"using strict";

// do not use notebook loaded  event as it is re-triggerd on
// revert to checkpoint but this allow extesnsion to be loaded
// late enough to work.

$([IPython.events]).on('app_initialized.NotebookApp', function(){

    /**  Use path to js file relative to /static/ dir without leading slash, or
     *  js extension.
     *  Link directly to file is js extension.
     *  first argument of require is a **list** that can contains several modules if needed.

    // require(['custom/noscroll']);
    // require(['custom/clean_start'])
    // require(['custom/toggle_all_line_number'])
    // require(['custom/gist_it']);

     *  Link to entrypoint if extesnsion is a folder.
     *  to be consistent with commonjs module, the entrypoint is main.js
     *  here youcan also trigger a custom function on load that will do extra
     *  action with the module if needed
    //     // do stuff

2. restart iPython Notebook and reload page to get slideshow button. (Just on the right of cell type)
Wish this helps.

Stanley Huang

Tuesday, April 1, 2014

[Level 1] How to auto restore when start iPython

iPython have a magic command call "alias". This command just command "alias" in unix shell could help you to create alias command in iPython environment. How could we save the alias that we created before, you could use "store" magic command. The alias would be saved in iPython internal db. ex.
In [1]: %alias ipython_alias echo 'hello world'
In [2]: %store ipython_alias
After you exit iPython and restart it again, you need to restore the aliases from internal db.
In [1]: %store -r
And how could we make iPython auto-restore when we launch it? You could modify ipython_config.py in profile. ex. If you use default profile
$> cat /home/stanley/.config/ipython/profile_default/ipython_config.py 
# StoreMagics configuration
c.StoreMagics.autorestore = True # uncomment this line and assign autorestore as 'True'
You could also create a script to add alias automatically.
## Add often use commands.
security_commands = 'chmod chown scp ssh sudo'
network_commands = 'ifcofig netstat ping route'
system_commands = 'iostat ps top vmstat'
common_commands = 'cat grep head nohup tail'
editor_commands = 'meld soffice vi'
sourcecode_commands = 'apt-cache apt-get easy_install git p4 pip'
## prepare command list
command_list = ' '.join([security_commands, network_commands, system_commands, common_commands, editor_commands, sourcecode_commands])
for cmd in command_list.split(' '):
    %alias $cmd $cmd
    %store $cmd

Wish this helps.

Stanley Huang