Archive for the ‘python’ Category

python: insert newline with regular expression (sub | substitute)

Monday, July 7th, 2008

Python allows to use regular expressions on multiline strings.
You can use re.MULTILINE flag to make ‘^’ and ‘$’ match resp. the beginning and end of each line.

The problem is how to insert newline character doing substitutions ?
Usually, substitution pattern is a raw string
ex: print re.sub(‘mytext’, ‘t(.)x’, r’T\1X’) # displays ‘myTeXt’
Here we use a raw string for substitution pattern (string prefixed with ‘r’ char), otherwise we would need to double the escape character (r’T\1X’ => ‘T\\1X’)
Problem with raw strings is that content is not interpreted, particularly ‘\n’ is not interpreted and is considered being two characters. => to insert newlines, we simply need NOT to use raw string in replacement pattern.

Let’s continue with our example, let’s say we want to insert a new line before captured group (letter ‘e’ in our case):

  • print re.sub(‘mytext’, ‘t(.)x’, r’\n\1′) # displays ‘myt\nxt’ since we’re using raw string for replacement pattern
  • print re.sub(‘mytext’, ‘t(.)x’, ‘\n\\1′) # displays ‘myt’ <newline> ‘xt’

[sources]

  • http://docs.python.org/lib/module-re.html
  • http://www.amk.ca/python/howto/regex/regex.html read part 2.2 on raw strings

python decorators: thank god! (advanced class features in python)

Sunday, July 6th, 2008

[sources]

  • http://wiki.python.org/moin/PythonDecorators exhaustive python decorators coverage
  • http://wiki.python.org/moin/PythonDecoratorLibrary python decorator library
  • http://www.python.org/dev/peps/pep-0318/
  • http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators wikipedia quick explanation of python decorators

python: easter eggs — developpers’ jokes

Sunday, July 6th, 2008

using culy braces instead of forced indentation as been requested for long to python developing team, run ‘from __future__ import braces’ to see what they think about it.

Similarly, if you want to know python’s phylosphy, run “import this”, which prints:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

python: ‘with’ statement

Sunday, July 6th, 2008

With python2.5 comes ‘with’ statement.

To use it, first you need to import it: ‘from __future__ import with_statement’

It’s usage is pretty simple:
with “expr” as “myname”:
do something

where:
- “myname” is a variable name in which the result of “expr”.__enter__() will be stored
- “expr” must be a context expression, i.e. an object which implements both __enter__() and __exit__() method

‘With’ statement has primarily being developped to reduce overhead of length try-except-else-finally statements.
If an exception is raised within the ‘with’ loop, then __exit__() method is called with corresponding (type, msg, traceback) objects.
=> instead of repeatedly writing the same try-except… code, simply wrap it into a class (more precisely into __exit__() method of this class) and use the with statement. If __exit__() return True, then exception is considered handled.

[Example reading a file content]

since python2.5, file objects implement such methods and therefore can be used with a ‘with’ statement:
ex from http://python.about.com/od/gettingstarted/qt/py25WITH.htm :

[whithout 'with']

fileIN = open(sys.argv[1], “r”)
line = fileIN.readline()
while line:
[some bit of analysis here]
line = fileIN.readline()

[with 'with']

from __future__ import with_statement
with open(sys.argv[1], “r”) as fileIN:
for line in fileIN:
[some bit of analysis here]

[sources]

  • http://docs.python.org/ref/with.html
  • http://effbot.org/zone/python-with-statement.htm describe usage of ‘with’ regarding exceptions management

python: ‘for’ or ‘while’ loop with ‘else’ statement (‘try’ statement too)

Sunday, July 6th, 2008

in python, ‘for’ and ‘while’ statements have an optional ‘else’ clause which gets executed when the testing condition becomes false.

The ‘break’ statement will get you out of the loop without executing ‘else’ clause

‘Try’ statement can have an optional ‘else’ clause too which gets executed when no exception was raise.
You cannot have an ‘else’ statement if your ‘try’ block has no ‘except’ statement.

[source]

  • http://docs.python.org/ref/while.html
  • http://docs.python.org/ref/for.html
  • http://docs.python.org/ref/try.html
  • http://docs.python.org/ref/break.html explain ‘break’ rule regarding ‘else’ statement

python ‘return’ statement within try … finally

Sunday, July 6th, 2008

When exectuting ‘return’ in code defined within a try,catch,finally statement, it’s good to know that ‘finally’ code gets executed prior to returning the value
=> indeed, finally really gets executed whatever happens (i.e. an exception being generated or not)

[source]

http://docs.python.org/ref/return.html

python “exec” statement

Sunday, July 6th, 2008

“exec” statement is pretty interesting in python since you can pass dictonaries to be used as global and local namespaces respectively

[source]

http://docs.python.org/ref/exec.html

unexpected static class properties

Friday, July 4th, 2008

Python does not have any “static” keyword to use as it is in php or java. Thing is you can create static class properties and might not even know it, leading to tricky bugs to spot!! If you have any doubt, call ‘id()’ builtin function which return the memory address of passed variable. If you define a class such as:

class myclass:
  prop1 = []
  prop2 = ''
  def __init__(self):
    self.prop3 = []

Well, in the above class, prop1 is static while prop2 and prop3 are not! Remember that lists, dictionaries and objects are always passed by reference in python while int, str, bool, None are not. Simply check by yourself that prop1 is static while prop2 and prop3 are  not:

a = myclass()
b = myclass()
print b.prop1 # output: []
a.prop1.append('x')
print a.prop1 # output: ['x']
print b.prop1 # output: ['x'] => static since a.prop1 modified b.prop1
a. prop2 = 'y'
print a.prop2 # output: 'y'
print b.prop2 # output: '' => non static since a.prop2 != b.prop2
a.prop3.append('z')
print a.prop3 # output: ['z']
print b.prop3 # output: [] => non static since a.prop3 != b.prop3
# just to clarify everything, use id() function after having affected values
# to properties, this function return memory address of passed variable
print id(a.prop1) == id(b.prop1) # output: True => static
print id(a.prop2) == id(b.prop2) # output: False => non-static
print id(a.prop3) == id(b.prop3) # output: False => non-static

Conclusion: To play safe, stick to the following rule:

  • declare non static properties into __init__() method
  • declare static properties at module level and not at class level (otherwise you will have str|int|bool|None properties being non-static while list|dict|objects would be static => nightmare to debug)

It took me more than one day to find this out, I hope it will save some people time. Cheers [Update - Follow-up] If you want, you can declare all your non-static variable at class level (outside __init__()), simply do not initialize them and set them to None. By doing so, each instance will have its own property (it’s the initialization when declaring properties at class level that make some static (those declared as list|dict|objetc through “prop1 = []” for inst.) and other non-static (those declared as int|str|bool|None)

memory address in python

Friday, July 4th, 2008

to print the memory address of a given var, call builtin ‘id()’ function: id(myvar)

This is pretty useful for debugging since python pass list and dictionaries by reference