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])