Tuesday, February 27, 2007

Autoupdate application modules

It's pretty easy to reload modules in Python. Using this feature you can update you whole application to a new version, without leaving or restarting your application.

The code below updates all loaded modules with newer version from any given path. To be included in the update, the module must contain the attribute autoupdate and a version attribute.

import sys
import os

def check(path):
""" Update all modules containing a autoupdate attribute
"""
for name,mod in sys.modules.items():
if hasattr(mod,'autoupdate'):
last_version=getattr(mod,'version')
print "Testing update of : %s v. %d" % (name,last_version)
org_file=os.path.split(mod.__file__)[1].replace('.pyc','.py')
new_module_file="%s\\%s.%d" % (path,org_file,last_version+1)
if os.path.exists(new_module_file):
print "Update found: %s" % new_module_file
update=file(new_module_file,'rb').read()
destination="%s\\%s" % (os.getcwd(), org_file)
print "Will replace: %s" % destination
#os.remove(destination)
#file(destination,'wb').write(update)
reload(mod)


This only works for modules that can work after a reload. Fx. SQLObject has a problem with that...

It would be an easy task to extend this to autoupdate from a FTP- or website.

Sunday, February 25, 2007

Socket performance

Just written a small script to test network/firewall performance in Python, and could not help myself from testing it on both XP and Linux...

OpenSUSE 10.2 performance:
1896.296296 mbit / second

Windows XP performance:
299.645137 mbit / second

(Tested on a dualboot machine.)

So, again the question must be, is this related to Python or the OS?

Well, iperf to localhost on Win XP yields:

C:\Documents and Settings\Martin\Desktop>iperf.exe -c localhost
------------------------------------------------------------
Client connecting to localhost, TCP port 5001
TCP window size: 8.00 KByte (default)
------------------------------------------------------------
[1912] local 127.0.0.1 port 1328 connected with 127.0.0.1 port 5001
[ ID] Interval Transfer Bandwidth
[1912] 0.0-10.0 sec 610 MBytes 511 Mbits/sec


iperf on Linux:

martin@msjdell:~> iperf -c localhost
------------------------------------------------------------
Client connecting to localhost, TCP port 5001
TCP window size: 49.4 KByte (default)
------------------------------------------------------------
[ 3] local 127.0.0.1 port 28150 connected with 127.0.0.1 port 5001
[ 3] 0.0-10.0 sec 3.55 GBytes 3.05 Gbits/sec

Monday, February 12, 2007

SQLObject

SQLObject is a really neat Object Relational Manager for Python. But imho not very well documented.

I have been developing a class which can handle the usual Create, Read, Update and Delete (CRUD) pattern. I'm using Kid for templates.

Here are some bites of the code:

# getting a tables columns
for col in table.sqlmeta.columnList:
print col.name
columnType=table.sqlmeta.columnDefinitions[col.name]
if col.foreignKey==None:
# normal column
else:
# relationship column

# getting remove and add method in join
rm=getattr(obj,'remove' + join.addRemoveName)
rm(record) # remove a record from relation