-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
303 lines (224 loc) · 9.99 KB
/
interpreter.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
import jo_db, time, os
def gets():
return raw_input("> ")
HELP = """
USAGE:
IO Operations:
1) load <name of file without extension>
will load <name...>.csv into the program and bind the result to name
2) print <name>
pretty_print whatever is stored in name
3) fieldwidth <number>
set the pretty_print display field width to <number> characters
4) filedump <name> as <outputname>
pretty_print whatever is stored in name to outputname.csv
5) shape <name>
print the shape of the dataframe stored in name.
can also be used to check whether a dataframe was loaded
as it will print nothing if not.
6) exit (or quit)
quits the interpreter
7) time
switch command return timing on or off
Assignment:
The format here depends on <op>.
General form: (subset is an exception)
let <newname> = <op> <name> <col1> <col2/num/nothing> as <type>
possible types: ints, floats, strings.
1) On two columns: <op> = add, sub, div, mul
let <newname> = <op> <name> <col1> <col2> as <type>
ex: let col1_plus_col2 = add dataframe 1 2 as strings
let col2_times_col5 = mul dataframe 2 5 as floats
let col2_divby_col3 = div dataframe 2 3 as ints
and the results can be accessed through print or filedump.
2) On one column and one number: <op> = pow, mod
let <newname> = <op> <name> <col1> <num> as <type>
ex: let col1_tothe_5thpower = pow dataframe 1 5.0 as floats
let col5_mod_2 = mod dataframe 5 2 as ints
3) On one column only: <op> = stringify, intify, floatify
let <newname> = <op> <name> <col1>
ex: let col5_ints = intify dataframe 5
let col3_floats = floatify dataframe 3
4) Subsetting by column
let <subsetname> = subset <name> num_1,num_2,...,num_n
ex: let subset_rows_1_2_and_5 = subset dataframe 1,2,5
let subset_row_8 = subset dataframe 8
Reductions:
General form:
<reduct_op> <colnum> in <name> as <type>
<reduct_op> = var (variance), sum, avg, max, min, med (median)
ex: var 4 in planes_data as floats
sum 2 in dataframe as ints
max 5 in rainfall_data as ints
Sorting:
sort <name> by <col> as <type> <order>
<order> = asc, desc
<col> = column number
ex: sort big_table by 2 as floats desc
sort phonebook by 6 as strings asc
note that <type> specifies the type of comparison
e.g. string comparisons are lexicographical, numeric comparisons are <
"""
KEYWORDS = ['load', 'sort', 'print', 'shape', 'as', 'int', 'float', 'str', \
'variance', 'let', 'avg', 'mod', 'filedump', 'subset', 'var', \
'times', 'plus', 'mean', 'average', 'med', 'median' ]
TYPES = ['ints', 'strings', 'floats']
namespace = {}
time_return_wait = False
fieldwidth = 7
while 1:
try:
if time_return_wait:
print "ELAPSED TIME: %f seconds." % (time.time() - start)
ipt = filter(None, gets().split(' '))
# print ipt
start = time.time()
# SWITCH COMMAND TIMING MODE ON OR OFF
if ipt[0] == 'time':
time_return_wait = not time_return_wait
elif ipt[0] == 'usage' or ipt[0] == 'help':
print HELP
elif ipt[0] == 'exit' or ipt[0] == 'quit' or ipt[0] == 'q':
print "good-bye."
os._exit(1)
# FUNDAMENTAL OPERATORS FOR IO
elif ipt[0] == 'load':
if len(ipt) == 2:
assert ipt[1] not in KEYWORDS
fd = open(ipt[1] + '.csv', 'r')
namespace[ipt[1]] = jo_db.DataSheet(fd)
fd.close()
elif ipt[0] == 'print':
if len(ipt) == 2:
if ipt[1] in namespace:
namespace[ipt[1]].pretty_print(field_width=fieldwidth)
elif ipt[0] == 'fieldwidth':
if len(ipt) == 2:
fieldwidth = int(ipt[1])
elif ipt[0] == 'filedump':
if ipt[1] in namespace:
assert ipt[2] == 'as'
namespace[ipt[1]].dump_to_csv(ipt[3])
elif ipt[0] == 'shape':
if len(ipt) == 2:
if ipt[1] in namespace:
print namespace[ipt[1]].shape()
# THE ASSIGNMENT OPERATOR
elif ipt[0] == 'let':
to_assign = ipt[1]
# let's not bind over keywords
assert to_assign not in KEYWORDS
assert ipt[2] == '='
# OPERATOR ON N COLUMNS
if ipt[3] == 'subset':
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].subset([int(x) for x in ipt[5].split(',')])
# OPERATORS ON TWO COLUMNS
elif ipt[3] == 'add' or ipt[3] == 'plus':
assert ipt[7] == 'as'
assert ipt[8] in TYPES
typename = ipt[8]
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].add_cols(int(ipt[5]), int(ipt[6]), as_types=typename)
elif ipt[3] == 'sub' or ipt[3] == 'minus':
assert ipt[7] == 'as'
assert ipt[8] in TYPES
typename = ipt[8]
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].sub_cols(int(ipt[5]), int(ipt[6]), as_types=typename)
elif ipt[3] == 'div':
assert ipt[7] == 'as'
assert ipt[8] in TYPES
typename = ipt[8]
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].div_cols(int(ipt[5]), int(ipt[6]), as_types=typename)
elif ipt[3] == 'mul' or ipt[3] == 'times':
assert ipt[7] == 'as'
assert ipt[8] in TYPES
typename = ipt[8]
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].mul_cols(int(ipt[5]), int(ipt[6]), as_types=typename)
# OPERATORS ON ONE COLUMN, ONE NUMBER
elif ipt[3] == 'pow':
assert ipt[7] == 'as'
assert ipt[8] in TYPES
typename = ipt[8]
if ipt[4] in namespace:
if typename == 'floats':
namespace[to_assign] = namespace[ipt[4]].pow_col(int(ipt[5]), float(ipt[6]), as_types=typename)
else:
namespace[to_assign] = namespace[ipt[4]].pow_col(int(ipt[5]), int(ipt[6]), as_types=typename)
elif ipt[3] == 'mod':
assert ipt[7] == 'as'
assert ipt[8] in TYPES
typename = ipt[8]
if ipt[4] in namespace:
if typename == 'floats':
namespace[to_assign] = namespace[ipt[4]].modn_col(int(ipt[5]), float(ipt[6]), as_types=typename)
else:
namespace[to_assign] = namespace[ipt[4]].modn_col(int(ipt[5]), int(ipt[6]), as_types=typename)
# OPERATORS ON ONE COLUMN
elif ipt[3] == 'intify':
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].intify_col(int(ipt[5]))
elif ipt[3] == 'floatify':
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].floatify_col(int(ipt[5]))
elif ipt[3] == 'stringify':
if ipt[4] in namespace:
namespace[to_assign] = namespace[ipt[4]].stringify_col(int(ipt[5]))
# REDUCTION (single-output) OPERATORS
elif ipt[0] == 'var' or ipt[0] == 'variance':
assert ipt[2] == 'in'
if ipt[3] in namespace:
print namespace[ipt[3]].var_col(int(ipt[1]))
# TYPED REDUCTION (single-output) OPERATORS
elif ipt[0] == 'min':
assert ipt[2] == 'in'
assert ipt[4] == 'as'
assert ipt[5] in TYPES
typename = ipt[5]
if ipt[3] in namespace:
print namespace[ipt[3]].min_col(int(ipt[1]), as_type=typename)
elif ipt[0] == 'max':
assert ipt[2] == 'in'
assert ipt[4] == 'as'
assert ipt[5] in TYPES
typename = ipt[5]
if ipt[3] in namespace:
print namespace[ipt[3]].max_col(int(ipt[1]), as_type=typename)
elif ipt[0] == 'avg' or ipt[0] == 'mean' or ipt[0] == 'average':
assert ipt[2] == 'in'
assert ipt[4] == 'as'
assert ipt[5] in TYPES
typename = ipt[5]
if ipt[3] in namespace:
print namespace[ipt[3]].avg_col(int(ipt[1]), as_type=typename)
elif ipt[0] == 'sum':
assert ipt[2] == 'in'
assert ipt[4] == 'as'
assert ipt[5] in TYPES
typename = ipt[5]
if ipt[3] in namespace:
print namespace[ipt[3]].sum_col(int(ipt[1]), as_type=typename)
elif ipt[0] == 'med' or ipt[0] == 'median':
assert ipt[2] == 'in'
assert ipt[4] == 'as'
assert ipt[5] in TYPES
typename = ipt[5]
if ipt[3] in namespace:
print namespace[ipt[3]].med_col(int(ipt[1]), as_type=typename)
# SORTING OPERATOR
elif ipt[0] == 'sort':
assert len(ipt) == 7
assert ipt[2] == 'by'
assert ipt[4] == 'as'
assert ipt[5] in TYPES
assert ipt[6] in ['asc', 'desc']
typename, order = ipt[5], ipt[6]
if ipt[1] in namespace:
namespace[ipt[1]].sort_by_col(int(ipt[3]), compare_as=typename, order=order)
else:
print "Didn't understand that query."
except:
print "Query malformed, or interrupt sent."