Posts Tagged ‘Programming’

(on Technorati , Del.icio.us)

Gedit 3.2 GDP Completions on Ubuntu 11.10 Oneiric Ocelot

Curiosity gets the best of me sometimes. Okay, most of the time. Did you know GNOME’s text editor, gedit, has a plethora of extensions which can basically transform it into an IDE? Something I’ve always wanted is intellisense-style autocompletion. The closest thing I’ve found for gedit is GDP Completions Plugin in the gedit-developer-plugins package in Ubuntu.

sudo aptitude install gedit-developer-plugins

However, there’s a bug in that package and the popup menu doesn’t actually work. Ctrl + Space is supposed to bring it up. So you want to add the Gedit Developer Plugins PPA and upgrade to the more recent version.

sudo add-apt-repository ppa:user/ppa-name
sudo apt-get update
sudo apt-get upgrade gedit-developer-plugins

If you try to run gedit now, you’ll notice it won’t… run, that is. Great. I know, right? The problem is that the bzr plugin (also included in the gedit-developer-plugins package) is trying to use the gtk2 version of bzr-gtk, but that doesn’t work in the gtk3 gedit. Anyway, you can pull a copy of the gtk3 bazaar plugin into your local bzr plugins directory. (I found this info here). Create ~/.bazaar/plugins/ if it doesn’t exist.

mkdir ~/.bazaar/plugins
cd ~/.bazaar/plugins
bzr branch lp:bzr-gtk/gtk3 gtk

The gedit-developer-plugins package and gedit should work after that! An alternative to the above would be to add a PPA that includes bzr-gtk 3. Not sure if one exists at the moment, but that would be a cleaner solution. And you thought it would be simple. I know I did. :P

It’s not as polished or featured as other implementations, but it’s a good start. Here’s a screenshot after I type os. then hit Ctrl + Space:

Further reading

External tools plugin

Rolling your own gedit 3 plugin

Canadian Income Tax 2009

If you have any trouble understanding how income tax works or is calculated, play around on this webpage and see if it helps you. (It probably won’t, but you might have fun not learning anything!) It lets you dynamically compare different income taxes within Canada using a pretty graph and it lets you calculate your own (simplified) tax results, whether your income is salary or hourly based.

canitax

Why Did I do this?

I didn’t do my taxes; an accountant did. But when I was reading about them, I stumbled upon a couple webpages and became interested in the differences among the provinces and territories within Canada and
different income ranges. That’s what started this mini Javascript project.

This is not a work of art

I wrote it mostly on the bus using my recently acquired Dell Mini 12 netbook (on Windows XP… ew). And from that experience, I can firmly say that writing even very simple things, it’s good to have a fair amount of time set aside in a relaxed environment. I would write a couple things here and there for 20 minutes or so… then not look at it again for a few days… it took me 5 minutes to figure out what I wanted to do the next time I opened it. The only times I made significant progress was when I sat down for more than an hour. The code wasn’t really designed, it was just… written. It’s messy, there’s lots of hard-coding, poorly named fields and variables (didn’t help with figuring out what I was doing last time), and if it were anything serious, I’d rewrite large chunks of it. And make it prettier. But as it stands, it’s just kinda fun. :)

In addition to being curious about the taxes in Canada, I was also interested in trying a javascript graphing library. I had been impressed with different javascript-generated graphs on the web and wondered how difficult they were to create. I used FLOT (with lots of copying and pasting from examples), and it seemed to work alright, but it depends on JQuery, which I wasn’t familiar with. Actually, I’m still not very familiar with it… and wrote almost everything in regular javascript. I know it’s worth learning, but I guess I’ll save
that for another time. :)

Let me know of any errors in tax calculation.. or code design, for that matter. There’s lots of those, but I’m sure I’m not aware of all of them! hah.

(I’ve been sitting on this post for about 2 months now. hah! Figured I might as well publish it.)

File List Applet – now with more autotools!

I decided that before I did any more work on the applet, I would improve its installation process to make it easier for people to try it out. So, the process to get and build the source now looks like this:

Download

  • Browse source here.
  • Download the source: bzr branch http://stevenbrown.ca/src/FileListApplet

Install

  1. Install dependencies (Ubuntu package names given): sudo apt-get install python-xdg python-gnome2-desktop python-gtk2 python-pyinotify
  2. Branch the source using the bzr command, above.
  3. cd into the directory.
  4. ./configure --prefix=/usr (the prefix is important!)
  5. make
  6. sudo make install
  7. If the applet does not show up in your Add to Panel menu, try restarting the bonobo-activation-server: killall bonobo-activation-server.

Autotools

