Inspirated

 
 

February 6, 2010

CSV Auto-Responder v1.0 — S60 3rd Edition Compatibility

Filed under: Blog — krkhan @ 3:16 pm

As I finally got my hands on a S60 3rd Edition device last week in form of E71, it was obviously the time to fix my PyS60 scripts for the new version. By using some workarounds to fix some brain-dead compatibility issues which I will detail in a later post, I was able to get CSV Auto-Responder running properly:

csvautoresponder-1.0.zip
CSV Auto-Responder v1.0 Screenshot

Tags: , , , , , , , , ,

January 6, 2010

(GUI, Mathematical Equations, Scientific Plotting) = (GTK+, LaTeX, Matplotlib)

Filed under: Blog — krkhan @ 10:16 am

GTK+ needs no introduction. LaTeX is the first thing that pops in anyone’s mind if mathematical equations’ typesetting is under consideration. Matplotlib — while not as well-known as the former two — is the super easy and elegant solution for scientific plotting on *nix platforms.

For an application demo, I required all three. Past experience has taught me that the most straightforward way of “gluing” things together is Python. GTK+ therefore = PyGTK. Next up was LaTeX, and a previous solution of mine for embedding LaTeX in PyGTK came to the rescue. The final requirement of Matplotlib was fulfilled without any hassle since the library was already written in Python.

The collective result was pretty:

radareq-0.1.tar.gz

Screenshot of GTK+ with LaTeX and Matplotlib
(Click on the image for larger version.)

The linked tarball contains the Python scripts for the application. For everything to run smoothly, LaTeX and Matplotlib packages need to be installed on your system. If you encounter any issues running the code, feel free to flame your distribution for the apparent lack of sanity regarding package management.

Tags: , , , , , , , , , , ,

December 3, 2009

Bookmark Undertaker — Check your Firefox favorites for dead links

Filed under: Blog — krkhan @ 9:24 pm

No matter how much you try to keep the browser bookmarks clean, inevitably they jumble up and one day you realize that you have no idea which links are working and which aren’t. This is where a small utility named AM-Deadlink comes to the rescue for Windows users which checks the links for errors. Somehow, the utility lacked an alternative in the open-source world. And this is where Bookmark Undertaker comes into picture:

Bookmark Undertaker Screenshot
(Click on the image for larger version.)

For the utility, I chose PyGTK as UI backend. For parsing the bookmarks.html files exported from Firefox, I used Beautiful Soup. The latter, I must say, made my life a lot easier by cleverly sanitizing the insanity contained in Firefox’s exported favorites, staying true to the project tagline:

You didn’t write that awful page. You’re just trying to get some data out of it. Right now, you don’t really care what HTML is supposed to look like.

Neither does this parser.

And indeed it does not.

For the time being, the application imports the bookmarks properly and displays their attributes including the favorite icons. It then checks the linked URLs for errors in a separate thread and marks them as working or non-working accordingly. Exporting the bookmarks is next on the TODO-list, while it’s possible that in future I will internationalize the application as well.

Time to purge those pesky outdated 404’s.

Tags: , , , , , , , , ,

November 21, 2009

PyS60 CSV Auto-Responder v0.1

Filed under: Blog — krkhan @ 8:11 pm

The idea is pretty simple actually. You run the application on your cellphone and give it the path of a CSV file. The file contains some data which can be queried. For example, let’s assume that the contents are:

ID,Name,Age,Message
0,The Black Knight,43,”I move for no man”
43,Sir Galahad, 38,”Oh, let me have just a little bit of peril?”
569,John Doe,21,”Hello World”

Next, you choose a keyword; something simple like “Respond”. Then, you put the cellphone aside. It keeps checking the incoming SMS messages and as soon as it receives a message having something similar to … :

Respond 43

… it queries the CSV file and replies to the sender with results of the lookup. In our case, the reply will be:

Received query 43 from +xxxxxxxxxxxx
Found record in database:
> Name: Galahad
> Age: 38
> Message: Oh, let me have just a little bit of peril?

And that is it. The script can be downloaded from this link. Updates in future shall be linked on its specific Inspirated Code page. Here’s the whole thing in action:

PyS60 CSV Auto-Responder

