Sunday 22 February 2009

Triangle Test in Python

def in_triangle(p,a,b,c):
# pre: a,b,c must be in clockwise order
# post: true if p is in the triangle abc
return right_of(p,a,b) and right_of(p,b,c) and right_of(p,c,a)

Saturday 7 February 2009

Enumerated Types in Python

How do we do enumerated types work in Python? Well, there is no enum keyword as such but there are many ways to get the same effect.

red,green,blue=range(3)

You can also isolate your enums in a class. e.g.

class Colors:
red,green,blue=range(3)

Colors.red yields 0 and dir(Colors) yields meaningful results.
Useful for programming a finite state machine.

Friday 6 February 2009

Parsing Libraries in Python: htmllib and sgmllib

Various structure markup processing tools are available in Python. Amongst these are htmllib and sgmllib. There's also an xmllib.

Wednesday 4 February 2009

How Multiple Inheritance Works in Python

class Derived(Base1, Base2, Base3):
statement1
statement2

The resolution rule for class attribute references is as follows (depth-first left to right):
  1. if attribute not in Derived, check Base1. If not in Base1, recursively check base classes of Base1.
  2. if not in Base1, check Base2. und so weiter, und so weiter.

Mixin classes are classes designed to be combined with other classes via multiple inheritance. The Tkinter module uses mixins extensively (e.g. the Misc class used as a mixin by the root window and widget classes). Misc provides a large number of Tk and window related services.

Tuesday 3 February 2009