Python 3

Sunday, January 11, 2009

Another way to think about lambda

Until now I had problems understanding the lambda statement. I did not know how is it used and what does it actually do. The description in the Python documentation did explain it but not easy enought to remember it.

Now I have thought of something which helps me remember the purpose of lambda and its syntax.

It's the same like functions in the math class:

f(x) = x+1

is the same as:

lambda x: x + 1

Sorting by class attributes

When you need to sort a sequence (e. g. a list) of objects you probably want to sort them by one of their attributes. How to to that?

Let's have this simple class:


class Record:
    def __init__(self):
        self.name = "noname"


now we have three objects of this class:


>>> r = Record()
>>> s = Record()
>>> t = Record()


And we give them some names


>>> r.name = "qwer"
>>> s.name = "asdf"
>>> t.name = "yxcv"


We have them in a list

>>> someList = [r, s, t]

And we want them to be sorted by their attribute name. This can be done by specifying the key argument of the sequence method .sort():


>>> someList.sort(key = lambda x: x.name)


That's all. The objects in the list are sorted by their name attributes.

P.S.:
The lambda x: x.name is a minimal function. For every x it returns x.name

Saturday, May 24, 2008

Regular Expressions Locale and Unicode

Today I had a very hard time to figure out, how do you actually use some options of the Python re module. I was especially interested in using re.LOCALE and re.UNICODE.

I don’t understand why there are no examples of their use in the Python documentation. At last I could google a site where I saw this kind of syntax for re.IGNORECASE (or short: re.I):

re.split(re.compile('th',re.I),'Brillig and the Slithy Toves')

By this, I finally knew that I shall use those options in this way:

rx = re.compile('some regex syntax', re.UNICODE)

or re.LOCALE instead of re.UNICODE.

These small syntax details are written nowhere in the Python documentation and that bothers me.

Wednesday, May 14, 2008

Debug Python 3 with Winpdb 1.3.8 Tychod

Thanks to Nir Aides, the developer of Winpdb I have learned how to debug source code of Python 3 in winpdb. There is some trade-off, though:


  1. You must have both Python 2.5 and Python 3 installed.

  2. When installing, wxPython for Python 2.5 (unicode), make sure you have Python 2.5 in the PATH instead of Python 3.

  3. When installing winpdb, make sure to have PATH set back to Python 3. Install winpdb into the directory of Python 3 (otherwise it won't work). (There may be an error message at the end of installation, just ignore it.)

  4. You must run winpdb with Python 2.5 but rpdb with Python 3



In Windows XP, the last step is done like this:


  1. Change PATH from C:\Python30 to C:\Python25

  2. Start new cmd window (otherwise it won't notice the changed PATH)

  3. Run (c:\Python30\Lib\site-packages>)python winpdb.py

  4. Change PATH back to C:\Python30

  5. Start another cmd window (to make it notice the new PATH)

  6. From the winpdb directory run python rpdb2.py -pwd=<somePassword> -d <filename> <command line arguments for your python script>

  7. in the GUI window of winpdb do File/Attach and write the password you selected for rpdb2.py in the command line

Monday, May 12, 2008

How to clear screen in Python 3.0

In Windows XP, the command for clearing the shell screen is "cls". In Python 3.0 we cally it by:

subprocess.call('cls', shell=True)

Thursday, May 8, 2008

speedtest.py

Ever wondered which solution to a problem is faster? Python has a nice timeit module, which allows you to stop the time for “small code snippets”. But what if you want to test a small chunk of code? Yes, you can do that, too. But I could not figure out from the standard help text.

Desperate, I downloaded some timeit examples with sample codes tested from PyMOTW, and figured out what to do.

First, you write a simple module, i.e. a python file without main script. Write just the functions you want to test.

and then you run

python -m timeit "import <module_name>; <module_name>.<function_name>()


And you should soon get your timing results.

Here is a module speedtest, where I measured whether makeDict() ist faster than makeList().



# speedtest 1.0, Sven Siegmund
# run with "python -m timeit "import speedtest; speedtest.<function_name>()"

def makeDict(value=1023): # 100 loops, best of 3: 2.08 msec per loop
aDict = {}
for i in range(value):
key = chr(97+i%26)
aDict.update({key:i})

def updateList(somekey, somevalue, somelist): # needed in makeList(), not to be tested alone
for listMember in somelist:
if somekey in listMember[0]:
listMember[1] = somevalue
return True
return False

def makeList(value=1023): # 100 loops, best of 3: 5.5 msec per loop
aList = []
for i in range(value):
key = chr(97+i%26)
if not updateList(key, i, aList):
aList.append([key, i])