Threads are love. Threads are speed. And more often than not, threads are a consistent PITA. However, I’ve had an accidental epiphany just a few hours ago:
“When in doubt When you need to communicate among threads, use synchronized Queues.”
There. This magic mantra will solve more issues in your life than you can ever imagine, and certainly more than I expected.
Getting back to the topic at hand, adding threading support to the program has sped up the bookmark checking process by a factor of about 435895234. Coupled with fixing of some parsing bugs, Bookmark Undertaker v0.3 is finally capable of providing a quick, stable and consistent way of sanitizing your Firefox favorites:

This time, I’ve also tried to provide Deb and RPM packages on the release page for easy installation by the Debian/Ubuntu/Fedora populace.
Ushering in the era of communist applications:
“If everyone gives one thread, the poor person will have a shirt.” — Russian Proverb
Tags:
Beautiful Soup,
Bookmark Undertaker,
Bookmarks,
Code,
Deb,
Debian,
Fedora,
Firefox,
Mozilla,
Open Source,
PyGTK,
Python,
Red Hat,
RPM,
Technology,
Threading,
Ubuntu
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

(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:
Code,
Equation,
Flag 42,
Graphics,
GTK+,
LaTeX,
Matplotlib,
Open Source,
Plot,
PyGTK,
Python,
TeX
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:

(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:
Beautiful Soup,
Bookmark Undertaker,
Bookmarks,
Code,
Firefox,
Mozilla,
Open Source,
PyGTK,
Python,
Technology
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:
INT_VECT: A 1024-byte interrupt vector containing 256 entries, with each entry being a 4-byte offset:segment pair.
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:

(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:
8086,
8088,
Assembly,
Code,
Emu8086,
Emu8086 Hardware Interrupt Editor & Generator,
Intel,
Interrupt,
Open Source,
PyGTK,
Python,
Vector,
x86
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:
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:
Code,
Equation,
Flag 42,
Graphics,
GTK+,
LaTeX,
Open Source,
PyGTK,
Python,
TeX,
Tutorial
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:
Into this:
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:
Code,
Flag 42,
Graphics,
GTK+,
Open Source,
PyGTK,
Python,
Tutorial