Posts

Showing posts from April, 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 my

[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: return for child in tree[parent][:-1]: sys.stdout.write(indent + '|-') printTree(child, tree, cmd_list, indent + '| ') child = tree[parent][-1] sys.stdout.write(indent + '`-')

[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 passwd(passphrase='passphrase') 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 ar

[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

[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 ch