Inspirated

 
 

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 24, 2009

The curious case of Screenlets

Filed under: Blog — krkhan @ 7:40 pm

Screenlets

Apple fans have Dashboard. KDE folks have Plasma. Gnome/Xfce people have, er.. tough choice.

Everyone likes desktop widgets. They’re pretty, and can prove to be really helpful with careful setup. Over the past few years, I have tried a few different widget frameworks and it’s kind of a strange phenomenon that all of them died the slow open-source death. adesklets, gDesklets and now Screenlets have bitten the dust. Screenlets, however, deserves special mention because of being the most recent among the deceased.

People behind Screenlets deserve credit for providing an easy-to-use framework for desktop widgets, which wasn’t the case with adesklets or gDesklets. Nevertheless, the compliment is in a way reserved for the basic framework and not the screenlets themselves. While it was fun and easy to write new widgets in Python, the existing ones were broken more often than not. There must be 100+ screenlets available online right now; pick any recent vanilla distribution and a considerably many will fail to work properly on it. Perhaps this is one of the reasons why the package never made it into Fedora repositories. The base product had significant potential, but the end-results built upon it were — in the greater picture — largely a disappointment. Before the situation improved however, Screenlets passed away quietly. Without even an obituary on Wikipedia or the project page itself. People like me who were waiting for a stable release kept finding out through Launchpad comments that development has split and moved to a new project called Universal Applets.

UA is still in early development stages, and does not offer even all the features present in Screenlets’ last version (such as widget zoom). But at least among all the remaining Gtk+ widget frameworks, it remains the only one with active development going on. While I wish its developers good luck for what appears to be a more promising framework than any of the ones mentioned above, I can only hope that it doesn’t disappear into obscurity like its ancestors — resulting in a Yet-Another-Widget-Framework. Meanwhile, I’m sticking with Screenlets’ last release since it works reasonable well once you’ve sorted individual widgets’ kinks out.

Sometimes, migrating to Qt doesn’t sound all that bad of an idea.

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

October 17, 2009

HOWTO: Integrate Compiz Fusion with Xfce the right way

Filed under: Blog — krkhan @ 10:03 pm

In past, I had always struggled to find the “correct” way of launching Compiz Fusion while starting Xfce. For a while, I had resorted to the easiest — and not perhaps the prettiest — way of launching Fusion Icon with the desktop autostart files. The problem with this method lied in the fact that Xfwm was launched before Fusion, and the most glaring workaround was to write my own xinitrc files for X startup, which was just uglier anyway.

Xfce’s own documentation is as bare as my memory while running it, so the right way was not actually obvious until I was fiddling around my configuration directory a few days ago. There, I found an interesting file named xfce4-session.xml. To truly exploit this lovely thing, I first copied it into my home configuration directory:

[krkhan@orthanc ~]$ cp /etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xfce4-session.xml ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-session.xml

And then edited the file with a text-editor, making it look something like:

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
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-session" version="1.0">
  <property name="general" type="empty">
    <property name="FailsafeSessionName" type="string" value="Failsafe"/>
    <property name="SessionName" type="string" value="Default"/>
    <property name="SaveOnExit" type="bool" value="false"/>
  </property>
  <property name="sessions" type="empty">
    <property name="Failsafe" type="empty">
      <property name="IsFailsafe" type="bool" value="true"/>
      <property name="Count" type="int" value="5"/>
      <property name="Client0_Command" type="array">
        <value type="string" value="fusion-icon"/>
        <value type="string" value="--force-compiz"/>
      </property>
      <property name="Client0_PerScreen" type="bool" value="false"/>
      <property name="Client1_Command" type="array">
        <value type="string" value="xfce4-panel"/>
      </property>
      <property name="Client1_PerScreen" type="bool" value="false"/>
      <property name="Client2_Command" type="array">
        <value type="string" value="Thunar"/>
        <value type="string" value="--daemon"/>
      </property>
      <property name="Client2_PerScreen" type="bool" value="false"/>
      <property name="Client3_Command" type="array">
        <value type="string" value="xfdesktop"/>
      </property>
      <property name="Client3_PerScreen" type="bool" value="false"/>
      <property name="Client4_Command" type="array">
        <value type="string" value="xfce4-settings-helper"/>
      </property>
      <property name="Client4_PerScreen" type="bool" value="false"/>
    </property>
  </property>
  <property name="splash" type="empty">
    <property name="Engine" type="string" value=""/>
  </property>
</channel>

Lines 13-15 initially referred to Xfwm’s commands, but replacing them with the Fusion Icon ones worked like a charm. This way, Fusion is always guaranteed a launch, which actually wasn’t the case with other workarounds.

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: , , , , , , , , , ,
« Previous PageNext Page »