Entries Tagged 'python' ↓

with: the lazy programmer CLI tool.

The other day Ricardo came up with an idea which I found great, so we went and implement it. Since then it has grown a nice set of features and I think it’s ready to test the world (well it was a week ago but I haven’t had the time to make the post).

In general the idea is to reduce the ammount of typing you do in the CLI.

I wrote a nice “extensive” readme file, which also showed me the very cool mime type “text/x-trac-wiki” so the link before is directly into SVN with trac wiki syntax formatting, sweet!

Since it’s a one file program there is no need for a installer, so just download it from here. If you are lazy enough the Makefile has a install target, and in case you where wondering the build.xml is just a sample. Although I could make it install the python code just for fun (evil laugh).

Last but not least I finally got my trac up (I’ll add another post for that) but if you find a bug please submit the patch here

Enjoy!

ClockTicker.py vs ClockTicket.java

Some people think that when scripting language abocates say “less code” is because they are lazy and that they are sacrificing performance over code. This article plans to counterpoint that with a small python/java program.

A little background first. We are working on a little project in which the server is written in python and we’ll have several clients (including one J2ME), also one of the people involve knows nothing about python. Anyway one of the proposals for server-client interaction is having the client pull every X seconds for updates. So I through of an algorithm for making this a bit more intelligent than just setting X to some value. The algorithm itself is very simple. it keeps track of the last Y plays, actually of the time they took, and then using a simple average calculation it will try to guess the next poll time. So basically if it’s a fast game (each player will play their turn fast) then the updates will be more frequent. it will also have a min poll time so clients won’t update way too often, and of course the server will have a timeout but that’s outside of the scope.

Below implementations written in python and java.

warning: sorry for the small code I’m still looking for a decent code highlighter for wordpress.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class ClockTicker
{
    private ArrayList data;
    private final int MIN;
    private final int SIZE;
    public ClockTicker()
    {
        //this(10,20)
        this.SIZE=20;
        this.MIN=10;
        data = new ArrayList(this.SIZE);
    }
    public ClockTicker(int min, int size)
    {
        this.MIN=min;
        this.SIZE=size;
        data = new ArrayList(size);
    }
    //not going to waste my time on the other 2 constructors
    public void add(Float item)
    {
        if (data.size() == this.SIZE)
            data.remove(data.size()-1);
        data.add(0,item);
    }
    public float next()
    {
        float sum = 0;
        Iterator i = data.iterator();
        while(i.hasNext())
            sum+=(Float)i.next();
        float ave = sum/data.size();
        return Math.min(MIN,ave);
    }

    public int len()
    {
        return data.size();
    }

    public static void main(String args[])
    {
    ClockTicker clock = new ClockTicker();
    Random random = new Random();//or could have used (float)Math.random(), this seems less ugly
    for (int i = 0;i<50;i++)
        {
        float value = random.nextFloat();
        clock.add(value);
        System.out.printf(”%s,%s,%sn”,value,clock.next(),clock.len());
        }
    }
}
class ClockTicker():
    def __init__(self,MIN=10,SIZE=20):
        self.data=[]
        self.MIN=MIN
        self.SIZE=SIZE
    def add(self,item):
        if len(self.data) == self.SIZE:
            self.data.pop()
        self.data.insert(0,item)
    def next(self):
        return min(self.MIN,sum(self.data)/len(self.data))

import random

clock=ClockTicker()
for i in range(50):
    value=random.random()
    clock.add(value)
    print value,clock.next(),len(clock.data)

