-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpretor.py
174 lines (132 loc) · 6.72 KB
/
interpretor.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
import common
def spc(string:str, strip=False):
"""Splits a given string on parenthesis and commas, taking just into acount first level parenthesis.
For instance: "(abc,def),ghi,(jkl,(mno))" -> ["abc,def","ghi","jkl,(mno)"]
Args:
string (str): The string to split
strip (bool): The boolean representing if the values should be stripped from forbidden characters before being processed.
Returns:
list(str): The list of substrings
"""
#getting parenthesis index
po=[] #list of opening parenthesis
pf=[] #list of closing parenthesis
indexo=0 #index of the last opening parenthesis found
indexf=0 #index of the last closing parenthesis found
# print("string =", string)
forbidden_char = ["[", "]", "'", '"']
if strip:
if len(string) == 1 and string[0] in forbidden_char:
string = ""
while len(string) >= 2 and string[0] in forbidden_char:
string = string[1:]
while len(string) >= 2 and string[-1] in forbidden_char:
string = string[:-1]
while indexo!=-1 and indexf!=-1: # While an opening and closing parenthesis have been found
indexo=string.find('(',indexo) # find the next parethesis index starting from the last one
indexf=string.find(')',indexf)
if (indexo!=-1 and indexf!=-1): # If an opening and closing parenthesis have been found
if (len(pf)>0 and indexo<pf[-1]): # If the situation is "...(...(...)...)..." the new opening parenthesis is a lower level one
pf[-1]=indexf # last closing parenthesis was a lower level one (inside other parenthesis) and should not be taken into account
indexf+=1
indexo+=1
else : # first level parenthesis
po.append(indexo)
indexo+=1
pf.append(indexf)
indexf+=1
#getting substrings
substr=[] # list of substrings to be returned
if (len(po)==0): # no parenthesis found: split the string only on commas
substr+=string.split(',')
elif (po[0]!=0): # take the begining of the string (before the first parenthesis) and split it on commas
substr+=(string[0:po[0]]).split(',')
for i in range(len(po)): # for each pair of '(' ')', gets what's inbetween
substr.append(string[po[i]+1:pf[i]])
if (i<len(po)-1): # gets what's inbetween pairs and split it on commas
substr+=(string[pf[i]+1:po[i+1]]).split(',')
if (len(pf)>0 and pf[-1]!=len(string)-1): # take the end of the string (after the first parenthesis) and split it on commas
substr+=(string[pf[-1]+1:]).split(',')
#epurating from void substring, residual apostrophe and unwanted substrings
substr=[s for s in substr if s not in ['',"'","['","']",'[',']','[]']]
return substr
def interp(string, **kwargs):
"""Interprets a given string according to the specification in **kwargs
Supported types are:
int, float, str, Color, Position, Size, Player, Wall, list and tuple
Args:
string (str): The string to interpret.
**kwargs (dict[str|Any]): kwargs should match the type of the desired variable.
Ex: for an int i of value a one shoulde use "interp('a',i=0)"
Special cases:
list: interp("[...]", list=[a,i]) where a matches the type of the list elements and i is the number of elements to look for.
or list=[a,0] when all substrings match the a structure, should be the last kwarg
or list=[None,-1,...] where ... matches the type of elements one by one
edge effect
tuple: interp("(...)", tuple=(a,i)) see list (no edge effect)
Returns:
dict[str|Any]: updated kwargs
"""
# local imports to avoid circular imports
from player import Player
from wall import Wall
# splits the given string
values=spc(string)
# current index
i=0
for arg in kwargs:
# basic types
if type(kwargs[arg])==str:
kwargs[arg]=values[i]
elif type(kwargs[arg])==int:
kwargs[arg]=int(values[i])
elif type(kwargs[arg])==float:
kwargs[arg]=float(values[i])
# common types, uses recursivity
elif type(kwargs[arg])==common.Color:
d=interp(values[i],r=0,g=0,b=0)
kwargs[arg]=common.Color(d['r'],d['g'],d['b'])
elif type(kwargs[arg])==common.Position:
d=interp(values[i],x=0,y=0)
kwargs[arg]=common.Position(d['x'],d['y'])
elif type(kwargs[arg])==common.Size:
d=interp(values[i],w=0,h=0)
kwargs[arg]=common.Size(d['w'],d['h'])
# specific types, uses recursivity
elif type(kwargs[arg])==Player:
d=interp(values[i],teamId=0, username="",color=common.Color(),position=common.Position(),size=common.Size())
# get rid of '' around usernames due to str(str)
if len(d['username'])>2 and ((d['username'][0]=="'" and d['username'][-1]=="'") or (d['username'][0]=="\"" and d['username'][-1]=="\"")):
d['username']=d['username'][1:-1]
kwargs[arg]=Player(d['teamId'],d['username'],d['color'],d['position'],d['size'])
elif type(kwargs[arg])==Wall:
d=interp(values[i],id=0,color=common.Color(),position=common.Position(),size=common.Size())
kwargs[arg]=Wall(d['id'],d['color'],d['position'],d['size'])
# list, uses recursivity, shifts index
elif type(kwargs[arg])==list:
# kwargs[arg]=[a,k...]
values = spc(string, True)
k=kwargs[arg][1]
if k==-1:
k=len(kwargs[arg])-2
for j in range(k):
d=interp('('+values[i+j]+')',element=kwargs[arg][2+j])
kwargs[arg][2+j]=d['element']
elif k==0:
k=len(values)-i
for j in range(k):
d=interp('('+values[i+j]+')',element=kwargs[arg][0])
kwargs[arg].append(d['element'])
else :
for j in range(k):
d=interp('('+values[i+j]+')',element=kwargs[arg][0])
kwargs[arg].append(d['element'])
i+=k # shift current index
del kwargs[arg][0] # removes a from the list
del kwargs[arg][0] # removes k from the list
# tuple, uses recursivity
elif type(kwargs[arg])==tuple:
d=interp(values[i],list=list(kwargs[arg]))
kwargs[arg]=tuple(d['list'])
i+=1
return kwargs