Posts

Showing posts from July, 2011

[Level 3] Python performance.

Days ago, I discussed the coding style of python, and I use one example to present what I want you to know. The most 2 ways to use of renaming a file are: 1. Use rename method os moudle [ os.rename(f1, f2) ], 2. Use external command by popen method[ os.popen('mv f1 f2') ]. Some people might think there are not much different between these two ways. But after a stress test, you will realize it did has different between these two ways. The sample code and test run are as the following. #!/bin/env python import os, sys from time import gmtime, strftime n = 10000 f1 = '/1' f2 = '/2' os.popen('touch %s' % f1).read() print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) for i in range(n): if os.path.exists(f1): os.rename(f1, f2) else: os.rename(f2, f1) print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) for i in range(n): if os.path.exists(f1): os.popen('mv %s %s' % (f1, f2))

[Level 3] Python performance tips.

Python performance tips. http://wiki.python.org/moin/PythonSpeed http://wiki.python.org/moin/PythonSpeed/PerformanceTips Wish this helps. regards, Stanley Huang

[Level 2] How to get python version on run-time.

#!/bin/env python import sys print sys.version # python -c 'import sys; print sys.version' 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] Wish this helps. regards, Stanley Huang

[Level 2] How to allow using symbolic link in tomcat? (with python code)

If you want to allow using symbolic link in tomcat, you could add and attribute called "context" with an key "allowLinking" eg. <Context path="/myapp" allowLinking="true"/> and I wrote a sample python code to do this. #!/bin/env python import os,sys from xml.dom import minidom def getXMLDom(FILE): return minidom.parse(open(FILE,'r')) def saveXML(FILE, dom): fd = open(FILE, 'w') fd.write(dom.toxml()) fd.close() def appendElement(FILE, base_element_name, element_name, attr_key_val, element_index=0, backup_flag='y'): if backup_flag == 'y': backupConfigFile(FILE) dom = getXMLDom(FILE) e = dom.createElement(element_name) for attr, val in attr_key_val: e.setAttribute(attr, val) dom.getElementsByTagName(base_element_name)[element_index].appendChild(e) saveXML(FILE, dom) xmlFile = prefix+'/opt/ruckuswireless/3rdparty/tomcat/conf/server.xml' base_element_