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."

Python & DTrace

July 1st, 2011 • Comments Off on Python & DTrace

The following DTrace script can be used to trace Python. It will show you the file, line-number, the time it took to get from the last line to the current one and the (indented) function it is in. This helps understanding the flow of your Python code and can help finding bugs & timing issues. As a little extra it is setup the way that it only shows the files of your code – not the once located in /usr/lib/*. This helps with readability and makes the output more dense since the site-packages are left out.

I now have this running in the background on a second screen whenever I code python. First screen holds my IDE – and next to all the other stuff I do it gives great up-to-date information on the stuff you’re coding. Again proof for the case that coding Python on Solaris is a great idea (Also thanks to DTrace). I know this is simple stuff – but sometimes the simple stuff helps a lot! Now it would only be nice to create a GUI around it šŸ™‚ First the DTrace script is shown; example output is further below…

#pragma D option quiet

self int depth;
self int last;

dtrace:::BEGIN
{
    printf("Tracing... Hit Ctrl-C to end.\n");
    printf(" %-70s %4s %10s : %s %s\n", "FILE", "LINE", "TIME",
           "FUNCTION NAME", "");
}

python*:::function-entry,
python*:::function-return
/ self->last == 0 /
{
    self->last = timestamp;
}

python*:::function-entry
/ dirname(copyinstr(arg0)) <= "/usr/lib/" /
{
    self->delta = (timestamp - self->last) / 1000;
    printf(" %-70s %4i %10i : %*s> %s\n", copyinstr(arg0), arg2, self->delta,
           self->depth, "", copyinstr(arg1));
    self-> depth++;
    self->last = timestamp;
}

python*:::function-return
/ dirname(copyinstr(arg0)) <= "/usr/lib/" /
{
    self->delta = (timestamp - self->last) / 1000;
    self->depth--;
    printf(" %-70s %4i %10i : %*s< %s\n", copyinstr(arg0), arg2, self->delta,
            self->depth, "", copyinstr(arg1));
    self->last = timestamp;
}

Example output:

 $ pfexec dtrace -s misc/py_trace.d
Tracing... Hit Ctrl-C to end.
 FILE                                                                   LINE       TIME : FUNCTION NAME 
[...]
 /home/tmetsch/data/workspace/pyssf/occi/service.py                      144       1380 : > put
 /home/tmetsch/data/workspace/pyssf/occi/service.py                      184         13 :  > parse_incoming
 /home/tmetsch/data/workspace/pyssf/occi/service.py                       47          9 :   > extract_http_data
 /home/tmetsch/data/workspace/pyssf/occi/service.py                       68         39 :   < extract_http_data
 /home/tmetsch/data/workspace/pyssf/occi/service.py                       70         10 :   > get_parser
 /home/tmetsch/data/workspace/pyssf/occi/registry.py                      60         36 :    > get_parser
 /home/tmetsch/data/workspace/pyssf/occi/registry.py                      83         12 :    < get_parser
 /home/tmetsch/data/workspace/pyssf/occi/service.py                       77          8 :   < get_parser
 /home/tmetsch/data/workspace/pyssf/occi/protocol/rendering.py            99         11 :   > to_entity
 /home/tmetsch/data/workspace/pyssf/occi/protocol/rendering.py            81         10 :    > extract_data
 /home/tmetsch/data/workspace/pyssf/occi/protocol/rendering.py            37         11 :     > __init__
 /home/tmetsch/data/workspace/pyssf/occi/protocol/rendering.py            41         11 :     < __init__
 /home/tmetsch/data/workspace/pyssf/occi/protocol/rendering.py            97         14 :    < extract_data
[...]

This was inspired by the py_flowtime.d script written by Brendan Gregg.

The Power of Python & Solaris

June 16th, 2011 • Comments Off on The Power of Python & Solaris

Here is a presentation I gave today to demo the power of Python and Solaris. It’s about creating a fictitious service similar to no.de or Google App Engine but with Python & Solaris. Warning: contains OCCI and DTrace!

Build Python with custom TCL/TK libs

April 26th, 2011 • Comments Off on Build Python with custom TCL/TK libs

Issue with my Solaris installation is that it has slightly older TCK/TK libs. Since I’m currently playing around with some Tkinter stuff and python I needed a newer version to test the TreeView and Notebook widgets for example.

Here is how yo can get Python 2.7.1 up and running against TCL/TK libs which are not system wide installed:

# Switch to TCL directory
cd unix/
./configure --prefix=<some_path>
make
make install
# Switch to TK directory
cd unix/
./configure --prefix=<some_path> --with-tcl=<some_path>
make
make install
# Switch to Python source directory
export LD_LIBRARY_PATH=<some_path>
./configure --prefix=<some_path>
make
make install

Please note the following:

Generating Tkinter GUIs from JSON

April 21st, 2011 • 2 Comments

Source available on GitHub

Yesterday I posted something about creating Tkinter based GUIs via an XML. Thanks to python and all its build in features (like JSON support) it was a piece of cake to rewrite it an now you can create Tkinter GUIs from a JSON definition. Just a few lines (ca 25 lines needed to be changed) later and this request was fulfilled:

To create the same GUI as in the last post the JSON file should look like:

{
    "Frame":{
        "LabelFrame":{
            "text":"Hello World",
            "column": 0,
            "columnspan": 2,
            "row": 0,
            "Label":{
                "text":"Entry:",
                "column": 0,
                "row": 0
            },          
            "Checkbutton":{
                "name":"check",
                "text":"checkbutton",
                "column": 1,
                "row": 0
            },
            "Entry":{
                "name":"entry",
                "text":"checkbutton",
                "width":"30",
                "bg":"gold",
                "column": 0,
                "row": 1,
                "columnspan": 2
            }
        },
        "Button":[
            {
                "name":"ok",
                "text":"OK",
                "column": 0,
                "row": 1
            },
            {
                "name":"cancel",
                "text":"Cancel",
                "column": 1,
                "row": 1
            }
        ]
    }
}

And for sure: the sample code from yesterday – didn’t change šŸ™‚ Again: you gotta love python! And right now the JSON UI definitions are nicer so I might follow this approach for the future.

Also nice about this approach is that within the JSON or XML file you define everything for the Tkinter widgets. Every value of the configuration of an Tkinter widget can be defined in the files. For example all these values could be put in the file for the ‘Entry’ Tkinter widget. Nice ain’t it?

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!

Page 5 of 8« First...34567...Last »