Harsh J

Memoirs of a QWERTY Keyboard

Archive for the ‘code’ tag

Scratching my itch

4 comments

If you like open source software, you end up doing that someday sooner or later – you scratch your itch and make a contribution. That’s how it rolls. Well unless you’re sponsored to do so, of course.

I did the same for KDE, having had an on/off relationship with it since 3.x, and finally settling onto 4.3, I managed to get into writing code for it. Although not a fan of the entire desktop, it is what I use on a daily basis and I do feel the lack of a few things sometimes. There’s already too much to customize; and am sure I don’t know the half of it yet.

I read a lot of comic books in my free time, on the PC. While I do have my collections named and arranged neatly, it has always been hard finding a particular file since there were no previews of its covers on KDE 4. Since Okular, KDE’s magnificent all-in-one document reader, reads the files (.cbr, .cbz type) why not also preview it. That became my itch, my want. And I scratched it with copious amounts of help provided by its development community. However, I’d be glad if some artist came along and gave the format an Oxygen-style icon as well – since it still lacks one.

In KDE 4.4, you will have comic book previews which would show you the comic book covers in its file manager’s preview mode. This should make your life easier. However, for .cbr thumbnails to work, you’d need the non-free version of unrar cause the free ones don’t do version 3+ files well. It isn’t a hard dependency, and .cbz ZIP files would work just fine without unrar. I’d also written support for .cbt, but it’d have to wait until KDE 4.5 cause of their ‘feature freeze’.

Since I made it this far, I also fixed certain minor annoyances – some reported by other people as well. A small list:

I’ll be more than glad to hammer more bugs, once the feature freeze melts. Go here to read about what’s new in KDE 4.4.

Written by Harsh

December 8th, 2009 at 12:04 am

Sorting entries in a QStringList Case-insensitively

4 comments

This post is simply a snippet-post for the users of Nokia’s Qt C++ cross-platform toolkit.

While writing some C++ code (after 2 long years since I last wrote them for academic reasons), I had this simple issue of sorting a QStringList in a case-insensitive manner. Normally, there exists a QStringList::sort() function that does the sorting of the strings stored in it in a case-sensitive manner, and is very fast at it. But Qt does not provide a way to perform the sort in a non case-sensitive manner, although it has hints on how to in the class’ documentation.

Being mostly a PyQt/PySide user who uses inbuilt Python lists to do all list-work, here’s how its apparently done in Qt/C++, using a QtCore class called QMap:

#include <QtCore/QStringList>
#include <QtCore/QMap>

void sortNonCaseSensitive( QStringList &sList ) {
    ///  Sorts the passed sList non-case-sensitively.
    ///  (Preserves the cases! Just doesn't use them
    ///  while sorting.)
    QMap<QString, QString> strMap;
    foreach ( QString str, sList ) {
        strMap.insert( str.toLower(), str );
    }
    sList = strMap.values();
}

That’s it.

Written by Harsh

October 24th, 2009 at 10:54 pm

Color Hot-Tracking in Smooth Tasks Plasmoid

3 comments

Polishing your K Desktop never seems to stop. First its the Desktop appearances, the plethora of widgets available to choose and use from for the desktop and the taskbar, the hilighting schemes in Kate/KWrite, it keeps going on. Maybe a bad thing – you never settle.

I was trying out the kde-extragear-plasmoids AUR package yesterday on my ArchLinux’s plain KDE installation and I came across this wonderful plasmoid known as Smooth Tasks. While nothing innovative in itself, its a simple plasmoid that apes the Windows 7 taskbar. Provides icon views of the applications running and allows peeking into them when hovered upon, and if grouped – lets you switch using the previews. I’ll leave the screenshots to do the rest of explanation.

What I liked most about Windows 7 is its ability to color the hover-glow on the icons in the taskbar based on the average computed color of the icon itself. This feature, as explained by Long Zheng, “delivers some sentimental value by making it easy to identify applications by color.” I completely agree with that point. However, Smooth Tasks missed this feature, and the built in light feature didn’t move entirely with the mouse pointer as well.

I cloned the code today to add, at least an initial working version, the color hot-tracking to Smooth Tasks and it was done by the afternoon. I’ve pushed the changes to the Smooth Tasks fork over at Bitbucket (which is a great site, by the way) and the image below describes how the initial work looks like. Notice the soft color glow. Here are some more pictures, with other icons.

Color Hot-tracking in Smooth Tasks on KDE

Color Hot-tracking in Smooth Tasks on KDE

Now all I’ve to figure out is a way to enhance the glow or another component of the effect to give it a more polished look. Windows 7 also colors the border of the taskbar item with the average color but that’s not possible with the way the KDE’s glow around items work, as far as I know. Please let me know if am wrong.

You need to a flashplayer enabled browser to view this YouTube video

Written by Harsh

October 10th, 2009 at 1:25 pm

Getting the latest Django Documentation to run offline

4 comments

Yes, really.

Yes, really.

Ok so you have started with Django, the Python web-framework and are finding yourself wasting a lot of precious little seconds on their regularly updated documentation online?

(The docs folder in your downloaded django tarball is just not so up to date!)

