If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Friday, August 15, 2008

flatten


#!/usr/bin/env python

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

def flatten(items):
'''Flatten a nested list.

>>> a = [[1], 2, [[[3]], 4]]
>>> list(flatten(a))
[1, 2, 3, 4]
>>>
'''
for item in items:
if getattr(item, "__iter__", None):
for subitem in flatten(item):
yield subitem
else:
yield item

if __name__ == "__main__":
from doctest import testmod
testmod()

pipe

Thursday, August 07, 2008

printobj


'''Quick and dirty object "repr"'''

__author__ = "Miki Tebeka "
# FIXME: Find how to make doctest play with "regular" class definition

def printobj(obj):
'''
Quick and dirty object "repr"

>>> class Point: pass
>>> p = Point()
>>> p.x, p.y = 1, 2
>>> printobj(p)
('y', 2)
('x', 1)
>>>
'''
print "\n".join(map(str, obj.__dict__.items()))

if __name__ == "__main__":
from doctest import testmod
testmod()

Blog Archive