Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

13.9.11

The switch to PyPy

After many frustrating nights with python i have decided to make the jump to PyPy, any new project that i start will most likely run on PyPy. As you may know PyPy is compatible entirely with python.

16.6.10

A simplified deferred method

This is much like the twisted deferred, but is simplified down for use without twisted.


class Deferred(object):
def __init__(self):
self._callbacks = []

def add_callback(self, callback):
if hasattr(self, 'result'):
callback(self.result)
else:
self._callbacks.append(callback)

def callback(self, result):
assert not hasattr(self, 'result')
for cb in self._callbacks:
cb(result)
self.result = result


def defer(result):
d = Deferred()
d.result = result
return d



15.6.10

hestia


The basic core of hestia is done! The status icon is currently working and as soon as i figure out how i want to plan the rest out i will start on that bit. See the check mark icon, in the status icon tray in this screenshot.

Currently the project runs a set of commands that you have chosen, and gets the return status and sets the icon accordingly. If the build fails it sets and X and if not you get a pretty green check mark. Also if it fails, you get a notification from notify or notify-osd

26.12.09

fluid dynamics update.

The fluid dynamics is working now! albeit at a slow frame rate but it is actually working.

12.12.09

Python fluid dynamics library.

I'm currently working on a fluid dynamics library for my graphics library but have a very broad implementation up here. This is currently a work in progress. so it may or may not work. It requires fftw. I plan to use it to show the flow between nodes on a graph.

Original paper [JGT01]

Code here.

11.12.09

Python drawing library.

I've been working on a python drawing library for about a month and a half now, and i'm ready to share some details about it. Originally i was just going to take nodebox and re implement the backend to support aggdraw and PIL. But i ran acrossed design problems and supporting their API would make the library feel more like a standalone program than a real library. This is due to the fact that nodebox is really a standalone application and i wanted something that i could easily call from other programs. So far the requirements for the library are aggdraw, PIL and numpy. I have worked up several patches for aggdraw so all of the current problems with aggdraw should be worked out.

3.12.09

graph drawing library.

I have written a graph library almost entirely in Python, with some necessary code in C. First generation path drawing, Below are images in order of progression with the bottom being the better path/node placement.



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


14.8.09

RSA module for Python

I'm currently developing a Python module in C for the RSA public key algorithm. The key generation is done and the encryption and decryption are mostly done. The module relies on GMP and is its only require, other than Python of course.

I will have more updates and a link to the code as soon as im done.
for now read here and here.

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)