-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
305 lines (227 loc) · 8.03 KB
/
functions.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
class var:
def __init__(self):
self.dataDict = {}
def exists(self,name):
try:
test = self.dataDict[name]
return True
except:
return False
def add(self,name,data):
self.dataDict[name] = ' '.join(data)
def addBool(self,name,data):
self.dataDict[name] = data
def addInt(self,name,data):
self.dataDict[name] = int(data)
def addFloat(self,name,data):
self.dataDict[name] = float(data)
def addArray(self,name,data):
self.dataDict[name] = data
def return_(self,name):
return self.dataDict[name]
def search(self, var, type):
returnList = []
analyzedStr = self.dataDict[var]
# check to see if variable is a list
if isinstance(analyzedStr,list):
analyzedElem = analyzedStr
else:
analyzedElem = analyzedStr.split()
if type == 'str' or type == 'string':
for elem in analyzedElem:
try:
int(elem)
except:
try:
float(elem)
except:
if not isinstance(elem,list):
returnList.append(elem)
else:
for elem in analyzedElem:
if type == 'arr' or type == '[]':
if isinstance(elem, list):
returnList.append(elem)
continue
else:
pass
try:
if type == 'int':
int(elem)
returnList.append(elem)
except:
pass
try:
if type == 'float':
float(elem)
try:
int(elem)
except:
returnList.append(elem)
except:
pass
return returnList
def init():
return var()
# VARIABLE functions
# SET / CHANGE FUNCTIONS
def dynamic(varData, line=[]):
# setting a DYNAMIC variable
# first we are going to check for syntax correctness
if line[2] == '=' or line[2] == 'is':
syntax = True
else:
syntax = False
if syntax:
varData.add(line[1],line[3:])
return varData
def static(varData, line=[]):
# setting a STATIC variable
# first we are going to check for syntax correctness
if line[2] == '=' or line[2] == 'is':
syntax = True
else:
syntax = False
if syntax:
if varData.exists(line[3]):
if len(line) > 4:
if line[4] == ':':
findData = varData.search(line[3],line[5])
varData.add(line[1], findData)
else:
varData.add(line[1], varData.return_(line[3:]))
else:
varData.add(line[1], line[3:])
return varData
# creating a boolean
# a true or false expression
# new bool name = 'true' 'false' or function / var
# 0 1 2 3 4 ...
def makeBoolean(varData, line=[]):
if varData.exists(line[4]):
if varData.return_(line[4]) == 'True' or varData.return_(line[4]) == 'False':
varData.addBool(line[2], varData.return_(line[4]))
return varData
else:
pass
varData.addBool(line[2], line[4])
return varData
# checking for an int
# returning a boolean stating
def makeInt(varData, line=[]):
# new int seven = 7
# 0 1 2 3 4
if varData.exists(line[4]):
try:
int(varData.return_(line[4]))
varData.addInt(line[2], varData.return_(line[4]))
return varData
except:
print('CAST VAR IS NOT AN (INT)')
try:
int(line[4])
varData.addInt(line[2], line[4])
except:
print('TYPE ERROR, TYPE IS NOT (INT)')
return varData
def makeFloat(varData,line=[]):
# new int seven = 7
# 0 1 2 3 4
if varData.exists(line[4]):
try:
float(varData.return_(line[4]))
varData.addFloat(line[2], varData.return_(line[4]))
return varData
except:
print('CAST VAR IS NOT A (FLOAT)')
try:
float(line[4])
varData.addFloat(line[2], line[4])
except:
print('TYPE ERROR, TYPE IS NOT A (FLOAT)')
return varData
def makeArray(varData,line=[]):
# new [] test = array
# 0 1 2 3 4 5 ...
buildArr = []
if len(line) < 4:
# here we are trying to type convert
splitchar = line[1][1]
# no character specified so assumes a space
if splitchar == ']': splitchar = ' '
# retrieve the data within the variable and split by the specified token
parseData = varData.return_(line[2][:-1])
for char in parseData.split(splitchar):
buildArr.append(char.strip())
# append the built array into the data dictionary
varData.addArray(line[2], buildArr)
return varData
# allow for [x,y,z] syntax
if line[4][0] == '[':
if len(line[4:]) > 1:
arrStr = str(''.join(line[4:])).replace('[','')
else:
arrStr = str(line[4]).replace('[','')
arrData = arrStr.replace(']','')
splitchar = ','
for char in arrData.split(splitchar):
if varData.exists(char.strip()):
buildArr.append(varData.return_(char.strip()))
else:
buildArr.append(char.strip())
else:
typeConv = False
# here we locate the character which we would like to
# segment the data into an array by
# this will be important as this should be customizable with relative ease
# in order to easily segment more complex data into arrays
splitchar = line[1][1]
# no character specified so assumes a space
if splitchar == ']': splitchar = ' '
# checks to see if there is only one argument remaining
if len(line[4:]) > 1 and line[5] != ':':
arrData = ' '.join(line[4:])
else:
arrData = line[4]
if arrData[-1] == ';':
# type converting into a new variable
typeConv = True
arrData = varData.return_(arrData[:-1])
else:
typeConv = False
try:
if line[5] == ':':
# we are type slicing one variable into a list
arrData = varData.search(arrData, line[6])
if line[6] == 'arr':
typeConv = False
else:
arrData = ' '.join(arrData)
splitchar = ' '
typeConv = False
else:
typeConv = False
except:
pass
# iterate through the given data using our specified token
# strip spaces off incase they were used alongside the token
if isinstance(arrData,list):
buildArr = arrData
else:
scanData = arrData.split(splitchar)
for char in scanData:
if typeConv is False and varData.exists(char.strip()):
buildArr.append(varData.return_(char.strip()))
else:
buildArr.append(char.strip())
# append the built array into the data dictionary
varData.addArray(line[2], buildArr)
# return the complete dictionary
return varData
# VARIABLE RETURN FUNCTION
def return_(varData, var):
return varData.return_(var)
def search(varData, var, type):
return varData.search(var,type)
def createVar(self):
return None