aboutsummaryrefslogtreecommitdiffstats
path: root/common.py
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2012-11-26 16:02:48 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2012-11-26 16:02:48 -0500
commitcb8db5d30ee769304c2c2b00f2a7d9bcb3c4098f (patch)
treec5352d84285af565d5246c3eb861ffba709761f1 /common.py
parent41c867480f1e20bd3b168258ed71450499ea6ccf (diff)
Removed 2-step parse for scheduling statistics.
Diffstat (limited to 'common.py')
-rw-r--r--common.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/common.py b/common.py
index a09ef7c..984f584 100644
--- a/common.py
+++ b/common.py
@@ -1,4 +1,78 @@
1import sys
1from collections import defaultdict 2from collections import defaultdict
3from textwrap import dedent
4
5def recordtype(typename, field_names, default=0):
6 ''' Mutable namedtuple. Recipe from George Sakkis of MIT.'''
7 field_names = tuple(map(str, field_names))
8 # Create and fill-in the class template
9 numfields = len(field_names)
10 argtxt = ', '.join(field_names)
11 reprtxt = ', '.join('%s=%%r' % f for f in field_names)
12 dicttxt = ', '.join('%r: self.%s' % (f,f) for f in field_names)
13 tupletxt = repr(tuple('self.%s' % f for f in field_names)).replace("'",'')
14 inittxt = '; '.join('self.%s=%s' % (f,f) for f in field_names)
15 itertxt = '; '.join('yield self.%s' % f for f in field_names)
16 eqtxt = ' and '.join('self.%s==other.%s' % (f,f) for f in field_names)
17 template = dedent('''
18 class %(typename)s(object):
19 '%(typename)s(%(argtxt)s)'
20
21 __slots__ = %(field_names)r
22
23 def __init__(self, %(argtxt)s):
24 %(inittxt)s
25
26 def __len__(self):
27 return %(numfields)d
28
29 def __iter__(self):
30 %(itertxt)s
31
32 def __getitem__(self, index):
33 return getattr(self, self.__slots__[index])
34
35 def __setitem__(self, index, value):
36 return setattr(self, self.__slots__[index], value)
37
38 def todict(self):
39 'Return a new dict which maps field names to their values'
40 return {%(dicttxt)s}
41
42 def __repr__(self):
43 return '%(typename)s(%(reprtxt)s)' %% %(tupletxt)s
44
45 def __eq__(self, other):
46 return isinstance(other, self.__class__) and %(eqtxt)s
47
48 def __ne__(self, other):
49 return not self==other
50
51 def __getstate__(self):
52 return %(tupletxt)s
53
54 def __setstate__(self, state):
55 %(tupletxt)s = state
56 ''') % locals()
57 # Execute the template string in a temporary namespace
58 namespace = {}
59 try:
60 exec template in namespace
61 except SyntaxError, e:
62 raise SyntaxError(e.message + ':\n' + template)
63 cls = namespace[typename]
64
65 # Setup defaults
66 init_defaults = tuple(default for f in field_names)
67 cls.__init__.im_func.func_defaults = init_defaults
68
69 # For pickling to work, the __module__ variable needs to be set to the frame
70 # where the named tuple is created. Bypass this step in environments where
71 # sys._getframe is not defined (Jython for example).
72 if hasattr(sys, '_getframe') and sys.platform != 'cli':
73 cls.__module__ = sys._getframe(1).f_globals['__name__']
74
75 return cls
2 76
3def load_params(fname): 77def load_params(fname):
4 params = defaultdict(int) 78 params = defaultdict(int)