Autotools is pretty much the standard in source package management on linux. Except for the name, there is nothing automatic about autotools. Every encounter I’ve had with autotools has usually defeated me and left me frustrated and leaving whatever I was working on to do something else. For me, because I had labeled it the next step, it basically stalled the entire project for a while. Most people tend to copy and paste other projects’ autotools setup, but I figured that was overkill for my purposes and I didn’t find anything that quite suited me. I looked at gnome-blog, but it seemed like some stuff wasn’t quite working properly and some was completely unnecessary… in fact, this seemed to be a trend when looking at the autotools stuff in projects. Why is this? Autotools is not simple and due to this simple fact, I think it fails completely on many levels. Developers massage it enough to get it working, but few actually understand it all – I know I sure don’t! So please forgive the sloppiness and feel free to send patches. :) I gave up doing a couple things, like getting the revision number (bzr revno) and including it in the version string (see configure.ac). I know it’s probably something super simple, but I couldn’t seem to pass a variable containing a string as the version….

I feel that GNOME, as a platform for development, could seriously benefit from some kind of frontend to autotools that handled GNOME development nicely and hid as much as possible from the developer (including all those nasty config files that pollute the package tree). Anyway, I did not have an enjoyable time grappling with autotools, but I’ll end this mini-rant here.

File List Applet – GNOME Panel Applet

This is kind of a proof of concept I’ve been playing with. The idea is that finding a file within a folder is often easier by type, and you are often only interested in the most recently modified file. The problem with a file manager is that although you can easily sort by either type or modification time, you cannot filter your view of all the other files you’re not interested in. I previously wanted to address this issue within Nautilus, (and I still believe this functionality would be wonderful in Nautilus), but I ended up doing this much less ambitious applet as a proof of concept.

This applet will let you add any number of folders to it, and will try to categorize the files automatically and intelligently. Currently, it’s more automatic than intelligent as it just looks at the mime-type. Even so, I’ve found it especially useful for keeping track of all my downloads:

Steven is catching up on the latest on planet.gnome.org and has downloaded a couple screencasts demoing the latest and greatest. These files are typically 2-10 megabytes, so they didn’t download instantly. Steven continues reading and forgot about the screencasts until a couple hours later. At that time, he can simply click on the File List Applet, select Downloads, select Video, and look at the top of the list for the newest files. Steven is happy. When finished, he can follow the same process to delete them – without once opening his file manager and being assaulted with ALL the files in his Downloads folder.

Ultimately, I would like to extend the idea to provide the same type/subtype menu system for all files under all folders – a type of summary – but I have not implemented that yet. There are other features in the cooking pot, as well, but I have to get started on some “RL” tasks… like my resume. :)

Screencast

I had a problem recording audio, so I ended up typing as narration. Unfortunately, this makes the YouTube one pretty unwatchable, but you can give the “HQ” version a try.

Download

No tarball yet as it’s still extremely rough.
Browse the source here.
Branch the source: bzr branch http://stevenbrown.ca/src/FileListApplet

Install

Update 2009/04/05: Updated install instructions here. (Some people don’t look at the comments….)

Installing will require some manual modifications.

  1. First, make sure you have the following packages (Ubuntu): python-xdg, python-gnome2-desktop, python-gtk2, python-pyinotify
  2. Then branch the source.
  3. Adjust the FileListApplet.server file’s location to wherever you keep it.
  4. Then copy FileListApplet.server to /usr/lib/bonobo/servers/.
  5. Restart the bonobo-activation-server. killall bonobo-activation-server
  6. Add it to the panel like other applets.

Update 2008/12/05: Added a couple screenshots.

AttrDict Python Module

I’ve been doing quite a bit of Python hacking in my recent free time. A couple days ago, I had the desire for a dictionary that I could access key values just like object attribute names. For example, in the dictionary d={'key':'value'}, I wanted to be able to use d.key to return 'value'. I thought this would be a common desire and probably already exists (and might even be built-in somehow), so I went on to #python and asked the folk there. Nobody knew of anything. So I set off to write my own. I called it AttrDict.

Yesterday, I found something similar already exists. It’s called ObDict. Similar? Very! :P But different enough that I continued mine… and have a somewhat complete python module of my own. So far it’s been a nice opportunity to learn more about Python objects and use the unittest module for unit testing.

It’s not 100% coverage of the dict interface, but it’s pretty close and I think it’s usable enough that I can slap a quick release together and work on other stuff. Bugs/patches/testing/suggestions welcome.

Update:

Wooops, forgot to write a bit more about it. Here’s the docstring from the module:

Simple extension of the built-in dictionary so that dictionary keys are mirrored
as object attributes. This is for convenience. You can do things like this:

d = dict() #standard dict, for comparison
a = AttrDict() #

d[‘ok’] = ‘this is ok’
d.ok -> AttributeError raised
d.ok = ‘test’ -> AttributeError raised

You cannot assign attributes to standard Python dicts.