python total of 19 lines, and just 11 for the ClockTicker
java total of 55 lines and 39 for the ClockTicket class, but lets be fair I use the { in a new line (hi k&r fanatics) those don’t count. so it’s -1 line for each method and class. that leaves us with.
java K&R total of 47 lines and 33 for ClockTicket.

That means the python code is close 66% shorter then the java counterpart. One thing I should point out is that I wrote the python code first and the java one is a port but if you look at the java code, it follows java conventions and IMO it’s a fair competitor.

1. lossy data types, this is probably the most important one when I was writing the java code for some reason I wrote public void add(int item) and I didn’t notice that until the ave calculation in which the result was a float, which made me saw my mistake that I actually needed a bigger number to store time deltas, even worst I forgot that the time measured since epoch was stored in a long not a float. Anyway the python code works with int,floats,doubles,probably even strings. In the java counterpart I’ll probably have to write one class for each different datatype or just handle them in the same class with method overloading.

2. default parameters: the python’s init method
def __init__(self,MIN=10,SIZE=20)
is equivalent in java to
public ClockTicker()
public ClockTicker(int min, int size)
public ClockTicker(int size)
public ClockTicker(int min)

lets leave the fact that the last two won’t compile and think of all the boilerplate code that needs to be written just to have different ways to create a constructor. in java it can be minimized with calls to this() but that is at least 3 lines of code per constructor and a couple of curses from whoever is following that code. So we are looking at at least 10 more lines to achieve the same functionality.

3. named parameters: Back to the last two constructors issue. In java it will have to either be an ugly hack inside the two constructor parameter that if you call it as ClockTicker(null,size) min will be set to default. Or an even uglier one like ClockTicker(int minOrSize) and it will guess what you are passing in. But what most sane java programmers will do is something like ClockTicker c= new ClockTicker(); c.setMin(); or c.setSize(); which means adding even more lines of code and lets not get into the issue of fixing setSize so it won’t eat already present data. in python you just do what you need ClockTicker(MIN=100) or ClockTicker(SIZE=10) and done.

4. lists,dicts,etc as native data types and not API:
This is not really a feature that will directly make the size of the code smaller, but the fact that they are fundamental data types makes the developers of the std. api use them more often. Therefore functions like sum,min and len are prepared to handle Collections of data while in the Java counterpart they need to be hand coded outside of the std. api. Which is what is happening in the next method.

5. the fake access control (public,private,etc.)
One of the first things a good java programmer learns is to make class variables private and provide methods to access them,
• this is because setters should make sure the Object don’t gets corrupted.
• changes in the internals of a class will not affect client code.
• provide a clean API
• provide security of the object

Now in practice most setters don’t check anything, noone changes the name of a variable inside the class and if they do they normally do not keep the old setter/getter pointing to the new variable, until they compile everything crashes and they go back either in the name change or readd the old setter/getter pointing at the new name. About the clean API lets suggest an alternative lets say all “private” methods start with a symbol and and by convention (something the java crowd loves) lets say that symbol is _ . So in the event that you call a method that starts with _ it will crash with an access error? hell no if you really need my private method by all means use it, now don’t come crying to me when I break your code because I changed my private stuff, fair enough right? now think of all the setters and getter that are out there. All those millions of lame, useless lines of code, waiting to be strip out of existence to make tarballs,binaries, ISOs, backups, internet transfers and compile times smaller. ohh and about security just use reflection like this or this

6. primitives issue, this is more a language flaw in java than a real advantage in LOC. We have to admit that with autoboxing it makes code smaller, although official documentation states autoboxing is slow. Therefore this is not the optimal solution to fix the headaches of working with collections and primitives

7. And just to mess around with some people, time(1) says this

for the python code
real 0m0.083s
user 0m0.040s
sys 0m0.008s

and for the java code
real 0m0.117s
user 0m0.068s
sys 0m0.012s

so in this particular case python is faster, and just in case you where wondering neither program is optimized. They where both written for readability since after all the idea of this was to show a way the polling could be implemented.

Overall the python code is more readable, has more functionality, has less LOC and is faster

wallpaper thingy 0.1

for the impacient ones download it from here

Why?
Ok so after almost killing myself over bonobo I got this stable. if you read my previous post on this I didn’t found anything I really liked. While developing this I came across 2 very nice programs, although I have too say they both suffer from the “way too many features problem”, to be fair wp-tray and desktop drapes

Features
wallpaper thingy (please please someone give me a good name for it) will very very simple following the UNIX tradition (do something and be great at it), what is this something

  • impress you with new wallpapers, therefore the default is random from a dir.
  • and it will let you configure many aspects of this (change interval,wp dir, icon to show, and maybe go back to the one before)
  • and that’s it. unless you have a very good addition.
  • Provides a CLI and a gnome-applet interface with the same code thanks MVC :)

Instalation
download, untar, sudo make install, then add key to gconf as the README file says.

Usage
Please check the README file, but it’s very simple set your wallpaper dir.

  1. As an applet, right click the a panel then “add to Panel” look for wallpaper thingy, then add, after that everytime you click the icon (or 60 secs pass*) you will get a new wallpaper
  2. As a cli script, run wp_thingy.py script (no params)

