Posts

Showing posts from September, 2011

[Level 3] More decorator samples.

There is a good website to demo decorator sample codes. here ''' Usage: @deprecated def func(): ... ''' class deprecated: def __init__(self, func): self.func = func self.__name__ = self.func.__name__ self.__doc__ = self.func.__doc__ self.__dict__.update(self.func.__dict__) pass """This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.""" def __call__(self, *args, **kwargs): msg = "Call to deprecated function %s()." % self.func.__name__ ## Suppressing Warnings warnings.simplefilter("ignore") warnings.warn(msg, category=DeprecationWarning) ## Delete ignore filter del warnings.filters[0] (tmpDebug2Console, libcommon.bDebug2Console) = (libcommon.bDebug2Console, False) libcommon.debug(msg, logfile='de

[Level 1] Compile python scripts.

If you want to compile python script manually, you could use following command. $ python -c "import compileall; compileall.compile_dir('./', force=1)" Wish this helps. regards, Stanley Huang

[Level 2] enumrate() in Python.

#!/bin/env python aWeek = ['Sun', 'Mon','Tue','Wed', 'Thu', 'Fri', 'Sat'] ## without enumerate() print 'without enumerate()...' index = 0 for day in aWeek: print index, day index += 1 ## without enumerate() print 'without enumerate()...' for index in range(len(aWeek)): print index, aWeek[index] ## with enumerate() print 'with enumerate()...' for (index, day) in enumerate(aWeek): print index, day without enumerate()... 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat without enumerate()... 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat with enumerate()... 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat Use enumerate() would make code easier to read. Wish this helps. regards, Stanley Huang

[Level 2] Python Decorator Sample.

Another sample for Python decorator. #!/usr/bin/env python class ServiceFee: def __init__(self, f): self.f = f pass def __call__(self, *args, **kargs): return self.f(*args, **kargs)*1.1 class TaxFee: def __init__(self, f): self.f = f pass def __call__(self, *args, **kargs): return self.f(*args, **kargs)*1.05 @ServiceFee @TaxFee def beef(pricePerKG, KG): return pricePerKG*KG print 'Beef $10/KG, and buy 5KG=%s' % beef(10, 5) print 'Beef $20/KG, and buy 10KG=%s' % beef(20, 10) $ ./t.py Beef $10/KG, and buy 5KG=57.75 Beef $20/KG, and buy 10KG=231.0 Wish this helps. regards, Stanley Huang

[Level 2] Join lines by sed commands.

If you want to join lines in shell script, you could use sed command. #!/bin/bash cat > /tmp/t.txt <<EOF eth0 eth1 eth2 eth3 eth4 eth5 eth6 lo0 EOF sed -e :a -e '$!N; s/\n/,/; ta' /tmp/t.txt $ ./joinLinesBySed.sh eth0,eth1,eth2,eth3,eth4,eth5,eth6,lo0 Wish this helps. regards, Stanley Huang

[Level 2] How to implement decorator in Python (II).

Months ago, I wrote a simple decorator, without decorator parameters, implement in Python. [Level 2] How to implement decorator in Python (I). Now, I'll show you how to implement decorator with parameter. The sample of Python decorator (1, by class): #!/bin/env python class Decorator: def __init__(self, man, woman): self.man = man self.woman = woman pass def __call__(self, f): def wrapped_f(*args, **kargs): print 'hello %s' % self.man f(*args, **kargs) print 'world %s' % self.woman return wrapped_f @Decorator('stanley', 'christy') def test(who): print 'test(%s)' % who test('joseph') $ ./test.py hello stanley test(joseph) world christy The sample of Python decorator (2, by wrap function): #!/bin/env python def Decorator(man, woman): def wrap(f): def wrapped_f(*args, **kargs): print 'hello %s' % man

[Level 2] Testing combination of loop and try.

Sample code and test result are as the following: #!/bin/env python class e(Exception): def __init__(self): pass server_list = [1,2,3,4,5] for server in server_list: try: sReturn = server print 's:%s' % sReturn if sReturn<3: continue if sReturn==3: raise e print 'try:%s' % sReturn except Exception, e: print 'except1:%s' % sReturn continue print 'except2:%s' % sReturn else: print 'else:%s' % sReturn break finally: print 'finally:%s' % sReturn $ ./t.py s:1 finally:1 s:2 finally:2 s:3 except1:3 finally:3 s:4 try:4 else:4 finally:4 Wish this helps. regards, Stanley Huang