Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

17.8.09

A Python dispatcher pattern


import sys
## the format of the dictionary is as follows:
## 'command': {'sub command' :
## {'function':'module.class.function'}
## }
dispatchDictionary = {
'add' : {'friend' :
{'function':'core.friends.friends.addFriend', 'help':'blah'}},
'connection' : {'info':{'function':'core.class.function','help': 'prints help'},
'help' : {'function' : 'core.shell.help.print_help'}},
'list' : {'files' : {'function' : 'core.shell.file.f_list'}},
'get' : {'file' : {'function' : 'core.shell.file.f_get'}}
}

class dispatch:
def __init__(self):
pass

def dispatch(self, input):
## the values 'in' _should_ be a list ['hi', 'hi']
try:
value = reduce(dict.get, input[:-1], dispatchDictionary)
except (TypeError), e:
## empty
print e
return

key = input[-1:][0]
try:
## make sure it really exists
value[key]
except KeyError:
key = input[:-1][0]


if key not in value:
if 'function' not in value:
## no function key
return
if value['function'] is None:
## empty
return
self.execute(value['function'], input)

elif key in value:
if 'function' not in value[key]:
## no function key
print value[input[-1:]]
return
if value[key]['function'] is None:
## empty
return
self.execute(value[key]['function'], input)



def execute(self, command, input):
if not input[-1:][0][:1] == '[' and not input[-1:][0][-1:] == ']':
formatted = False
else:
formatted = True

cSplit = command.split('.')
try:
__import__(cSplit[:1][0])
except ImportError, e:
print e
try:
module = sys.modules[cSplit[:1][0]]
except Exception, e:
print e
return

if formatted:
splittable = input[-1:][0][1:-1]
funcInput = splittable.split('/')
else:
funcInput = input[1:]

## create an instance
Class = self.r_getattr(module, '.'.join(cSplit[1:-1]))
c = Class()
func = self.r_getattr(c, '.'.join(cSplit[-1:]))
func(funcInput)

def r_getattr(self, object, attr):
return reduce(getattr, attr.split('.'), object)

def r_setattr(self, object, attr, value):
attrs = attr.split('.')
return setattr(reduce(getattr, attrs[:-1], object), attrs[-1], value)

def add(self):
pass


RSA update

I've now placed the code on github. In its current state it compiles with no errors but does not work completely correctly. I think i missed something somewhere, i will rectify the problem and post when its 100% done.

22.7.09

Cython an introduction.

One of the most useful things that i've come acrossed in Python is Cython a C extension for python, this tool was derived from Pyrex which is developed by Greg Ewing.

cdef extern from "xine_internal.h"
ctypedef extern struct xine_t
The above is an example from cython, it shares the same general syntax as python but has added many keywords that are specific to it. cdef is the keyword that is pretty much the same as def but it is declaring that the function is a C function.

In the example above cdef extern from "...", it is telling the interpreter to include the specified file as an extern.

The next thing that is going on is ctypedef extern struct xine_t, this is declaring the C struct xine_t as an extern.

As soon as you have declared all the necessary datatypes in Cython, you can create a class and call those external functions from that class.

# forward declaration of the external function
cdef extern const_char_ptr xine_get_version_string()


cdef class libxine:
.....
def xineGetVersionString(self):
return xine_get_version_string()


You can combine datatypes into the Cython code and use it like the code below. The code is a cast which is the same a C cast. You can also set class objects that are structs or external objects or types and them pass those through functions.

def xineGetMetaInfo(self,int Info):
MetaTable = {
0: "Title",
1: "Comment",
2: "Artist",
3: "Genre",
4: "Album",
5: "Year"
}

cdef char *GetMeta
GetMeta = <char *>xine_get_meta_info(self.xineStream, Info)
if GetMeta == NULL:
raise Exception('MetaFail')
return MetaTable[Info], GetMeta

Original source (pyxine-ng)