Linux, programming, computers and life

November 8, 2006

Things i miss in python

Filed under: programming, c++, python

Coming from a C++ programmer:

  • I’m not the first to mention it, i know - missing of begin/end of block. If i copy-paste some code i need to think again how to align it - o/w it’ll not work! And there’s no automatic tool to align the code for me
  • Compile time checks:
  • const checks
  • non-declared variables check. if i write obj.xxx = 12 and the real member variable name is Xxx it takes ages to find the problem :(

Technorati Tags: , ,

November 1, 2006

Memoization in python

Filed under: programming, c++, python

I’m writing a genetic algorithm, which is pretty time consuming. I was thinking - why not use memoization and save some time. It appears pretty easy to do and there’re several examples for copy-paste in the internet, but it appeared that’s it not as simple as i thought it would be.
I’m trying to memoize (i’ll use cache from now) objects of a certain class. The class has some double precision data members, which i want to allow to be a little different, for example abs(self.a - rhs.a) < 0.1. It’s easy, just define __eq__ method. Now, let’s put objects into a dictionary (for caching)… oops - there’s an exception thrown - they are unhashable now.
Fast search in google revealed i need to define __hash__ method which must return equal results for equal objects…. Now it becomes non trivial at all.
In c++ it’s easy since std::map (stl equivalent of python’s dictonary) is based on operator< (or on comparison functor with the same functionality), and in python dictionary is based on hash value, which is faster but not easy calculable in my problem.
Is there something like std::map ready? Currently the solution was to round floating points members and cache them. Any better solutions?

Technorati Tags: Technorati Tags , , ,

September 7, 2006

Things to remember about python

Filed under: programming, python

My main programming language for a long time (more than 5 years) is C++. I still use it at work every day. Now, when i started to use python for several small (non work-related) projects, there’re several things which made me rewrite some code when i discovered them, since they are done differently in python and C++. In other words, some tips on python for C/C++ programmers and a reminder for me :)

  • when you need to know both element (in sequence) and it’s index use enumerate

          for index, element in enumerate([’a', ‘b’, ‘c’]): print ‘i: %d e: %s’ % (index, element)

  • when you need to iterate more than one sequence in parallel use zip

          for e, o in zip([2,4,6,8], (1,3,5,7)): print e, ‘ :: ‘, o

  • use xrange instead of range for performance
  • iterate sequences as much as possible instead of using elements index ([] operator). This way if you change your list to generator expression, it’ll still work
  • use in operator to verify whether something is in a sequence, i.e.:

          if 3 in [1, 2, 3]: print ‘OK’

Technorati Tags: , , ,

August 24, 2006

Some python benchmarking

Filed under: programming, python

I had to develop on a very old laptop - Pentium I, 64MB of RAM and Windows 98. Python was my language of choice, the version was 2.4.something, probably not the latest. (I have no possibility to check the exact version now, i’ll update it as soon as i can).
Since i was working on such a slow device, i had to optimize as much as i could. I tried profile and pstats modules but i got to several small functions which i had to improve. Because I’m new in python, i did some benchmarks on several things, which might be obvious for some, but were not obvious for a python newbie (me). I’d really like to know whether the results are as expected.
Here they are, each code section has comment of a time it took:

import time

 
# x**2 vs x*x:
# 9.5 sec:

start = time.time()

for j in range(500000):

    t = [j, j]

    z = (t[0] - 1)*(t[1] - 1)

    if j == 0: print z

print time.time() - start 

 
# 20.2 sec:


start = time.time()

for j in range(500000):

    t = [j, j]

    z = (t[0] - 1)**2

    if j == 0: print z

print time.time() - start 

 
elmLst = []

for i in range(100):

    elmLst.append([0,1,2,3,4,5,6,7,8,9,0])

 
# reduce() vs loop:
# 9 sec:

start = time.time()

for j in range(1000):

    z = reduce(lambda p1,p2: (i1 + i2 for i1, i2 in zip(p1, p2)), elmLst)

    if j == 0: print z

print time.time() - start 

 
# 11 sec:

start = time.time()

for j in range(1000):

    z = None

    for e in elmLst:

        if z == None: z = e

        else:

            z = [e1+e1 for e1, e2 in zip(z, e)]


    if j == 0: print z

print time.time() - start 

 
e1 = range(100)

e2 = range(100)

 
# […] vs map()
# 7 secs:

start = time.time()

for j in range(10000):

    z = [e[0]*e[1] for e in zip(e1, e2)]

    if j == 0: print z

print time.time() - start 

 
# 15 secs:

nowstart = time.time()

for j in range(10000):

    z = map(lambda e: e[0]*e[1], zip(e1, e2))

    if j == 0: print z

print time.time() - start 

Technorati Tags: , ,

August 2, 2006

Python editor

Filed under: programming, linux, python

My quest for free python IDE or editor continues. There’s also eclipse which i forgot to mention and has a plugin for python development. Here finally decent code completion is available. The only problem, it’s to heavy for me - i’d like to see lighter alternative. It’s also a little unintuitive, speaking from a person who worked with Visual Studio six years ago, emacs for 5 last years and currently working with (proprietary) SlickEdit. However i managed to do almost evertthing i wanted to.
Suprisingly, i’m on emacs now. Emacs 22 has python-mode which has decent keybindings for working with integrated python interpreter. It has no autocompletion, but… what it has is enough for now.
The quest continues…

Technorati Tags: , , , ,

July 21, 2006

Python IDE/Editor

Filed under: programming, linux, python

I’m currently looking for Python editor or IDE. I need to write a project for my studies and i decided i’d learn python during this project. So… i’m looking for a good python editor.
Here’s features i’d like to see:

  • free
  • code completion (that’s where emacs falls behind)
  • word completion (like Alt-/ in emacs)
  • multiple open files
  • colors customization
  • indentaion which will be ok by default most of the time (important thing in python)
  • it should work in linux ;)

I tried:
  • eric3 - i don’t like the code completion there
  • spe - unable to run it on my ArchLinux
  • mooedit - somehow word completion and code completion does not work for me
  • PyPe - somehow and code completion does not work for me
  • emacs - it has everything but code completion
  • vim - i don’t know too much this wonderful editor. Does it have it all?
Update 1
There’s also Pida & geany - 1st is vim based and second lacks auto complete

Update 2
I’m currently working on eric3.

What other stuff am i missing?

Technorati Tags: , , ,

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers