annotate corebio/utils/__init__.py @ 11:4b38580a8b97

Uploaded
author davidmurphy
date Thu, 16 Feb 2012 21:15:10 -0500
parents c55bdc2fb9fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
2
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
3 # Copyright (c) 2005 Gavin E. Crooks <gec@threeplusone.com>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
4 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
5 # This software is distributed under the MIT Open Source License.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
6 # <http://www.opensource.org/licenses/mit-license.html>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
7 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
8 # Permission is hereby granted, free of charge, to any person obtaining a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
9 # copy of this software and associated documentation files (the "Software"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
10 # to deal in the Software without restriction, including without limitation
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
11 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
12 # and/or sell copies of the Software, and to permit persons to whom the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
13 # Software is furnished to do so, subject to the following conditions:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
14 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
15 # The above copyright notice and this permission notice shall be included
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
16 # in all copies or substantial portions of the Software.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
17 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
24 # THE SOFTWARE.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
25 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
26
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
27
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
28 """Extra utilities and core classes not in standard python.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
29 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
30
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
31
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
32 __all__ = ('isblank', 'isfloat', 'isint', 'fcmp', 'remove_whitespace',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
33 'invert_dict','update', 'stdrepr', 'Token', 'Struct', 'Reiterate',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
34 'deoptparse', 'crc32', 'crc64', 'FileIndex', 'find_command',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
35 'ArgumentError', 'frozendict')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
36
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
37 import os.path
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
38 import math
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
39
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
40 def isblank( string) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
41 """Is this whitespace or an empty string?"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
42 if string == '' : return True
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
43 return string.isspace()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
44
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
45 def isfloat(s):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
46 """Does this object represent a floating point number? """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
47 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
48 float(s)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
49 return True
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
50 except (ValueError, TypeError):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
51 return False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
52
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
53 def isint(s):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
54 """Does this object represent an integer?"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
55 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
56 int(s)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
57 return True
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
58 except (ValueError, TypeError):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
59 return False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
60
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
61 def fcmp(x, y, precision):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
62 """Floating point comparison."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
63 # TODO: Doc string, default precision. Test
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
64 if math.fabs(x-y) < precision:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
65 return 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
66 elif x < y:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
67 return -1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
68 return 1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
69
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
70 def remove_whitespace( astring) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
71 """Remove all whitespace from a string."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
72 # TODO: Is this horrible slow?
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
73 return "".join(astring.split())
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
74
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
75
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
76 def invert_dict( dictionary) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
77 """Constructs a new dictionary with inverted mappings so that keys become
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
78 values and vice versa. If the values of the original dictionary are not
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
79 unique then only one of the original kesys will be included in the new
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
80 dictionary.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
81 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
82 return dict( [(value, key) for key, value in dictionary.iteritems()] )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
83
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
84
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
85
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
86 def update(obj, **entries):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
87 """Update an instance with new values.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
88
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
89 >>> update({'a': 1}, a=10, b=20)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
90 {'a': 10, 'b': 20}
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
91 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
92 if hasattr(obj, 'update') :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
93 obj.update( entries)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
94 else :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
95 for k, v in entries.iteritems() :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
96 setattr(obj, k, v)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
97 return obj
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
98
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
99
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
100
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
101 def stdrepr( obj, attributes=None, name=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
102 """Create a standard representation of an object."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
103 if name==None : name = obj.__class__.__name__
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
104 if attributes==None: attributes = obj.__class__.__slots__
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
105 args = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
106 for a in attributes :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
107 args.append( '%s=%s' % ( a, repr( getattr(obj, a) ) ) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
108 args = ',\n'.join(args).replace('\n', '\n ')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
109 return '%s(\n %s\n)' % (name, args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
110
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
111
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
112 class Token(object):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
113 """Represents the items returned by a file scanner, normally processed
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
114 by a parser.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
115
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
116 Attributes :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
117 o typeof -- a string describing the kind of token
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
118 o data -- the value of the token
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
119 o lineno -- the line of the file on which the data was found (if known)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
120 o offset -- the offset of the data within the line (if known)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
121 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
122 __slots__ = [ 'typeof', 'data', 'lineno', 'offset']
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
123 def __init__(self, typeof, data=None, lineno=-1, offset=-1) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
124 self.typeof = typeof
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
125 self.data = data
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
126 self.lineno = lineno
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
127 self.offset = offset
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
128
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
129 def __repr__(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
130 return stdrepr( self)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
131
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
132 def __str__(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
133 coord = str(self.lineno)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
134 if self.offset != -1 : coord += ':'+str(self.offset)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
135 coord = coord.ljust(7)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
136 return (coord+ ' '+ self.typeof +' : ').ljust(32)+ str(self.data or '')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
137
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
138
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
139
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
140 def Struct(**kwargs) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
141 """Create a new instance of an anonymous class with the supplied attributes
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
142 and values.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
143
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
144 >>> s = Struct(a=3,b=4)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
145 >>> s
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
146 Struct(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
147 a=3,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
148 b=4
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
149 )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
150 >>> s.a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
151 3
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
152
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
153 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
154 name = 'Struct'
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
155
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
156 def _init(obj, **kwargs) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
157 for k, v in kwargs.iteritems() :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
158 setattr( obj, k, v)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
159
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
160 def _repr(obj) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
161 return stdrepr( obj, obj.__slots__, name)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
162
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
163 adict = {}
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
164 adict['__slots__'] = kwargs.keys()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
165 adict['__init__'] = _init
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
166 adict['__repr__'] = _repr
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
167
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
168 return type( name, (object,) , adict)(**kwargs)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
169
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
170
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
171 class Reiterate(object):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
172 """ A flexible wrapper around a simple iterator.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
173 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
174 def __new__(cls, iterator):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
175 if isinstance(iterator, cls) : return iterator
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
176 new = object.__new__(cls)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
177 new._iterator = iter(iterator)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
178 new._stack = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
179 new._index = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
180 return new
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
181
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
182 def __init__(self, *args, **kw):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
183 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
184
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
185
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
186 def __iter__(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
187 return self
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
188
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
189 def next(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
190 """Return the next item in the iteration."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
191 self._index +=1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
192 if self._stack :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
193 return self._stack.pop()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
194 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
195 return self._iterator.next()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
196
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
197 def index(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
198 """The number of items returned. Incremented by next(), Decremented
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
199 by push(), unchanged by peek() """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
200 return self._index
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
201
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
202 def push(self, item) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
203 """Push an item back onto the top of the iterator,"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
204 self._index -=1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
205 self._stack.append(item)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
206
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
207 def peek(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
208 """Returns the next item, but does not advance the iteration.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
209 Returns None if no more items. (Bit may also return None as the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
210 next item.)"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
211 try :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
212 item = self.next()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
213 self.push(item)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
214 return item
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
215 except StopIteration:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
216 return None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
217
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
218 def has_item(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
219 """More items to return?"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
220 try :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
221 item = self.next()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
222 self.push(item)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
223 return True
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
224 except StopIteration:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
225 return False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
226
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
227 def filter(self, predicate):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
228 """Return the next item in the iteration that satisifed the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
229 predicate."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
230 next = self.next()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
231 while not predicate(next) : next = self.next()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
232 return next
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
233 # End class Reiterate
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
234
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
235
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
236
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
237
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
238
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
239 def crc32(string):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
240 """Return the standard CRC32 checksum as a hexidecimal string."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
241 import binascii
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
242 return "%08X"% binascii.crc32(string)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
243
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
244 _crc64_table =None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
245
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
246 def crc64(string):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
247 """ Calculate ISO 3309 standard cyclic redundancy checksum.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
248 Used, for example, by SWISS-PROT.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
249
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
250 Returns : The CRC as a hexadecimal string.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
251
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
252 Reference:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
253 o W. H. Press, S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
254 "Numerical recipes in C", 2nd ed., Cambridge University Press. Pages 896ff.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
255 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
256 # Adapted from biopython, which was adapted from bioperl
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
257 global _crc64_table
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
258 if _crc64_table is None :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
259 # Initialisation of CRC64 table
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
260 table = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
261 for i in range(256):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
262 l = i
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
263 part_h = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
264 for j in range(8):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
265 rflag = l & 1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
266 l >>= 1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
267 if part_h & 1: l |= (1L << 31)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
268 part_h >>= 1L
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
269 if rflag: part_h ^= 0xd8000000L
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
270 table.append(part_h)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
271 _crc64_table= tuple(table)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
272
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
273 crcl = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
274 crch = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
275 for c in string:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
276 shr = (crch & 0xFF) << 24
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
277 temp1h = crch >> 8
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
278 temp1l = (crcl >> 8) | shr
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
279 idx = (crcl ^ ord(c)) & 0xFF
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
280 crch = temp1h ^ _crc64_table[idx]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
281 crcl = temp1l
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
282
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
283 return "%08X%08X" % (crch, crcl)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
284 # End crc64
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
285
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
286
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
287 class FileIndex(object) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
288 """Line based random access to a file. Quickly turn a file into a read-only
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
289 database.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
290
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
291 Attr:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
292 - indexfile -- The file to be indexed. Can be set to None and latter
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
293 replaced with a new file handle, for exampel, if you need to
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
294 close and latter reopen the file.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
295
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
296 Bugs:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
297 User must set the indexedfile to None before pickling this class.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
298
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
299 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
300 __slots__ = [ 'indexedfile', '_parser', '_positions', '_keys', '_key_dict']
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
301
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
302 def __init__(self, indexedfile, linekey = None, parser=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
303 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
304
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
305 Args:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
306 - indexedfile -- The file to index
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
307 - linekey -- An optional function. keyofline() will be passed each line
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
308 of the file in turn and should return a string to index the line,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
309 or None. If keyofline() is supplied, then only lines that generate
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
310 keys are indexed.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
311 - parser -- An optional parser. A function that reads from a file handle
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
312 positioned at the start of a record and returns an object.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
313 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
314
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
315 def default_parser(seekedfile) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
316 return seekedfile.readline()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
317
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
318 if parser is None : parser = default_parser
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
319 self._parser = parser
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
320
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
321 indexedfile.seek(0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
322 positions = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
323 keys = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
324
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
325 while True :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
326 position = indexedfile.tell()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
327 line = indexedfile.readline()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
328 if line == '' : break
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
329
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
330 if linekey :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
331 k = linekey(line)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
332 if k is None: continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
333 keys.append(k)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
334
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
335 positions.append(position)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
336
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
337 self.indexedfile = indexedfile
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
338 self._positions = tuple(positions)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
339
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
340 if linekey :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
341 self._keys = tuple(keys)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
342 self._key_dict = dict( zip(keys, positions))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
343
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
344
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
345 def tell(self, item) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
346 if isinstance(item, str) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
347 p = self._key_dict[item]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
348 else :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
349 p = self._positions[item]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
350 return p
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
351
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
352 def seek(self, item) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
353 """Seek the indexfile to the position of item."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
354 self.indexedfile.seek(self.tell(item))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
355
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
356 def __iter__(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
357 for i in range(0, len(self)) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
358 yield self[i]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
359
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
360 def __len__(self) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
361 return len(self._positions)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
362
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
363 def __getitem__(self, item) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
364 self.indexedfile.seek(self.tell(item))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
365 return self._parser(self.indexedfile)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
366
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
367 def __contains__(self, item) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
368 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
369 self.tell(item)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
370 return True
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
371 except KeyError :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
372 return False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
373 except IndexError :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
374 return False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
375
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
376 # End class FileIndex
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
377
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
378
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
379 def find_command(command, path=None):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
380 """Return the full path to the first match of the given command on
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
381 the path.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
382
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
383 Arguments:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
384 - command -- is a the name of the executable to search for.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
385 - path -- is an optional alternate path list to search. The default it
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
386 to use the COREBIOPATH environment variable, if it exists, else the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
387 PATH environment variable.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
388
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
389 Raises:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
390 - EnvironmentError -- If no match is found for the command.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
391
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
392 By default the COREBIO or PATH environment variable is searched (as well
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
393 as, on Windows, the AppPaths key in the registry), but a specific 'path'
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
394 list to search may be specified as well.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
395
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
396 Author: Adapted from code by Trent Mick (TrentM@ActiveState.com)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
397 See: http://trentm.com/projects/which/
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
398 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
399 import _which
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
400 if path is None :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
401 path = os.environ.get("COREBIOPATH", "").split(os.pathsep)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
402 if path==['']: path = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
403
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
404 try :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
405 match =_which.whichgen(command, path).next()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
406 except StopIteration, _which.WhichError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
407 raise EnvironmentError("Could not find '%s' on the path." % command)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
408 return match
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
409
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
410
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
411
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
412 class ArgumentError(ValueError) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
413 """ A subclass of ValueError raised when a function receives an argument
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
414 that has the right type but an inappropriate value, and the situation is not
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
415 described by a more precise exception such as IndexError. The name of the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
416 argument or component at fault and (optionally) the value are also stored.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
417 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
418
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
419 def __init__(self, message, key, value=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
420 """ Args:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
421 - message -- An error message.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
422 - key -- The name of the argument or component at fault.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
423 - value -- Optional value of the argument.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
424 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
425 ValueError.__init__(self, message)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
426 self.key = key
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
427 self.value = value
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
428 # end class ArgumentError
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
429
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
430
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
431 class frozendict(dict):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
432 """A frozendict is a dictionary that cannot be modified after being created
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
433 - but it is hashable and may serve as a member of a set or a key in a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
434 dictionary.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
435 # Author: Adapted from code by Oren Tirosh
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
436 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
437 # See: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/414283
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
438
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
439 def _blocked_attribute(obj):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
440 raise AttributeError, "A frozendict cannot be modified."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
441 _blocked_attribute = property(_blocked_attribute)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
442
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
443 __delitem__ = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
444 __setitem__ = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
445 clear = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
446 pop = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
447 popitem = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
448 setdefault = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
449 update = _blocked_attribute
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
450
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
451 def __new__(cls, *args, **kw):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
452 new = dict.__new__(cls)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
453 dict.__init__(new, *args, **kw)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
454 return new
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
455
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
456 def __init__(self, *args, **kw):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
457 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
458
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
459 def __hash__(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
460 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
461 return self._cached_hash
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
462 except AttributeError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
463 # Hash keys, not items, since items can be mutable and unhasahble.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
464 h = self._cached_hash = hash(tuple(sorted(self.keys())))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
465 return h
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
466
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
467 def __repr__(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
468 return "frozendict(%s)" % dict.__repr__(self)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
469 # end class frozendict
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
470