a[‘ok’] = ‘this is ok’
a.ok -> ‘this is ok’ #attribute is automatically created
a.ok = ‘changed’
a[‘ok’] -> ‘changed’ #and the dict value is automatically updated
a.ok2 = ‘new value’ #adding new attribute, ok2
a[‘ok2′] -> ‘new value’ #dict key is automatically created

This introduces a limitation on the dictionary keys such
that they must be strings and provide valid Python syntax for accessing.

For example:

{‘123′:’valid’} #is a valid dictionary

but

mydict.123 #is not valid Python syntax.

Attempting to create a key that cannot be accessed through an attribute name
will raise an AttributeError Exception.

This module has not been built for optmization. Some of the method
documentation has been taken from Python’s dict documentation. For more info
on a particular method, refer to that. I’ve tried to mirror the effects of
the standard dict as closely as possible.

Snakes in an Office

I’ve mentioned before that my job (whose contract is almost up) doesn’t require programming. But that doesn’t mean that programming is not useful. When I first got my laptop, I asked the tech guy about the process of installing new software. It went something like this:

  1. Write a formal request for the software you would like installed and submit it to the tech support department.
  2. Wait for review and approval.
  3. Wait for them to install it.

A pretty standard (and painful) process in large organizations, unfortunately. I asked him, “So if I want to install Python I just submit one of these requests?”

Insead of answering my question, he asked me one in return. “What would you want to install Python for?!” I think he actually spat and curled his lips at the thought.

“I don’t know… writing basic scripts.” I really didn’t know what at the time, but I knew it would be useful. And I wanted to use it.

He proceeded to look at me as if I had suggested bathing in tomato sauce as a remedy for headaches.

So I was under the impression that I couldn’t install new software and didn’t want to bother going through all that formal cock holding; I went without my Firefox, my Python. I’ve since discovered that I can install things. (Sorta.)

Anyway, I was using Python at work today, and I just love it. Previously, I had been fumbling around with VBA in Excel and Word. I had a button set up in Word that would run a macro/VBA to export the form’s contents to a CSV file in a particular folder. I could open a Word document anywhere, press the export button, and the CSV would be created/overwritten in that folder. Then I had a button set up in Excel to run a macro that would import all CSV files in that folder as rows in the spreadsheet. (This was actually really useful, and if you have to deal with a lot of similar tasks in Office, use VBA and macros.) There was an inconsistency in the number of rows I had in my spreadsheet and the number of docs I had… so I wanted to compare the exported CSVs with the available DOCs.

Enter, the Python interpreter (great for little tasks and as a substitute shell on windows).

The word documents were in a bunch of different folders (for various reasons) but once I had a variable, originals, containing a list of all the documents, and a variable, exported, containing a list of all the CSV, files, I was rolling.


>>> len(originals), len(exported)
(198,200) # Hmmmm, looks like a job for sets!
>>> s1,s2 = (set(originals), set(exported))
>>> len(s1.difference(s2))
398
>>> # What the...
...
>>> originals
['doc1.doc', 'doc2.doc', ...., 'doc200.doc']
>>> exported
['doc1.txt', 'doc2.txt', ..., 'doc200.txt']
>>> # Oh yeah, different extensions. Duh...
... # Bus leaves SOON!
... # Need to quickly strip the extensions and rebuild the sets....
...
>>> s1,s2 = ( set([s[:-4] for s in s1]), set([s[:-4] for s in s2]) )
>>> s1
['doc1', 'doc2', ... , 'doc200']
>>> # Cool
...
>>> s1.difference(s2)
(['doc38', 'doc72'])

AHA!! Caught my bus. Python is great. :D

How to open a folder with the default file-manager in mono/C#

using System.Diagnostics;

Process.Start (“file:///home/”);

Did a bit of IRC channel ping-pong today. Went over to #f-spot to ask a user-related question about f-spot, and got pulled in by curiosity on a totally unrelated topic: opening a folder with the default file-manager. Initiated some discussion on #mono and discovered this was, in fact, not trivial – which seemed odd. But I’ve never personally written any C#, or much desktop code at all, for that matter. So I started playing around with various suggestions from the good folks in #mono, and writing my very first C# application.

(Yeah, basically a one-liner. You gotta start somewhere! ^_^) Anyway, for something so simple, there seemed to be a lot of uncertainty and discussion about it (even from the man, himself! – He claims his memory is fading…), so I figure it’s worth documenting. More info can be seen here under Process.

On FreeDesktop systems it will use xdg-open, if not, it will try to use gnome-open or kfmclient to open the files.

Not sure if this will work on Windows. The file:// prefix is required.

Update: According to ccoish, this will work on Windows.

Passing this info back to #f-spot resulted in a patch to allow you to open the folder containing your photo. Thanks, Gabriel! Open source is cool. :)

Not sure if I’ll look at C#/Mono much more, was kinda gonna do the Python thing for a while… but this was a fun distraction. :)