Marco Islas Blog




Python

  • Python MySQldb little trick

    if you are using Python and MySQLdb you may find that using special chars like ñ or ó break the program raising an DecodeError because character xxx is not in the range...
    MySQLdb has a way to set the charset for the connection but it is useless if you are using MySQL prior to 4.1. If you are having problems with this, then try doing this:
    import MySQLdb
    import sys
    reload(sys)
    sys.setdefaultencoding('latin-1')
     
    Replace "latin-1" with your desired encoding and try it.
  • Playing with Nokia S60.




    Me :-)

    Originally uploaded by markuz



    I've just started playing with Nokia S60v5. I'm currently using Python to develop some learning stuff. Remember, Symbian is now Open Source, a little bit late, but Open Source at the end.

  • Right now




    Right now

    Originally uploaded by markuz



    3:20 in the morning and I'm working, I need to finish a program that I wrote almost a year ago, that was a Demo program and in 4 days (right now just 2) should be production ready!... I'm gonna need a double Monster dosis.

  • Monster




    Monster

    Originally uploaded by markuz



    Working :-)

  • Christine 0.6.1

    I just want to let you know that Christine 0.6.1 is in the net now You can download the sources from here: Binary Packages
  • Christine update for December 01

    Tonight I have been working on Christine, in the GUI, IMO a media player that work fine should also look good and sometimes GTK just don't fit on that, why? it is because GTK is a general purpose graphical toolkit and everything is general, I wish GTK has some more widget options (like more buttons that fit a specific function, like those small rounded with a simple question sign help buttons)

    christine buttonsFortunately, you can write your own widgets and make them look as you want. Tonight I have been working on Christine's new buttons, just for the Previous, Play/Pause, Next. Creating custom buttons is not a complicated task, just subclass gtk.Button and connect your button "expose-event" signal to a custom function, remember at the end of this function return "True" in order to stop the signal emition. If you don't do this the gtk.Button will do its task and will redraw the button.


    Christine

    As usual, you can check out the source code from the svn

  • Python trick: How to make lazy objects?

    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 build


    Christine on Win32

    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:

    • Python 2.6 (I built Christine with it, but python >= 2.5 should work too)
    • Gstreamer Winbuild (http://www.gstreamer-winbuild.ylatuya.es/doku.php)
    • Microsoft Visual C++
    • py2exe (http://py2exe.org/)

    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.

    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()
     
    And the result should be like this:

    Putting an image as background in Gtk

    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.
  • Christine running on MS Win32



    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 running on Win32 Christine running on Win32Coding Christine
Previous page 1 2 3 4 ... 16 Next page
156 entries




twitter logo




Recent Comments On Blog

avatar
Marco Antonio Islas Cruz on
 
avatar
Marco Antonio Islas Cruz on
 
avatar
Marco Antonio Islas Cruz on
 Getting ready
avatar
Marco Antonio Islas Cruz on
 Python: Create win32 services using Python and py2exe
avatar
jopython@gmail.com on
 Python: Create win32 services using Python and py2exe
avatar
yodenuevo on
 Holy Shit!
avatar
markuz on
 Holy Shit!
avatar
yo on
 Holy Shit!
avatar
Gustavo on
 Things that happen last week
avatar
Marco Antonio Islas Cruz on
 Christine: rola_christine.py