Tuesday, March 22, 2011

Using javascript for data visualization and animations.

Visit neurosoftware.ro.


We are still learning how to run jsxscript in our Wordpress blog.

Friday, March 4, 2011

Adding commas to integer strings for readability

There ase many ways to make more human readable the string representation of an integer.
Click on Activestate.com thousands separator

As an example, 1234567890 looks more humanly readable as 1,234,567,890.

We normally use other people's tested and understood code to save time, but this is one good programming problem.

The manual way to do this is to insert commas before blocks of contiguous three digits.
Strings are supposed to be immutable in Python by design for simplicity. Every changes to a string must be done by a copy. So we convert a string to a list just so to insert characters!


def  withcommas(s):
     p = len(s) -3
     s = list(s)
     while p > 0:
        s.insert(p, ",")
        p -= 3
     return "".join(s)



But suppose that s starts with a '+" or "-" sign? Then the leftmost position for a comma is at the second character or index 1. We add a conditional test so that the code will not be put off by the leading sign.

def  withcommas(s):
     p = len(s) -3
     s = list(s)
     if s.startswith("+") or s.startswith("-"):
        firstpos = 1
     while p > firstpos:
        s.insert(p, ",")
        p -= 3
     return "".join(s)

and finally we include a trivial programming problem for the reader. Instead of 3, let the caller of withcommas() specify a blocklength to the function. We are just getting ready for the time when we will concentrate on creating mathematical tables.

Our aim is to create simple, working first prototypes. The reader again is encouraged to read and search the cited reference above for faster solutions.

Thursday, March 3, 2011

Pickled objects in Python

The inventor of Python has given a catchy name to saving objects in files as "pickled objects".
I often use the cPickle module and one can get by using only two powerful functions load and dump in applications.

1. Save a named object to a pickle file.

import cPickle
a = "The quick brown fox"
b = "Hello world."
c = 123

D = { "a": a, "b": b}

Note that we create a dictionary containing the assigned names of the objects, and their corresponding values. An object is stored in external file and it is possible to recover their contents (seconds, minutes, hours, days, even years later)! Now here is the code for storing in the pickfile.

pickfilename = "tmp.pck"
pickfile = open(pickfilename,"w")

cPickle.dump(D, pickfile)


The cPickle.dump() function expects an object and an open file in write mode. If you dump the contents of tmp.pck you will get something like this displayed on the screen:


The idea of using a dictionary is the most natural way of saving a set of variables you are interested at to save and look for values later. Of course if you only wanted to save the variable a, you can do cPickle.dump(a, pickfile)

The name of all active local variables can be accessed by the function vars() which is a dictionary. But it also includes loaded modules which you may not want to be included!

2. Load saved object in pickle file

Nowt that you have created a pickfile, "tmp.pck", let us take a look at it. Dumping it on the console gives

(dp1
S'a'
S'The quick brown fox'
p2
sS'b'
S'Hello world.'
p3

So at least we are at ease that the values are indeed stored for later access.

myvars = cPickel.load(open(pickilename,"r"))

We knew that the name of the dictionary variable stored was "D". But you dont have to use D.
Instead above, we use a new label, myvars.

Now we can recover the values of the former variables by indexing using 'a' and 'b' as keys.Thus,


a = myvars["a"]
b = myvars["b"]


Notes.

The cPickle functions has an additional parameter calle 'protocol'. At this point in time, we refer the interested reader to the Python
tutorial
on the Pickle and cPickle modules.