|
|
|
|||||||||||
Python trick: How to make lazy objects?November
22
This time I'm about to talk about lazy object, I called like that because they are more or less similar to what we use in lazy treeviews (In Gtk) where the Tree shows expanders but didn't load the child nodes until you expand the node). What I want to do is create objects and count them, but don't load any data until I need it. Why would I need this?. Well. I'm currently writing a program for my employer, this program load a list of customers and shows how many accounts it has. As this accounts are kind of complex I create an object for them. This object is responsible for load/save the data for it, and any data you need is accessed via properties. In my first approach, the object was loading the data as soon as the constructor was called, but sometimes I never access to any info in that object, remember, a customer may have many (and I mean really many) accounts, then why load all the data if there is a chance that I'll never use it?. Well, that is because there is a chance that I have to use that data. Then how could I create the object and then load the data when somebody require it?. That may be easy, may not. :-). One approach is making use of python properties. Just create the object and when someone want to use a "value" from the object use the property to retrieve the data, process it and then return a value. A simple property may be defined like this:
class demo(object): def get_some_property(self): #Here you can make use of whatever you want to retrieve the data pass def set_some_property(self, value): #Here you get a value and save it. pass some_property = property(get_some_property, set_some_property, None, 'Doc') I just create a class "demo" and the property "some_property", everytime you use this:
d = demo() d.some_property Python will call the method get_some_property allowing you to do your trick. But if you have an object with more and more properties is better to get all data at one time and then just use properties to return a piece of it. You could call a method on every property if the data container is empty but for me it is not the best. What you could do, is get the data the first time you need it. But, how could I know when a property/variable is requested on my object? Well, Python classes have several reserved methods, and one of them is __getattribute__. This method let you handle every request to any property/class variable in your object. With this you can digg in your object to know if the container is empty, and if it is empty fill it with data before returning the property value :-). Lets say that the demo object have a __data dictionary containing the data. and propeties look into it for the right value, it also have a reference to the storage layer called storage.
class demo(object): def __init__(self): self.__data = {} self.storage = storage() def __getattribute__(self, name): obj = object.__getattribute__(self,name) #Check if the requested property is __data if name in("__data", '_account__data'): #Get the reference of __data d = object.__getattribute__(self,'_account__data') if not d: #Get in only if __data is empty. #Get your s = object.__getattribute__(self,'storage') #We can assign without using object.__getattribute__ self.__data = s.get_data() #Get the value again obj = object.__getattribute__(self,name) return obj Pay attention that I used object.__getattribute__ instead self.property, if I use self.property will call self.__getattribute__ and will cause a recursion exception. With this, we can create a great number of objects quickly and reducing the memory footprint. Christine 0.6.0-pre1: first win32 buildNovember
22
![]() This night I have been working on the Win32 build of christine. It seems to be working fine. This version is just a test, for those that wanted to use christine on Microsoft Windows. Altough that I have made some tests on a clean Windows XP installation cannot guarantee that the Win32 will work on your computer. Anyway, if you are brave enough, download the win32 binary and report any bug you found on it. If you want to compile it, you will need this:
The build script is in win32resources/christine_setup.py, just copy it to the source root and run python christine_setup.py -q py2exe to build it. You can download the win32 binary from here: http://downloads.sourceforge.net/project/christine/christine/0.6.0-pre1/Christine-0.06.00-pre1.exe I forgot to mention that you have to install Microsoft Visual C++ 2008 redistributable in order to work. Image as background in a Gtk Application.November
18
This time I'm going to talk about putting an image as the application background in Gtk. In Gtk we are used to leave the colors of the application to the theme, but sometimes we will need to use an image as background. I already wrote how to draw a pixbuf in a gtk.DrawingArea (Esp), we could use that, but we will "draw" directly on the widget window instead. Yes, I said the widget's window instead the widget itself. You should know that every widget that has been packed in a container has a gtk.gdk.window object and is the responsible for containing your widget. Well, we can draw on that object. What we need is to create a simple gtk.gdk.Pixbuf and call the gtk.gdk.window.draw_pixbuf method using your widget.window object on the expose-event. The code should look like this:
#!/usr/bin/env python import gtk def draw_pixbuf(widget, event): path = '/home/markuz/wallpapers/WMwall1024x768.gif' pixbuf = gtk.gdk.pixbuf_new_from_file(path) widget.window.draw_pixbuf(widget.style.bg_gc[gtk.STATE_NORMAL], pixbuf, 0, 0, 0,0) window = gtk.Window() window.set_title('Drawing Test') window.set_size_request(640,480) window.connect('destroy',gtk.main_quit) hbbox = gtk.HButtonBox() window.add(hbbox) hbbox.connect('expose-event', draw_pixbuf) button = gtk.Button('Press Me!') hbbox.pack_start(button, True, False, 10) window.show_all() gtk.main() ![]() It is just a window with an HBoxButton as container and a Button in the middle. The button draws normal, but the HButtonBox is drawing its gtk.gdk.window with a pixbuf. Nokia Internet Stick CS-10 on LinuxNovember
11
Anyway, in the office bought this pretty "Internet Stick", and with a small googling I found that it was possible to make it run on Linux. In some blogs I read that you just need to umount the device using "eject", This is because this stick identifies itself as a "CD-ROM" and when you use "eject" it changes its behaviour and start acting like a modem. But I have one problem, when I insert this device in my computer's usb port dmesg shows errors while reading the device. Then, it never gets mounted (the first step for umounting it :-)) and then it can't change the way it worked. Googling a bit more, I found that we can use udev to avoid this connect-wait_for_recognition-umount-dial procedure. You just need to create this rule in /etc/udev/rules.d/90-nokia-zerocd.rules
SUBSYSTEMS=="usb", SYSFS{idVendor}=="0421", SYSFS{idProduct}=="060c", ACTION=="add", PROGRAM=="nokia-testcd %M %s{serial}", RUN+="/usr/bin/eject -s %k", OPTIONS+="last_rule" As you can see, it depends on a program "nokia-testcd", it is just a simple bash script with this code: Read More...Christine running on MS Win32October
31
Finally!!!, Christine, my beloved media player is running on Windows, this is something that I really wanted to do for the next release. I had spend the last nights trying to compile it, first I tried to compile GStreamer, then I realized that there are binaries in the GStreamer WinBuilds project, with a little less work, I tried to compile Christine using cygwin, but it didn't worked. Then using the Python's distutils I compiled the C modules and I created a binary with Py2exe There it is, running. I still have a lot of work to do, because Christine is written for UNIX and several technologies like Dbus are not in win32, although it seems to be in progress. I'm running it on a virtual machine and I haven't tested if it really plays something, I'll give a try tomorrow. For tonight I'm happy :-) Some shots: ![]() ![]() Christine playing videosOctober
23
![]() Christine playing videos Originally uploaded by markuz I was playing with gstreamer video sinks, I found that using cacasink and aasink you can play videos in ascii mode ;-) funny even when (for me) has no good use. Christine without menuOctober
22
![]() Christine without menu Originally uploaded by markuz This is christine without the menubar and the side bar. It look pretty clean don't you think? Christine small modeOctober
21
![]() Christine small mode Originally uploaded by markuz This is Christine in small mode (Ctrl+d). |
|
|
|||||||||
|
|
|||||||||||
Recent Comments On Blog
Marco Antonio Islas Cruz on
Marco Antonio Islas Cruz on
Getting ready
Marco Antonio Islas Cruz on
Python: Create win32 services using Python and py2exe
jopython@gmail.com on
Python: Create win32 services using Python and py2exe
yodenuevo on
Holy Shit!
markuz on
Holy Shit!
yo on
Holy Shit!
Gustavo on
Things that happen last week
Marco Antonio Islas Cruz on
Christine: rola_christine.py
k001 on
Christine: rola_christine.py