* this will be configurable, if you can’t wait go to wp_thingy.py:34 and change it or delete line 36 so it will never autorefresh

Wanna help?

  • Here is what you can do, check the trunk and send me some patches for all the TODO
  • give me some feedback (please do not post at trac.)
  • run time wp_thingy.py and send me that together with the size/structure of your wallpaper dir to see if the algoritm can be optimized.
  • tell everyone about it :)

Turbogears 1.0b1 on webfaction!

the guys at webfaction updated their control panel to the latest TG

If you have an older installation the best way to upgrade will be to make a backup of your existing turbogears project, delete your current app from the panel and then add the new one.

Just go to “Applications” submenu set Turbogears (1.0b1) then link it on the “websites” submenu, wait a min so the server will refresh, while at it copy your app back to your $HOME/webapps/appdir and your done.IMO webfaction is the best thing out there for shared TG hosting their servers are reliable and their support is great, it’s a very nice service even as a testing/temporary environment.

check out their hosting plans and if your interesting sign up

if you really find this “article” interesting then give me some credit :)

keep up the good work guys, specially Remi :)

show_stock buttons in pygtk

So I’m learning pygtk to write a small program inspired by my last post.

and I wrote this to see if I really undestand how the tookit worked.

I’m reading http://www.pygtk.org/pygtk2tutorial/index.html which seems to be the official tutorial and it’s written very well and/or gtk is a very handy tool.

I personally like a lot the way they do the table layout it’s much more simplier then it’s java cousing. Since you can actually make a mind map on how big each component is based on it’s numbers instead of having to add them up and see if they collide.

The funniest part of all is that I wrote this in about 30min this morning before going to work and I have spend 15 more minutes making it “presentable”, after that I spend almost 2-3 hours figuring out how to post the damn thing to wordpress, but that is the topic of my next post. for now enjoy the code.

Bonus points if you tell me why lines 48-49 screw up the layout.

[python]

#!/usr/bin/env python

import pygtk
pygtk.require(’2.0′)
import gtk

#this will generate the list of all the stock options
stock_options = [option for option in dir(gtk) if option.startswith(”STOCK”)]

class Stock_Buttons:

def createButton(self,stock_option):

#ugly but this is a test.
button = gtk.Button(stock=eval(”gtk.”+stock_option))
button.connect(”clicked”, self.clicked,stock_option)
button.show()
return button

def clicked(self,widget,data=None):

print “gtk.Button(stock=gtk.%s))” % data

def __init__(self):

#setting some defaults.
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title(”Sample stock buttons”)
self.window.set_border_width(10)#this will handle both gui events (click the X) and system events like
#the kill command.
self.window.connect(”destroy”, lambda wid: gtk.main_quit())
self.window.connect(”delete_event”,lambda a1,a2:gtk.main_quit())

#small trick to get the the size if the grid
#93 boxes fit into a 10×10 grid
#45 boxes fit into a 5×5 grid and so on.
import math
length = math.ceil(math.sqrt(len(stock_options)))

#pasing floats to gtk is deprecated so to avoid that we make sure it’s an int
length = int(length)
print len(stock_options)
#we set false so gtk will strink the buttons.
table = gtk.Table(length,length,False)
#set one widget on each 1×1 slot until we run out of widgets
for index,option in enumerate(stock_options):
i=index / length
j=index % length
#~ why this shows a weird layout?
#~ i=index % length
#~ j=index / length
table.attach(self.createButton(option),i,i+1,j,j+1)

self.window.add(table)
table.show()
self.window.show()

def main():

gtk.main()
return 0

if __name__ == “__main__”:

Stock_Buttons()
main()

[/python]

I’ll post a screenshot but the upload thing doesn’t wants to work unless I give 777 to the dir so no way, same goes with the raw code, ones I’ll figure out how to do it I’ll update this.

mysqldb or mysql-python windows binaries

I was supposed to post this when i did it, but I didn’t had the … insert excuse here… to finally set up my blog. So here it is now.

I finally compile the driver to work under windows the exact steps I do not know, really I did so many diferent things that I can’t reproduce them. But I’ll do it again one day for now you can get the driver from http://sourceforge.net/project/showfiles.php?group_id=22307.
Yay me over 10k downloads!

by the way thanks to JJ (my local C guru who help me undestand why the driver didn’t compile) and to Andy the author of the driver