Mercurial > pylearn
comparison lookup_list.py @ 300:7c5e5356cb11
doctest syntax for lookup_list
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Mon, 09 Jun 2008 13:37:22 -0400 |
parents | cb6b945acf5a |
children | 32c5f87bc54e |
comparison
equal
deleted
inserted
replaced
299:eded3cb54930 | 300:7c5e5356cb11 |
---|---|
5 """ | 5 """ |
6 A LookupList is a sequence whose elements can be named (and unlike | 6 A LookupList is a sequence whose elements can be named (and unlike |
7 a dictionary the order of the elements depends not on their key but | 7 a dictionary the order of the elements depends not on their key but |
8 on the order given by the user through construction) so that | 8 on the order given by the user through construction) so that |
9 following syntactic constructions work as one would expect:: | 9 following syntactic constructions work as one would expect:: |
10 example = LookupList(['x','y','z'],[1,2,3]) | 10 >>> example = LookupList(['x','y','z'],[1,2,3]) |
11 example['x'] = [1, 2, 3] # set or change a field | 11 >>> example['x'] = [1, 2, 3] # set or change a field |
12 print example('z','y') # prints [3,2] | 12 >>> print example('z','y') # prints [3,2] |
13 x, y, z = example | 13 >>> x, y, z = example |
14 x = example[0] | 14 >>> x = example[0] |
15 x = example["x"] | 15 >>> x = example["x"] |
16 print example.keys() # prints ['x','y','z'] | 16 >>> print example.keys() # prints ['x','y','z'] |
17 print example.values() # prints [[1,2,3],2,3] | 17 >>> print example.values() # prints [[1,2,3],2,3] |
18 print example.items() # prints [('x',[1,2,3]),('y',2),('z',3)] | 18 >>> print example.items() # prints [('x',[1,2,3]),('y',2),('z',3)] |
19 example.append_keyval('u',0) # adds item with name 'u' and value 0 | 19 >>> example.append_keyval('u',0) # adds item with name 'u' and value 0 |
20 print len(example) # number of items = 4 here | 20 >>> print len(example) # number of items = 4 here |
21 example2 = LookupList(['v', 'w'], ['a','b']) | 21 >>> example2 = LookupList(['v', 'w'], ['a','b']) |
22 print example+example2 # addition is like for lists, a concatenation of the items. | 22 >>> print example+example2 # addition is like for lists, a concatenation of the items. |
23 example + example # throw an error as we can't have duplicate name. | 23 >>> example + example # throw an error as we can't have duplicate name. |
24 | |
24 @note: The element names should be unique. | 25 @note: The element names should be unique. |
26 | |
25 @todo: Convert this documentation into doctest | 27 @todo: Convert this documentation into doctest |
26 format, and actually perform doctest'ing: | 28 format, and actually perform doctest'ing: |
27 U{http://epydoc.sourceforge.net/manual-epytext.html#doctest-blocks} | 29 U{http://epydoc.sourceforge.net/manual-epytext.html#doctest-blocks} |
28 """ | 30 """ |
29 def __init__(self,names=[],values=[]): | 31 def __init__(self,names=[],values=[]): |