Monthly Archives: October 2009

dir command in Python

There is a quite useful command in python to introspect a particular object or module.
Pass any object or module to dir function, which is a built-in function. Yes, you can access it from Python interpreter.

>>> a = []
>>> dir(a)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Quite simple, yet powerful.

also, try this interesting stuff, philosophy of python on “this”
import this

Regular Expressions – Python

While searching for python based parser implementations, for my next personal project, visited following websites about regular expressions, thought useful to others also.

1. Python official documentation about RE

2. Dive into Python – (Basic cookbook)

DJango fixtures

One of the important feature from DJango is fixture generation and consumption.

Fixture is nothing but searialized data in file with proper file naming. Well, it by default recognizes some serialization formats. ex: xml, json etc. Usally file names goes like this: MyFixture.json.

DJango tries to learn data format from file extension.
Generating fixtures from DJango is pretty easy dumpdata

Above command results serialized data of all models of your all apps under project. Ofcourse, there are couple of options to fine tune your command to exclude some apps, to mention format of serialization etc. help dumpdata

issue above command to get full details of available options from your DJango project path.