Mercurial > pylearn
comparison lookup_list.py @ 82:158653a9bc7c
Automated merge with ssh://p-omega1@lgcm.iro.umontreal.ca/tlearn
author | Frederic Bastien <bastienf@iro.umontreal.ca> |
---|---|
date | Mon, 05 May 2008 11:02:03 -0400 |
parents | 427e02ef0629 |
children | 8fa1ef2411a0 |
comparison
equal
deleted
inserted
replaced
78:3499918faa9d | 82:158653a9bc7c |
---|---|
1 | 1 |
2 from copy import copy | 2 from copy import deepcopy |
3 | 3 |
4 class LookupList(object): | 4 class LookupList(object): |
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 |
15 print example.keys() # prints ['x','y','z'] | 15 print example.keys() # prints ['x','y','z'] |
16 print example.values() # prints [[1,2,3],2,3] | 16 print example.values() # prints [[1,2,3],2,3] |
17 print example.items() # prints [('x',[1,2,3]),('y',2),('z',3)] | 17 print example.items() # prints [('x',[1,2,3]),('y',2),('z',3)] |
18 example.append_keyval('u',0) # adds item with name 'u' and value 0 | 18 example.append_keyval('u',0) # adds item with name 'u' and value 0 |
19 print len(example) # number of items = 4 here | 19 print len(example) # number of items = 4 here |
20 print example+example # addition is like for lists, a concatenation of the items. | 20 example2 = LookupList(['v', 'w'], ['a','b']) |
21 print example+example2 # addition is like for lists, a concatenation of the items. | |
22 example + example # throw an error as we can't have duplicate name. | |
21 Note that the element names should be unique. | 23 Note that the element names should be unique. |
22 """ | 24 """ |
23 def __init__(self,names=[],values=[]): | 25 def __init__(self,names=[],values=[]): |
24 assert len(values)==len(names) | 26 assert len(values)==len(names) |
25 self.__dict__['_values']=values | 27 self.__dict__['_values']=values |
73 | 75 |
74 def __repr__(self): | 76 def __repr__(self): |
75 return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()]) | 77 return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()]) |
76 | 78 |
77 def __add__(self,rhs): | 79 def __add__(self,rhs): |
78 new_example = copy(self) | 80 new_example = deepcopy(self) |
79 for item in rhs.items(): | 81 for item in rhs.items(): |
80 new_example.append_keyval(item[0],item[1]) | 82 new_example.append_keyval(item[0],item[1]) |
81 return new_example | 83 return new_example |
82 | 84 |
83 def __radd__(self,lhs): | 85 def __radd__(self,lhs): |
84 new_example = copy(lhs) | 86 new_example = deepcopy(lhs) |
85 for item in self.items(): | 87 for item in self.items(): |
86 new_example.append_keyval(item[0],item[1]) | 88 new_example.append_keyval(item[0],item[1]) |
87 return new_example | 89 return new_example |
88 | 90 |
89 | 91 |
92 def __eq__(self, other): | |
93 return self._values==other._values and self._name2index==other._name2index and self._names==other._names | |
94 | |
95 def __ne__(self, other): | |
96 return not self.__eq__(other) | |
97 | |
98 def __hash__(): | |
99 raise NotImplementedError() |