Tags: , , , , , , , , ,

Facebook Friends Graph v0.1

Filed under: Blog — krkhan @ 12:01 am

The motivation behind obsession with these manically large graphs is explained in this previous post of mine. The current post’s purpose is to instead link to the (finally) working code for generating the graphs. I have created a project page at Launchpad for this little application. The trunk contains the most recent code, which is still in its nascent form but works pretty well given an installation of PyGTK, Python GtkMozembed and pydot. I might port the application to Windoze in future provided I get the time for it.

Starting with the tradition of linking to Inspirated Code subsection, latest updates about the application shall be posted on this page.

And as is the custom, the image itself:

Facebook Friends Graph
(Click on the thumbnail for larger version.)
(Warning: The larger version is a gigantic 32 MB PNG image with a resolution of 16517x13808 (even larger than the last time). If you want to view it, I recommend downloading it to your hard-disk first and then opening it outside your web browser.)

Tags: , , , , , , , , , , , , ,

October 31, 2009

HOWTO: Use PyS60’s Bluetooth Console on Fedora/Ubuntu/Debian Linux

Filed under: Blog — krkhan @ 11:03 pm

While developing PyS60 apps is one of the most fun things you could do with your Nokia phone, debugging them isn’t as zippy as one would hope for in a Py development environment. To make up for that, PyS60 gives the developers an option for directly connecting to the interpreter through Bluetooth. Doesn’t sound very appealing? How about this: You connect your laptop with the cellphone, jump in at some place in the code while your app is executing and then use lappy’s big keyboard for exploiting different code and values in the interpreter. Sounds better?

To accomplish this on a Linux distro, you will need the following packages installed on your system:

Name Links
gnome-bluetooth
uucp/cu

After making sure that both are present on your system, install PyS60 on your phone if you haven’t already done so.

Now the fun part:

  1. Switch on Bluetooth in the cellphone.

    PyS60 Bluetooth HOWTO, Mobile screenshot #1

  2. Launch bluetooth-properties and click on “Setup New Device”.

    PyS60 Bluetooth HOWTO, PC screenshot #1

  3. Select your cellphone.

    PyS60 Bluetooth HOWTO, PC screenshot #2

  4. You will be shown a pin.

    PyS60 Bluetooth HOWTO, PC screenshot #3

  5. Enter the pin when queried on the cellphone.

    PyS60 Bluetooth HOWTO, Mobile screenshot #2

  6. The phone should be successfully paired.

    PyS60 Bluetooth HOWTO, PC screenshot #4

  7. Authorize your Linux system to make automatic connections to the phone.

    PyS60 Bluetooth HOWTO, Mobile screenshot #3

  8. As root, run this shell script:
    [root@orthanc ~]# ./rfcomm-listen.sh

    Serial Port service registered
    Waiting for connection on channel 2

  9. Launch PyS60 interpreter and select “Bluetooth Console” from the application menu.

    PyS60 Bluetooth HOWTO, Mobile screenshot #4

  10. Select your Linux machine.

    PyS60 Bluetooth HOWTO, Mobile screenshot #5

    The command you ran in previous step should have new output:

    [root@orthanc ~]# ./rfcomm-listen.sh

    Serial Port service registered
    Waiting for connection on channel 2
    Connection from 00:17:4B:B6:35:31 to /dev/rfcomm0
    Press CTRL-C for hangup

    The cellphone screen should be showing something like this:

    PyS60 Bluetooth HOWTO, Mobile screenshot #6

  11. As root again, open a new terminal and run:
    [root@orthanc ~]# cu -l /dev/rfcomm0

    Connected.

  12. Hit Enter till prompt (>>>) appears, then type:

    >>> import appuifw
    >>> appuifw.query(u'Hello World', 'text')

  13. Viola, you should have an input box on the mobile screen:

    PyS60 Bluetooth HOWTO, Mobile screenshot #7

  14. Enter any text and press the OK key. It should be show up in the terminal you were using to type in code:

    >>> import appuifw
    >>> appuifw.query(u'Hello World', 'text')
    u’Finally’

  15. Exit the interpreter by typing CTRL+D on an empty line:

    >>> import appuifw
    >>> appuifw.query(u'Hello World', 'text')
    u’Finally’
    >>>
    Interactive interpreter finished.
    cu: Got hangup signal

    Disconnected.