Well then this is the guide for you. It will explain in neat little steps on how to go about obtaining the freshest of all django documentation online (From their code repository) and build it for use on your local machine or web-server.

First off, here are the requirements you will need installed before you begin:

  1. SVN (Subversion)
  2. Sphinx (Python)
  3. make, etc… build tools

For example, on a Debian/Ubuntu system you would run:

harsh@work:~$ sudo apt-get install subversion python-sphinx make

Fetch the documentation

Once you have got them all installed, lets start the process by fetching the documentation files from Django’s SVN repository:

harsh@work:~$ mkdir django && cd django

harsh@work:~/django$ svn co http://code.djangoproject.com/svn/django/trunk/docs

After the above command finishes downloading the documentation files (It’s checking-out source code from the repository), you should have a folder named docs under the current working folder (~/django in my example’s case).

Build the HTML documentation

Our next task is to build these reStructuredText (reST) files into HTML using Sphinx. We issue the following command to do so:

harsh@work:~/django$ cd docs

harsh@work:~/django/docs$ make html

Very simple! All the built files would go to the ~/django/docs/_build folder if you’re following my example. This folder would contain two folders named doctree and html. The html one is our sweet nectar of immortality!

Note: There are other make [options] that you could probably use, such as pickle, latex, etc. Also, the file conf.py under the documentation root might be of interest to people who would like to customize things a little more.

Done!

Now that we have our files ready, we can move it to a preferred place. For me it was /var/www (My local web-server root). So all I then had to do was:

harsh@home-desktop:~/django/docs$ mv _build/html /var/www/django-doc

That’s it. Now you can just browse to this folder either directly (Files) or by putting it on your local development server. Enjoy!

P.s. With some more configuration, you can automate this process and even make it update weekly or so for you. So add all the salt you would like for taste, and happy hacking!

Written by Harsh

December 19th, 2008 at 12:52 am

The new print function in Python 3

5 comments

The print statement has gone. A function replaces it in Python 3.0. Apart from the other changes, this one probably needs the most mind and finger retraining, cause its something I use a lot while fiddling with the interpreter.

So far, till Python 2.6

So this is how you all have been happily typing away in Python 2.x:

print "Print this and print a newline"
print "Print this, but not a newline",

Now, if you had a collection of elements you wanted to print with certain formatting, say with punctuation, here’s how you would do it:

basket = ('Apple', 'Oranges', 'Banana')
print ", ".join(basket) + "."

# Which produces the output:
"Apple, Oranges, Banana."
About the print() function in Python 3.0

About the print() function in Python 3.0

Some would even prefer to use a logical loop instead, for readability’s sake. But this method does require a string operation to happen, and loops probably would take up lines, or worse, in certain cases.

Now, in Python 3.0 onwards

The print statement has gone for good and the print() function comes in.

This decision is well explained in the PEP (Python Enhancement Proposals) and the specific paper on it can be found here (Numbered 3105)

Digging into this function’s documentation would reveal all about it in simple text (If you are an avid reader of the Python Documentation). Of what’s continued here is all just for newbies-only, with some demonstrations.

A simple demo equivalent to the first as above:

print ("Print this line, and print a newline")
print ("Print this line, but not a newline", end="")

You might notice that its got a little complex here, with a keyword argument being supplied instead of an ending comma as before.

But lets see the full power of this new print() function by doing the same punctuation to the fruit basket as before:

basket = ('Apple', 'Oranges', 'Banana')

print (*basket, sep=", ", end=".\n")

# Which produces the same output as desired:

"Apple, Oranges, Banana."

We do the same thing as before, except that we don’t require a loop, nor string operations. The function’s two keyword arguments sep and end handle the complex jobs for us. Basically, this is what they mean:

  • sep – Seperator string – Defines the string that is to be placed between every two values printed.
  • end – Ender string – Defines the string to be printed at the end of the print function.

By default, sep has a space (‘ ‘) and end is a newline (‘ \n ‘). So a simple signature of this new print function would be like:

print ( [object(s)], sep=' ', end='\n' )

And finally, the Star of this show

The one last addition to the print function made by the Python team was the file keyword argument. That’s right, one more keyword argument.

  • file – Object Name – Specify file/object to print to.

This one is a really cool addition, and it defaults to sys.stdout, (i.e.) your terminal. Thus, a more complete print() signature is:

print ( [object(s)], sep=' ', end='\n' , file=sys.stdout)
# No, you obviously don't have to import sys for this.
# Its just to describe what file its printing to.
# (/dev/stdout in UNIX's case)

All the file argument needs is an object that supports any write(string) method.

Now lets try printing the fruit basket to a file than to the terminal as default:

fruits = open("fruitsfile.txt", "w")
basket = ('Apple', 'Oranges', 'Banana')
print (*basket, sep=", ", end=".\n", file=fruits)

# Effectively prints the punctuated line,
# to the file named fruitsfile.txt

This could have a great use, especially with logging! And since it supports any object with a write method, the possibilities could be endless.

Now tell me if you still hate that its got parentheses? Look at the power of this new function!

Written by Harsh

December 9th, 2008 at 11:06 pm