@@ -43,40 +43,28 @@ def __init__(self, keys, values):
4343 self ._keys = tuple (keys )
4444 self ._values = tuple (values )
4545
46- def keys (self ):
47- """ Return the keys (key names) of the record
48- """
49- return self ._keys
50-
51- def values (self ):
52- """ Return the values of the record
53- """
54- return self ._values
46+ def __repr__ (self ):
47+ values = self ._values
48+ s = []
49+ for i , field in enumerate (self ._keys ):
50+ s .append ("%s=%r" % (field , values [i ]))
51+ return "<%s %s>" % (self .__class__ .__name__ , " " .join (s ))
5552
56- def items (self ):
57- """ Return the fields of the record as a list of key and value tuples
58- """
59- return zip (self ._keys , self ._values )
53+ def __hash__ (self ):
54+ return hash (self ._keys ) ^ hash (self ._values )
6055
61- def index (self , key ):
62- """ Return the index of the given key
63- """
56+ def __eq__ (self , other ):
6457 try :
65- return self ._keys .index (key )
66- except ValueError :
67- raise KeyError (key )
68-
69- def __record__ (self ):
70- return self
71-
72- def __contains__ (self , key ):
73- return self ._keys .__contains__ (key )
58+ return (self ._keys == tuple (other .keys ()) and
59+ self ._values == tuple (other .values ()))
60+ except AttributeError :
61+ return False
7462
75- def __iter__ (self ):
76- return iter ( self ._keys )
63+ def __ne__ (self , other ):
64+ return not self .__eq__ ( other )
7765
78- def copy (self ):
79- return Record (self ._keys , self . _values )
66+ def __len__ (self ):
67+ return len (self ._keys )
8068
8169 def __getitem__ (self , item ):
8270 if isinstance (item , string ):
@@ -86,27 +74,122 @@ def __getitem__(self, item):
8674 else :
8775 raise TypeError (item )
8876
89- def __len__ (self ):
90- return len (self ._keys )
91-
92- def __repr__ (self ):
93- values = self ._values
94- s = []
95- for i , field in enumerate (self ._keys ):
96- s .append ("%s=%r" % (field , values [i ]))
97- return "<Record %s>" % " " .join (s )
98-
99- def __hash__ (self ):
100- return hash (self ._keys ) ^ hash (self ._values )
77+ def __iter__ (self ):
78+ return iter (self ._keys )
10179
102- def __eq__ (self , other ):
80+ def __contains__ (self , key ):
10381 try :
104- return self ._keys == tuple ( other . keys ()) and self . _values == tuple ( other . values () )
105- except AttributeError :
82+ self .index ( key )
83+ except ( IndexError , KeyError ) :
10684 return False
85+ else :
86+ return True
10787
108- def __ne__ (self , other ):
109- return not self .__eq__ (other )
88+ def index (self , item ):
89+ """ Return the index of the given item.
90+ """
91+ if isinstance (item , integer ):
92+ if 0 <= item < len (self ._keys ):
93+ return item
94+ raise IndexError (item )
95+ if isinstance (item , string ):
96+ try :
97+ return self ._keys .index (item )
98+ except ValueError :
99+ raise KeyError (item )
100+ raise TypeError (item )
101+
102+ def value (self , item = 0 , default = None ):
103+ """ Obtain a single value from the record by index or key. If no
104+ index or key is specified, the first value is returned. If the
105+ specified item does not exist, the default value is returned.
106+
107+ :param item:
108+ :param default:
109+ :return:
110+ """
111+ try :
112+ index = self .index (item )
113+ except (IndexError , KeyError ):
114+ return default
115+ else :
116+ return self ._values [index ]
117+
118+ def keys (self ):
119+ """ Return the keys of the record.
120+
121+ :return: tuple of key names
122+ """
123+ return self ._keys
124+
125+ def values (self , * items ):
126+ """ Return the values of the record, optionally filtering to
127+ include only certain values by index or key.
128+
129+ :param items: indexes or keys of the items to include; if none
130+ are provided, all values will be included
131+ :return: tuple of values
132+ """
133+ if items :
134+ d = []
135+ values = self ._values
136+ for item in items :
137+ try :
138+ i = self .index (item )
139+ except KeyError :
140+ d .append (None )
141+ else :
142+ d .append (values [i ])
143+ return tuple (d )
144+ return self ._values
145+
146+ def items (self , * items ):
147+ """ Return the fields of the record as a list of key and value tuples
148+
149+ :return:
150+ """
151+ if items :
152+ d = []
153+ keys = self ._keys
154+ values = self ._values
155+ for item in items :
156+ try :
157+ i = self .index (item )
158+ except KeyError :
159+ d .append ((item , None ))
160+ else :
161+ d .append ((keys [i ], values [i ]))
162+ return d
163+ return list (zip (self ._keys , self ._values ))
164+
165+ def data (self , * items ):
166+ """ Return the keys and values of this record as a dictionary,
167+ optionally including only certain values by index or key. Keys
168+ provided in the items that are not in the record will be
169+ inserted with a value of :py:const:`None`; indexes provided
170+ that are out of bounds will trigger an :py:`IndexError`.
171+
172+ :param items: indexes or keys of the items to include; if none
173+ are provided, all values will be included
174+ :return: dictionary of values, keyed by field name
175+ :raises: :py:`IndexError` if an out-of-bounds index is specified
176+ """
177+ if items :
178+ d = {}
179+ keys = self ._keys
180+ values = self ._values
181+ for item in items :
182+ try :
183+ i = self .index (item )
184+ except KeyError :
185+ d [item ] = None
186+ else :
187+ d [keys [i ]] = values [i ]
188+ return d
189+ return dict (self )
190+
191+ def copy (self ):
192+ return self .__class__ (self ._keys , self ._values )
110193
111194
112195class Entity (object ):
0 commit comments