Posts

[Level 2] Singleton in Python.

#!/usr/bin/env python class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instance print me print you print me == you print id(me) == id(you) $ ./testSingleton.py True True $ You could also use metaclass to implement it. #!/usr/bin/env python class Singleton(type): def __init__(self, *args, **kwargs): super(Singleton, self).__init__(*args, **kwargs) self.__instance = None def __call__(self, *args, **kwargs): if self.__instance is None: self.__instance = super(Singleton, self).__call__(*args, **kwargs) return self.__instance class s(object): __metaclass__ = Singleton def __init__(self, size): self.__size = size def getSize(self): return self.__size class n(object): def __init__(self, size): self.__size = size def ...

[Level 1] How to evaluate the string to boolean?

If you want to evaluate the string you could create a function like below. #!/bin/env python def toBoolean(str): return if str in ['True', '1'] print toBoolean('True') print toBoolean('False') But you could have an easier way to implement with native module called "ast.literal_eval()" e.g. >>> import ast >>> help(ast.literal_eval) Help on function literal_eval in module ast: literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. >>> ast.literal_eval('False') False >>> ast.literal_eval('True') True >>> ast.literal_eval('1') 1 >>> ast.literal_eval('0') 0 Wish this helps. regards, Stanley Huang

[Level 2] Juniper vpn for ubuntu

If you need to setup the vpn on Ubuntu, you could refer to the following 2 links: http://mad-scientist.us/juniper.html http://holyarmy.org/2009/06/vpn-on-ubuntu-linux-with-juniper-network-connect/ Wish this helps. regards, Stanley Huang

[Level 2] Package management in Python. (pip)

pip is a tool for installing and managing Python packages, you could find more info here Wish this helps. regards, Stanley Huang

[Level 2] MySQL security check with tcpdump.

you can check if MySQL data streams are encrypted or not by the following command: # tcpdump -l -i eth0 -w - src or dst port 3306 | strings Wish this helps. regards, Stanley Huang

[Level 3] Pythonbrew, the Python environment management tool.

The more info, please click here The Chinese resource from OpenFoundry, click here . Wish this helps. regards, Stanley Huang

[Level 3] Slides from Devoxx talk on Language / Library / VM Co-Evolution.

Slides from Devoxx talk on Language / Library / VM Co-Evolution, more info, please click here . Wish this helps. regards, Stanley Huang