poniedziałek, 8 października 2012
python graph implementation
And now as I have promised in my previous post.
it needs some more work, i.e. dumping to Graphviz format, but apart of that it is working quite well.
poniedziałek, 1 października 2012
metaclass with dynamic creation of properties
Meteclass with dynamic properties creation:
Written as helper class for my graph implementation (metaclass for nodes, edges and graph itself).
środa, 26 września 2012
yet another Observer pattern in python
Observer sniffing a particular attribute from Observable class. This little spy is notified every time watched attribute is changing its value.
Observers are hold in weakref.WeakValueDictionary to avoid crashes in case they would disappear (i.e. being collected and deleted by gc). And here is the source code:
środa, 8 sierpnia 2012
python traced class
With metaclass programming in python you can easily created a class tracing all updates to its attributes:
All objects using Traced metaclass are able to tell you which attributes have been changed. To get list of updated attributes (or keys when using TracedDict or indexes in case of TracedList) just call updated member function.
There is only one shortage: attributes defined and set inside __init__ (real instance attributes like instanceAttribute in TracedTest class) will be put into updated list when calling constructor. To avoid this behavior and trace changed during lifetime of object you need at call at ther very last line in constructor self.updated( reset = True ) (or uncomment last line in above example).
sobota, 14 lipca 2012
wtorek, 24 stycznia 2012
python multiprocessing handy template with callbacks
Here is a handy implementation of process pool I wrote a few days ago. All workers are created in pool and are running in daemonic mode, reading and executing ProcessTasks, which is made up using any python callable together with callback and exception callback definition.
The process pool is using two queues: first for tasks to be processed and second holding task results for callbacks. It could be executed in daemon mode too, processing results in a separate thread. Once you decide the job is done, you need to call ProcessPool::finalize (or ProcessPool::processAllResults), which is sending dummy assassins (in fact just a True value) to the workers (so they could return from their run method), terminating them once they are not alive any more and closing tasks and results queues. Blah, blah, blah... Code that was published below was kinda buggy. Error-free version can be found here.
The process pool is using two queues: first for tasks to be processed and second holding task results for callbacks. It could be executed in daemon mode too, processing results in a separate thread. Once you decide the job is done, you need to call ProcessPool::finalize (or ProcessPool::processAllResults), which is sending dummy assassins (in fact just a True value) to the workers (so they could return from their run method), terminating them once they are not alive any more and closing tasks and results queues. Blah, blah, blah... Code that was published below was kinda buggy. Error-free version can be found here.
Subskrybuj:
Posty (Atom)