Tuesday, July 21, 2009

DEXUTILS: A python utility library I wish I had in EVERY language

I have been working with python for 2 years now. It is by far the most general purpose language and productive programming language I have experienced to date.

The first thing that any professional programmer should get for python is an AWESOME IDE. I HIGHLY recommend wingware's Python IDE. It truly is worth the money and comes for all flavors of operating systems. For those using windows and wanting something free software I recommend using mmm-experts python scripters. Both have an amazing feature of clicking (while holding the ctrl key) to navigate to a function. So I basically have a folder put in the Environment variable PYTHONPATH and in that I place a folder by the name dex. Now in my code I do import dex and I can type function name. If any is not working I can modify it there and then in my script.

I do a lot of general purpose programming and over the time dex (aka dexutils ) has expanded exponentially. So without further ado I present you dexutils.

It comes with a simple distutils installer (for windows users an installer is also always present

For quick programming hacks I dont want to waste time opening / reading and then closing a file. So I made a quick function about it:

import dex
lines = dex.readlines('filename')

For saving a lines to a file you can do :

import dex
lines = ['1','2','3','4','5']
dex.writelines('temp.txt',lines)


This makes using python faster than writing a macro in vba anyday!
There a LOTS of functions in it. Some quite advanced. Almost all of them come with __doc__ strings so if you are using wingware or pyscripter you get cool help information.
Here a a couple more:
Check if a string can be made an integer:
from dex import stringutils
print stringutils.IsInt('123')

Splitting a CSV string: (will print ['1', '2', '3', 'stuff', '5'])
from dex import stringutils
print stringutils.SplitCSV('1,2,3,"stuff",5')

Print the current scripts filename :

from dex import stringutils
print stringutils.GetFilename(__file__)

How about reversing a string : '123' -> '321'
from dex import stringutils
print stringutils.ReverseStr('123')

There are various modules e.g stringutils , fileutils, arrutils. Just read the docs :) I am sure you will find it helpful. Also any cool library I found highly useful I have made a part of the code. e.g. xlrd (more on that later).

The link (again) : dexutils

Enjoy!