ś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.
piątek, 4 listopada 2011
Dear Python, can I create global variables in functions or methods?
Yeeee... no. Global variables in python are global on module level only. So, the answer is noooo... yes, you can.
Having a problem of a sub-process that has to execute just a callable (function, lambda or class with __call__ defined), and following DRY, I've put common functionality in base class and then created a set of children. As this code is part of much bigger project, DIRAC, where have to follow conventions and use everywhere a set of of global variables (references to services, clients, some global functions, etc.). Of course I was going to import them in a base class, but then what?
Globals are visible at the module level, so if I put them to base class, I would be able to use them in base class module (all functions and methods over there). But what I really want, was to use them in inherited classes to.
No problem, you can always store them as a member of base... Yuck! But then instead of just calling "gGlobalVar.doSomethingForMe()" I have to call it by "self.gGlobalVar.doSomethingForMe()". Awkward! Ugly! Bad!
What come to my mind is a python sequence for looking up symbols. There is always __builtins__ module, right? And this one is first in a line of r lookup, right? So what about storing them over there?
So here is an example:
Dear Python, if you knew how to cook and clean...
Having a problem of a sub-process that has to execute just a callable (function, lambda or class with __call__ defined), and following DRY, I've put common functionality in base class and then created a set of children. As this code is part of much bigger project, DIRAC, where have to follow conventions and use everywhere a set of of global variables (references to services, clients, some global functions, etc.). Of course I was going to import them in a base class, but then what?
Globals are visible at the module level, so if I put them to base class, I would be able to use them in base class module (all functions and methods over there). But what I really want, was to use them in inherited classes to.
No problem, you can always store them as a member of base... Yuck! But then instead of just calling "gGlobalVar.doSomethingForMe()" I have to call it by "self.gGlobalVar.doSomethingForMe()". Awkward! Ugly! Bad!
What come to my mind is a python sequence for looking up symbols. There is always __builtins__ module, right? And this one is first in a line of r lookup, right? So what about storing them over there?
So here is an example:
- let's say this is our module storing "globals", say testGlobals.py:
- and here is a base class, say testBase.py:
Notice, we can't put there anything outside baseClass, as it wouldn't be visible in inherited classes at all. I'm using built-in setattr or __builtins__.__setattr__, cause in pure python __builtins__ is visible as module, but when you import baseClass elsewhere, it would be just a dictionary. If you don't believe just execute this one:
once in interactive session:
and then in batch: - and here it is, a testChild.py
- at least some testing:
- and its output:
Dear Python, if you knew how to cook and clean...
czwartek, 7 października 2010
subversion checksum failed
Well, it happens occasionally that in process of committing changes to the repository some weird problem with bad checksum is reported and the whole commit is blocked:
Commit failed (details below):
svn: Checksum mismatch for '/home/userdir/devel/RTTWeb-etree-branch/cherry/sql/.svn/text-base/Job.py.svn-base', expected: '947e06aeba5bd534250ad1124e3f60d6', actual:' a226478ea1900a7aebdb3bb0cb2d42d1'
How to fix it and don't lost your precious changes? Two ways:
Now guess which way to choose and why I'm so smart now? ;)
Commit failed (details below):
svn: Checksum mismatch for '/home/userdir/devel/RTTWeb-etree-branch/cherry/sql/.svn/text-base/Job.py.svn-base', expected: '947e06aeba5bd534250ad1124e3f60d6', actual:' a226478ea1900a7aebdb3bb0cb2d42d1'
How to fix it and don't lost your precious changes? Two ways:
- VERY, VERY, VERRRY BAD:
- go to the .svn subdirectory, in above example /home/userdir/devel/RTTWeb-etree-branch/cherry/sql/.svn/
- add yourself write right to entries file and open it for editing
- now very curious find and replace 'actual' checksum with 'expected' one, but don't touch any other line/word/character in that file
- save & commit again
- go to the .svn subdirectory, in above example /home/userdir/devel/RTTWeb-etree-branch/cherry/sql/.svn/
- SAFE, CORRECT & ONLY WAY:
- make a copy of working copy somewhere:
cp -R /home/userdir/devel/RTTWeb-etree-branch/cherry/sql /home/userdir/backup - remove wrong file from working copy:
rm /home/userdir/devel/RTTWeb-etree-branch/cherry/sql/Job.py - update working copy from server:
svn up - copy back 'wrong checksum' file to working copy:
cp //home/userdir/backup/Job.py /home/userdir/devel/RTTWeb-etree-branch/cherry/sql - commit:
svn ci -m "recovered from checksum error"
- make a copy of working copy somewhere:
Now guess which way to choose and why I'm so smart now? ;)
czwartek, 23 września 2010
Compiz, whay not?
A few years ago my colleague had shown me Beryl window manager with all this super-duper spinning workspaces, 3D shaking transparent windows on rotating cubes. It sank deeply into my memory. I've tried several times to install it on MBP running Fedora, unfortunately w/o success. Up to now. This what I did:
installation
reboot to load new graphic adapter drivers
switch compiz on from top-level gnome menu "System -> Preferences -> Desktop Effects"
let is auotstart at boot "System->Preferences->Startup Applications->New" and put there:
and save
Ta-dam! It's there. It's 5 minutes job, everything works out of the box.
sudo yum install yum install mesa-dri-drivers-experimental xorg-x11-drv-nouveau \
compiz-fusion compiz-fusion-extras compiz-fusion-extras-gnome \
compizconfig-backend-gconf compizconfig-python \
emerald emerald-themes fusion-icon fusion-icon-qt \
libcompizconfig protobuf
Name: whatever, Fusion Icon
Application: fusion-icon --force
and save
Ta-dam! It's there. It's 5 minutes job, everything works out of the box.
Subskrybuj:
Posty (Atom)