Pat yourself on the back. Now, you can use your Bluetooth console to import your modules, execute some stuff and then jump in the middle to test some extra lines or values. In fact, I found it to be a pretty darned good way of learning about PyS60’s API. Res secundae!

Tags: , , , , , , , , , , , , , , , ,

October 5, 2009

Emu8086 Hardware Interrupt Editor & Generator

Filed under: Blog — krkhan @ 7:41 pm

If there has been a closed-source software which I have enjoyed using at my university, it’s Emu8086. The shareware, as the name suggests, emulates the 8086 processor down to the minutest detail. Since 8086/8088 processors form a significant part of my Microprocessor Interfacing & Programming course, I have grown rather fond of the software’s workings.

Recently, I needed to generate hardware interrupts on the emulator. Not only that, but I wanted to use my own service routines for the interrupts so that I could try to understand what happens behind-the-scenes in such scenarios. Reading through the Emu8086 documentation, I figured out that two files are responsible for accomplishing the required tasks:

  1. INT_VECT: A 1024-byte interrupt vector containing 256 entries, with each entry being a 4-byte offset:segment pair.
  2. emu8086.hw: A 256-byte file with each byte representing a corresponding interrupt. A non-zero byte signifies that particular interrupt being active. The byte is set to zero again after the interrupt is serviced.

As manually editing these files became cumbersome, I had to code two PyGTK apps for editing these files in a pretty interface:

Emu8086 Hardware Interrupt Editor & Generator Screenshot
(Click on the thumbnail for larger version.)

The intvecteditor.py file lets the user load/edit/save an 8086 interrupt vector. Similarly, the hwintgen.py allows the user to load an emu8086.hw and then generate/monitor interrupts in it. The utilities require PyGTK to run, but I find it infinitely easier now to analyze interrupt behavior of the emulator. This also lead to an interesting observation, as the emulator checked for interrupts at the beginning of an instruction instead of at its end — something which didn’t confirm with the actual 8086 working model.

x86 is fun. After all,

“Do you program in assembly?”
NOP

Tags: , , , , , , , , , , , ,

September 26, 2009

Facebook Friends Graph — Plotting your social network together

Filed under: Blog — krkhan @ 3:58 pm

Update: New version

While visiting profile of a friend, I noticed that he and I had about 70 mutual friends. Immediately it gave me the idea to plot the common friend connections and see what interesting patterns emerge in the larger picture. Something like this:

Facebook Friends' Graph Sample

In the sample above, the names in circles (“nodes”) happen to be in my friend list. The connecting lines represent their own friendship status. For example, Saad Jasra and Hassan Ahmad are friends among themselves apart from being my friends on their own. Similarly, Ali Zeeshan Ijaz is a friend of both Saad Jasra and Abdullah Afaq Ali.

Luckily, Facebook API had Python bindings available which considerably simplified my task. Those, coupled with pydot, resulted in a dot file with all the required connections. Graphviz did the remaining work:

Facebook Friends' Graph
(Click on the thumbnail for larger version.)
(Warning: The larger version is a 7329x5953 PNG image with a humongous file size of 13 MB. If your hardware specs are squeamish, don’t blame me if it brings your machine on its knees — this is not a DoS attack.)

Now came the intriguing part. The resulting graph was visibly split in two large portions. This resulted from the fact that I had spent a major portion of my life (15 years to be exact) in Saudi Arabia before moving to Pakistan. More interestingly, an old friend of mine from Saudi Arabia — Atif Sheikh — was also enrolled at my university in Pakistan. When I zoomed into the graph, I spotted him at the nexus of two networks. Similarly, the names in the middle of a network were the most connected people in that network. That is, the names congested in the middle of Pakistan network were friends from university and the names at the edges of that network were friends outside the university who didn’t share my academic connections.

I haven’t polished the code for a stable release yet as I doubt that other people would be interested in having gigantic plots of their social lives. Nevertheless, I’ll try to package it in form of a proper Facebook application in near future. After all, as I quoted in a previous post of mine:

“Statistics are like a bikini. What they reveal is suggestive, but what they conceal is vital.”

Tags: , , , , , , , , , , , ,

September 10, 2009

HOWTO: Use LaTeX mathematical expressions in PyGTK

Filed under: Blog — krkhan @ 10:04 pm

I had never really laid my hands on LaTeX until I required it in one of the helper applications for my graduation project. Unfortunately, the requirement wasn’t as simple as producing some documents as I had to embed mathematical expressions on the fly in my PyGTK apps. Googling around for the solution, I found GtkMathView which accomplished something similar to this albeit using MathML. However, my luck ran out on me again as the widget lacked Python bindings. The other solution was to generate transparent PNGs on the fly and include them as GtkImages. This worked rather well, as the final code allowed easy modifications to the generated expressions.

Requirements for the code were:

Final results:

LaTeX in PyGTK

And the simple code behind it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
"""An example demonstrating usage of latexmath2png module for embedding math
equations in PyGTK
 
Author: Kamran Riaz Khan <krkhan@inspirated.com>
"""
 
import gtk
import os
import latexmath2png
 
pre = 'gtktex_'
eqs = [
	r'$\alpha_i > \beta_i$',
	r'$\sum_{i=0}^\infty x_i$',
	r'$\left(\frac{5 - \frac{1}{x}}{4}\right)$',
	r'$s(t) = \mathcal{A}\sin(2 \omega t)$',
	r'$\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$'
	]
latexmath2png.math2png(eqs, os.getcwd(), prefix = pre)
 
def window_destroy(widget):
	for i in range(0, len(eqs)):
		os.unlink(os.path.join(os.getcwd(), '%s%d.png' % (pre, i + 1)))
	gtk.main_quit()
 
window = gtk.Window()
window.set_border_width(10)
window.set_title('LaTeX Equations in GTK')
window.connect('destroy', window_destroy)
vbox = gtk.VBox(spacing = 10)
window.add(vbox)
 
images = [None] * len(eqs)
for i in range(len(eqs)):
	images[i] = gtk.image_new_from_file('%s%d.png' % (pre, i + 1))
	vbox.pack_start(images[i])
 
window.show_all()
gtk.main()
Tags: , , , , , , , , , ,

August 30, 2009

HOWTO: Add grid lines to a GtkDrawingArea in PyGTK

Filed under: Blog — krkhan @ 6:29 pm

Recently, I needed to create some visualizations in a drawing area for one of my PyGTK apps. The PyGTK tutorial had an excellent writeup complete with a working example for immediate help. Unfortunately, my requirements were of a “grid” view on which other objects would be drawn. Naturally, I hoped for some variable on the GtkDrawingArea which I would set to true and have the grid lines drawn automatically. But since I couldn’t find any such magic setting in the API, I had to draw the lines manually.

Consequently, I could use either of the following approaches:

  • Loop horizontally and vertically while draw_line()ing.
  • Draw a 10×10 box as a tile and repeat it over the background.

The first approach would’ve worked faster but involved writing more code. The second was uglier — especially for larger images — but resulted in me having to add only a few lines to the example:

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        xpm_data = ["10 10 2 1",
            " 	c #EEEEEE",
            ".	c #FFFFFF",
            "          ",
            " ........ ",
            " ........ ",
            " ........ ",
            " ........ ",
            " ........ ",
            " ........ ",
            " ........ ",
            " ........ ",
            "          "]
        tile_pixmap, tile_mask = gtk.gdk.pixmap_create_from_xpm_d(
            area.window, None, xpm_data)
        tile_gc = area.window.new_gc(fill = gtk.gdk.TILED,
            tile = tile_pixmap)
        width = int(self.hruler.get_range()[3])
        height = int(self.vruler.get_range()[3])
        area.window.draw_rectangle(tile_gc, True, 0, 0, width, height)

Turning this:

GtkDrawingArea Example

Into this:

GtkDrawingArea Grid Example

Note that the 10×10 tiles’ borders match prettily with the markers on top and left rulers. There will definitely be some more efficient method for achieving this but for the time being tiling serves my needs perfectly.

Tags: , , , , , , ,
« Previous PageNext Page »