2009/02/16

Print in terminal with colors using python

This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts:

class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'

def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''


To use code like this, you can do something like

print bcolors.WARNING + "Warning: No active frommets remain. Continue?"
+ bcolors.ENDC

2008/12/09

Good MPI env for NAMD

export MP_SHARED_MEMORY=yes
export MP_EAGER_LIMIT=65536

2008/10/01

install Mysql with normal user

scripts/mysql_install_db --no-defaults

2008/08/25

Examples of Python threading and Queue

#!/usr/bin/env python
import threading,Queue,random,time,sys

class th(threading.Thread):

def __init__(self,threadName,queue):
threading.Thread.__init__(self,name=threadName)
self.name=threadName
self.Q=queue
print 'Name: %s started.'%(self.getName())

def run(self):
while 1:
num=self.Q.get(1)
if num==-1:
self.Q.task_done()
break
else:
print '%s sleep %f'%(self.name,num)
time.sleep(num)
print '%s wake after %f'%(self.name,num)
self.Q.task_done()
print '%s finish.'%self.name

thnum=2
Q=Queue.Queue(thnum*2)
for t1 in range(thnum):
th('thread%d'%t1,Q).start()

for t1 in range(10):
Q.put(random.random(),1)

for t1 in range(thnum):
Q.put(-1,1)
print 'wait...'
Q.join()


Do not forget to call Q.task_done() after each run of thread. Otherwise, Q.join() will be waiting for ever.

2008/08/07

Search hostname in knowhost file when hostname is hashed

ssh-keygen -F hostname [-f known_hosts_file]

2008/08/05

Extract files from rpm file

Check what is in rpm:


rpm -qlp filename.rpm

Extract


rpm2cpio filename.rpm |cpio -idmv

2008/08/04

Hosting Muliple Site Django under Nginx with WSGI

My site is very small and the hosting provider have strict limit on memory. I compile the Nginx like this:

./configure --prefix=$HOME/nginx_build --with-cpu-opt=pentium4 --add-module=./mod_wsgi --without-http_autoindex_module --without-http_auth_basic_module --without-http_charset_module --without-http_ssi_module --without-http_geo_module --without-http_map_module --without-http_proxy_module --without-http_memcached_module --without-http_rewrite_module --without-http_referer_module --without-http_upstream_ip_hash_module --without-http_browser_module --without-http_limit_zone_module --without-http_empty_gif_module --conf-path=$HOME/conf/nginx.conf

The most important thing that host Django by mod_WSGI is
add
wsgi_use_main_interpreter off;
wsgi_enable_subinterpreters on;
to your conf file. Mod_wsgi need to create distinct sub interpreters.