- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpy_struct.py
More file actions
Latest commit
43 lines (31 loc) · 1.1 KB
/
Copy pathpy_struct.py
File metadata and controls
43 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
classStructMeta(type):
def__new__(cls, name, bases, dct):
ifname!='Struct':
mandatory_attrs= []
optional_attrs= []
attrs=dct['attrs'].replace(" ", "")
attrs=attrs.replace("\t", "")
if'['inattrs:
m_arttrs, opt_attrs=attrs.split('[')
assertopt_attrs.endswith(']')
assertopt_attrs.startswith(',')
mandatory_attrs=m_arttrs.split(',')
optional_attrs=opt_attrs[1:-1].split(',')
else:
mandatory_attrs=attrs.split(',')
optional_attrs= []
all_attrs=mandatory_attrs+optional_attrs
init_attrs=", ".join(mandatory_attrs[:])
ifoptional_attrs:
init_attrs+=", "
init_attrs+=", ".join(map("{0}=None".format, optional_attrs))
init="def __init__(self, {0}):\n".format(init_attrs)
fornameinall_attrs:
init+=" self.{0} = {0}\n".format(name)
loc= {}
execcompile(init, "<tempo_file_for_class_{0}>".format(name), \
'exec') inloc
dct['__init__'] =loc['__init__']
returnsuper(StructMeta, cls).__new__(cls, name, bases, dct)
classStruct(object):
__metaclass__=StructMeta