Thursday, April 30, 2009

Queue class for Zeo

Supper cool in that i uses a decorator for the transaction commit
First time I actually used a decorator. I knew about them, but I never thought of a use for them. Ok I am slow :)


from persistent import Persistent
from BTrees.LOBTree import LOBTree
from ZODB.POSException import ConflictError
import Queue
import transaction
import time

def transactionDecorator(fn):
def newfn(*args,**kwargs) :
while 1 :
try :
transaction.manager.begin()
result = fn(*args,**kwargs)
transaction.manager.commit()
return result
except ConflictError:
time.sleep(0.01)
except Exception,e:
transaction.manager.abort()
raise e
return newfn

class BTreeQueue(Persistent) :
def __init__(self):
self.bTree = LOBTree()
@transactionDecorator
def push(self,item):
key = None
try :
key = self.bTree.maxKey()
except ValueError :
key = 0
self.bTree.insert(key+1,item)

@transactionDecorator
def extend(self,items) :
start = None
try :
start = self.bTree.maxKey()
except ValueError :
start = 0
start += 1
result = {}
for i,key in enumerate(range(start,start+len(items))) :
result[key] = items[i]
self.bTree.update(result)

@transactionDecorator
def pop(self):
item = None
error = False
try :
key =self.bTree.minKey()
item = self.bTree.pop(key)
except ValueError :
error = True
finally :
if error :
raise Queue.Empty()
else :
return item

@transactionDecorator
def rotate(self):
"""pops a item then puts it on end again, returns value"""
item = None
error = False
key =self.bTree.minKey()
item = self.bTree.pop(key)
self.bTree.insert(self.bTree.maxKey()+1,item)
return item

@transactionDecorator
def count(self):
count = None
try :
count = self.bTree.maxKey() - self.bTree.minKey() + 1
except ValueError :
count = 0
return count

@transactionDecorator
def empty(self):
self.bTree.clear()

Wednesday, April 15, 2009

Notes about Cython classes

Just my own personal Notes for my own reference

In cython the are 2 different types of classes

Normal python classes as in the form

class A(object):
pass

Cython classes
cdef class A :
cdef int someterm

cython classes do not have a __dict__ to store their attributes in.
that is why they are so fast
but the concequence is that you can not add attributes at runtime like you can with normal python classes.
If you peak into the source code generated by cython you see they are actually structs.

Wednesday, April 8, 2009

Running xserver on vps/dedicated server

Use Xvfb on ubuntu/debian it is in package xvfb

used in this manner

Xvfb :1 &
env DISPLAY=:1 firefox
DISPLAY=:1 firefox
ssh -t -L 5900:localhost:5900 far-away.east 'x11vnc -localhost -display :0'
xtightvncviewer -encodings "copyrect tight hextile" localhost:0
ssh -t -L 5900:localhost:5900 server 'x11vnc -localhost -display :1'

Also simpler is you use
xvfb-run python testjs.py

Notes on setting up an ubuntu box

I keep having to do this all the time.
And all the time I need to look on the net for stuff I remember I need to do but can not remember exact command line

apt-get install g++ build-essential

apt-cache search XXXX

Setting of multiple IP addresses on one network card.

sudo ifconfig eth0:0 192.168.1.11 up
what is commonly know as ethernet alias

Setting up ssh to be used with secure keys so password is not needed.
http://polishlinux.org/apps/ssh-tricks/

Tuesday, April 7, 2009

Metaclass Articles

Some resources on Metaclasses

http://cleverdevil.org/computing/78/

http://www.voidspace.org.uk/python/articles/metaclasses.shtml

http://www.devx.com/opensource/Article/31482/1954?pf=true

Will make post on how to create class at runtime

Monday, April 6, 2009

How to simulate events in Javascript

Was searching on google on how to simulate a click using javascript on an html link object. Basically I found stuff that either did not work, or else the common answer was that it is not possible.

Anyway with a little of investigative searching I found the dispatchEvent method. The example ofcourse works in firefox. It also works in the version of webkit that ships with QT. It does not work with konqueror, and it does not work with IE.

It appears according to this website that a slightly different technique has to be used for IE.


Anyway might be useful for future reference.

Saturday, April 4, 2009

Mixins and Python

This is really cool. A way to expand python classes/objects without direct inheritance. Useful also. http://www.linuxjournal.com/node/4540/print .