1 # vim: set encoding=utf-8 et sw=4 sts=4 :
6 This module provides some tools to ease working with sequences.
10 r"""Sequence() -> Sequence instance.
12 This is an abstract class to ease the implementation of sequences. You can
13 inherit your objects from this class and implement just a method called
14 as_tuple() which returns a tuple representation of the object.
18 >>> class A(Sequence):
19 >>> def __init__(self):
22 >>> def as_tuple(self):
23 >>> return (self.a, self.b)
30 r"iter(obj) -> iterator object :: Get iterator."
31 for i in self.as_tuple():
35 r"len(obj) -> int :: Get object length."
36 return len(self.as_tuple())
38 def __getitem__(self, i):
39 r"obj[i] -> object :: Get item with the index i."
40 return self.as_tuple()[i]
43 return '%s%r' % (self.__class__.__name__, self.as_tuple())
46 if isinstance(obj, basestring):
48 if hasattr(obj, 'items'):
49 return tuple(obj.items())
50 if hasattr(obj, '__iter__'):
57 if isinstance(i, basestring):
59 if hasattr(i, '__iter__'):
64 if __name__ == '__main__':
71 return (self.a, self.b)
82 for f in (as_tuple, as_table):
92 print f([[1,2,3],[6,7,8]])
96 print f(dict(a=1, b=2))