No Huddle Offense

"Individual commitment to a group effort-that is what makes a team work, a company work, a society work, a civilization work."

Generating Tkinter GUIs from XML

April 20th, 2011 • 1 Comment

Update: Please also see this post!

Sometimes I just need to develop some GUIs for testing and demos. For 80% cases I hack those kind of codes in Python so Tkinter is the best choice there. Pretty old school – I know 🙂

I would recommend Qt (using PySide or PyQT) for bigger GUI projects but for interoperability (Tkinter is part of the python download and runs everywhere :-)) issues and development speed I still choose Tkinter most of the time 🙂

What bugged me was the amount of code needed to create a simple GUI. Especially defining the GUI itself in a programming language is a pain – so why no split those things? Defining the GUI in an XML file and just use python to bind some operations behind it…An old blog post here describes this using Tkinter. So a peace of cake (esp. with Tkinter). I made some slight modifications (Support for frames in frames in frames etc.; grid geometry manager instead of pack manager; minor improvements on the XML side etc.) and now you can write a simple XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<Frame>
    <LabelFrame text='Hello World' borderwidth='2' relief='groove' column='0' row='0' columnspan='2'>
        <Label text='Entry:' column='0' row='0'/>
        <Checkbutton name='check' text='checkbutton' column='1' row='0'/>
        <Entry name='entry' bg='gold' width='30' column='0' row='1' columnspan='2'/>
    </LabelFrame>
    <Button name='ok' text='OK' column='0' row='1'/>
    <Button name='cancel' text='Cancel' column='1' row='1'/>
</Frame>

Write some simple python code so the GUI actually does something:

[...]
xml_ui = TkFromXML()
root = xml_ui.initialize('ui.xml', title = 'Some test gui...')

# config vars for checkboxes etc.
c = xml_ui.checkbox('check')
    
def ok():
    print xml_ui.entry('entry')
    print c.get()
    root.destroy()

# add button behaviour
xml_ui.button('ok', ok)
xml_ui.button('cancel', root.destroy)

root.mainloop()

So if ‘OK’ is pressed it will output the text inserted in the Box and the state of the Checkbox. Overall you get something like this:

Simple ain’t it? Comments more than welcome – I tend to release this on GitHub soon!

Fun with Joyent’s Node.js cloud analytics

April 2nd, 2011 • Comments Off on Fun with Joyent’s Node.js cloud analytics

Since Joyent release their no.de service I wanted to give it a try. Especially since they added an analytic feature based on DTrace which is described in all details here: Joyent’s wiki. Also nice is this blog post.

So I was tempted to play around with both for a while now. Finally I came up with the idea of drawing words in the analytics panel of the no.de service. Just like a stock exchange ticker with some scrolling test.

So I wrote a simple server.js which responses to different HTTP method (POST, PUT, GET, DELETE, HEAD) request with different latencies. I created an instrumentation in the cloud analytics panel of the no.de service for HTTP server operations (method) over latency. Now only thing left was to write some python code to execute concurrent HTTP requests using different HTTP methods. So 5 concurrent requests with HTTP GET, POST, PUT, DELETE and HEAD would result in vertical 5 dots. 3 concurrent request with HTTP HEAD, PUT and GET in 3 vertical dots with some space between them. I guess you get the picture.

Here are some screenshots:



Hardest part was to figure out the right values for the latencies. Took a while to figure out those who printed the nicest characters without too many ‘drawing erros’.

RSA implemented in Python

March 31st, 2011 • Comments Off on RSA implemented in Python

Just needed to work through some algorithms lately. Best book/reference in that area is most probably the third edition of Introduction to Algorithms. Here is some python code with references to the pages in this book describing the RSA algorithm:

#!/usr/bin/python

def euclid(a, b):
    """
    Taken from page 935
    """

    if b == 0:
        return a
    else:
        return euclid(b, a % b)

def extended_euclid(a, b):
    """
    Taken form page 937
    """
    if b == 0:
        return [a, 1, 0]
    else:
        previous_d, previous_x, previous_y = extended_euclid(b, a % b)
        d, x, y = (previous_d, previous_y, previous_x - a // b * previous_y)
        return [d, x, y]

def generate_keys(p, q):
    """
    Generation of public and private keys...
    """

    n = p * q
    m = (p - 1) * (q - 1)

    e = long(2)
    while e < m:
        if euclid(e, m) == 1:
            break
        else:
            e = e + 1

    dd, x, y = extended_euclid(m, e)
    if y > 0:
        d = y
    else:
        d = y % m

    return [(e, n), (d, n)]

def rsa(p, q, msg):
    """
    RSA walkthrough - Taken from page 962
    """
    pub_key, priv_key = generate_keys(p, q)

    print 'Public Key: ', pub_key
    print 'Private Key: ', priv_key

    e, n = pub_key
    d, n = priv_key

    crypted = (msg ** e) % n
    print 'Crypted value is: ', crypted

    original = crypted ** d % n
    print 'The original number was: ', original

if __name__ == "__main__":
    p = long(raw_input('First prime: '))
    q = long(raw_input('Second prime: '))

    msg = long(raw_input('Number to test rsa with: '))

    rsa(p, q, msg)

My Software Development Environment for Python

February 21st, 2011 • Comments Off on My Software Development Environment for Python

Python is my favorite programming language for multiple reasons. Most important though is that it has a strong community, a Benevolent Dictator For Life (BDFL) and allows rapid development of high quality software. I love automation of processes wherever possible because it saves time. Here is a list of tools, methodologies, stuff I use to ensure code quality:

Tools I sometimes use during the development of code:

Thinks I would like to have replacements for:

Overall I’m pretty happy with this setup and find it good for fast coding. I usually write unittests (With test for success, failure and sanity for every routine) first before starting coding. I don’t find it a time waste but actually I’m pretty sure that the overall setup allows me to code and refactor faster.

Maybe others or you (?) also want to writeup their setups and we can start a site like The Setup for Software Development? Please Retweet and share the link!

Update: Also have a look at this Python and Dtrace blog post.

OCCI and More

November 15th, 2010 • Comments Off on OCCI and More

Busy times – but this post nicely wraps up what is going on regarding OCCI!