Saturday, August 22, 2009

Python And Eclipse

When I started learning Python, I used it's IDLE. That was a bit annoying, comparing with Java IDEs, such as NetBeans, or Eclipse (which I used them regularly that time). However, it was a good way to master coding with Python.

Python interpreter could be used to test smaller codes, but it is not a feasible way to write fast code for huge projects and systems. So Eclipse is probably the best open source IDE in the market for creating and managing Python programs.

Pydev is a plugin that enables users to use Eclipse for Python and Jython development -- making Eclipse a first class Python IDE. It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others.

Pydev Extensions is a plugin that works with Pydev, providing many other features such as code-analysis, code completion with auto-import, a much better implementation of go to definition, remote debugging, 'quick-fixes', mark-occurrences, quick outline, browse globals, among others.


Python's Lambda

Python's lambda allows you to declare a one-line nameless minifunction on the fly. The format is:

lambda parameter(s): expression using the parameter(s)

It returns the result of the expression. The expression has to be a one-liner (no newlines)! Here is a little example ...

>>> def f(x):
... return x*2
>>> f(3)
>>> g = lambda x: x*2
>>> g(3)
>>> (lambda x: x*2)(3)

This is a lambda function that accomplishes the same thing as the normal function above it. Note the abbreviated syntax here: there are no parentheses around the argument list, and the return keyword is missing (it is implied, since the entire function can only be one expression). Also, the function has no name, but it can be called through the variable it is assigned to.

You can use a lambda function without even assigning it to a variable. Not the most useful thing in the world, but it just goes to show that a lambda is just an in-line function.

To generalize, a lambda function is a function that takes any number of arguments (including optional arguments) and returns the value of a single expression. lambda functions can not contain commands, and they can not contain more than one expression. Don’t try to squeeze too much into a lambda function; if you need something more complex, define a normal function instead and make it as long as you want.

lambda functions are a matter of style. Using them is never required. It could be used in places where you want to encapsulate specific, non-reusable code without littering your code with a lot of little one-line functions.


Tuesday, August 11, 2009

Python cPickle

Your Basic Pickle
Serialization, also called pickling or flattening, converts structured data into a data stream format. Essentially, this means that structures such as lists, tuples, functions, and classes are preserved using ASCII characters between data values. The pickle data format is standardized, so strings serialized with pickle can be deserialized with cPickle and vice versa.

The main difference between cPickle and pickle is performance. The cPickle module is many times faster to execute because it's written in C and because its methods are functions instead of classes. While this improves performance, it also means that the cPickle methods can't be extended or customized, whereas pickle classes can.

Serving Up Condiments: A cPickle Example
First, the script informs Python that we'll be using the cPickle module:
import cPickle

Next, I define the object I want to serialise and store. In this case, it's a list of condiments I've got in the fridge:
inFridge = ["ketchup", "mustard", "relish"]
print inFridge

That print statement's output will display the following:
['ketchup', 'mustard', 'relish']

I want to save my results in a file called fridge.txt, so the script creates a file handler and opens the file for writing:
FILE = open("fridge.txt", 'w')

Now the magic happens. I call the cPickle command, dump, to pickle my data and dump the results to the file:
cPickle.dump(inFridge, FILE)

I'm finished for now, so the script closes the file:

I now have a file, fridge.txt, that contains the following:

The pickle and cPickle modules have an option to save the information in a binary format; however, I've used the default ASCII because it is human-readable.

Now I've looked in my kitchen and realised I also have pickles. I can add them to my list and reserialize it, and cPickle will remember what's contained there without duplicating the information:

print inFridge

The output of my print command now displays:
['ketchup', 'mustard', 'relish', 'pickles']

That looks right, so I have my script repickle the inFridge list and add it to the file:
FILE = open("fridge.txt", 'w')
cPickle.dump(inFridge, FILE)

To get my information out of the file and back into a useable list, I simply open the file for reading and use the cPickle.load command to unpickle it. For the purposes of demonstration, I've used a new variable, inFridgeFile, to store the results:
FILE = open("fridge.txt", 'r')
inFridgeFile = cPickle.load(FILE)
print inFridgeFile

The output of the print command displays:
['ketchup', 'mustard', 'relish', 'pickles']

When I repickled my list, cPickle recognized my original contents and didn't duplicate them. The inFridgeFile variable contains my information restored to its original list format.

To Put A Lid On It
You have various options for serializing your data in Python, including pickle and cPickle. My cPickle example showed that this implementation is truly handy, especially since these commands will preserve your original object and allow modifications to be made even after it has been processed. This functionality will keep you from being stuck in a pickle next time you're saving or transmitting objects.