-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·139 lines (116 loc) · 4.53 KB
/
generate.py
File metadata and controls
executable file
·139 lines (116 loc) · 4.53 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
''' Generate mutable type code '''
import sys
MATH_FUNCTIONS = [
('add', '+'),
('sub', '-'),
('mul', '*'),
# Division is handled differently in python2 and python3
# We'll substitue the python3 options lower
('div', '/'),
]
MATH_CODE = {
'': 'return self.val {} other',
'r': 'return other {} self.val',
'i': 'self.val {}= other;return self',
}
mut_types = {
'bool': {
'numeric': False,
},
'int': {
'numeric': True,
},
'float': {
'numeric': True,
},
# str intentionally not added here as it, as it is between simple and
# numeric types
}
for prim, info in mut_types.items():
fname = 'mutable_primitives/{}.py'.format(prim)
cname = prim[0].upper() + prim[1:]
numeric = info['numeric']
header = [
"''' {} - A mutable {} class".format(cname, prim),
"",
"##################################################",
"###### AUTOGENERATED - DO NOT EDIT DIRECTLY ######",
"##################################################",
"'''"
]
imports = [
"\nimport sys" if numeric else "",
"from mutable_primitives.base import Mutable",
"",
"",
]
def cls_func(defline, code):
code = [' {}'.format(x) for x in code]
return [""] + [' {}'.format(x) for x in [defline] + code]
cls_code = [
"class {}(Mutable):".format(cname),
" ''' {} - A mutable {} class '''".format(cname, prim),
" base = {}".format(prim),
]
cls_code.extend(cls_func("def __init__(self, val):", [
"super({}, self).__init__(val, self.base) #pylint: disable=super-with-arguments".format(cname),
"self.val = val",
]))
cls_code.extend(cls_func("def get(self):", [
"''' get raw (primitive) value '''",
"return self.val",
]))
cls_code.extend(cls_func("def set(self, val):", [
"''' set raw (primitive) value '''",
"assert isinstance(val, self.base)",
"self.val = val",
]))
cls_code.extend(cls_func("def __eq__(self, other):", [
"return self.val == other",
]))
cls_code.extend(cls_func("def __ne__(self, other):", [
"return self.val != other",
]))
cls_code.extend(cls_func("def __str__(self):", [
"return '{}({})'.format(self.__class__.__name__, self.val)",
]))
cls_code.extend(cls_func("def __repr__(self):", [
"return '{}({})'.format(self.__class__.__name__, self.val)",
]))
cls_code.extend(cls_func("def __bool__(self):", [
"''' boolean test for python3 '''",
"if self.val:",
" return True",
"return False",
]))
cls_code.extend(cls_func("def __nonzero__(self):", [
"''' boolean test for python2 '''",
"if self.val:",
" return True",
"return False",
]))
if info['numeric']:
for mtype, basecode in MATH_CODE.items():
for basename, op in MATH_FUNCTIONS:
if basename == 'div':
def indent(code):
if code:
return ' ' + code
return ''
# python2 div
cls_code.extend(["", " if sys.version_info[0] < 3:"])
divcode = cls_func("def __{}{}__(self, other):".format(mtype, 'div'), [] + basecode.format('/').split(';'))
cls_code.extend([indent(x) for x in divcode][1:])
# python2 floordiv and truediv
cls_code.extend([" else:"])
divcode = cls_func("def __{}{}__(self, other):".format(mtype, 'floordiv'), [] + basecode.format('//').split(';'))
cls_code.extend([indent(x) for x in divcode][1:])
divcode = cls_func("def __{}{}__(self, other):".format(mtype, 'truediv'), [] + basecode.format('/').split(';'))
cls_code.extend([indent(x) for x in divcode])
#('div', '/'),
#('floordiv', '//'),
#('truediv', '/'),
else:
cls_code.extend(cls_func("def __{}{}__(self, other):".format(mtype, basename), [] + basecode.format(op).split(';')))
open(fname, 'w+').write('\n'.join(header + imports + cls_code + ['']))