forked from lassefolkersen/highfrontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.py
442 lines (321 loc) · 16.1 KB
/
primitives.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import pygame
import global_variables
import datetime
#from ocempgui.widgets import *
#from ocempgui.widgets.Constants import *
def sort_dict(dict):
items = list(dict.items())
items.sort()
return [value for key, value in items]
def invert_dict(d):
inv = {}
for k,v in d.items():
keys = inv.setdefault(v, [])
keys.append(k)
return inv
def make_linear_x_axis(surface,frame_size,xlim,solar_system_object_link,unit=""):
"""
Function that paints a linear x axis on a surface and returns it. Needs the surface, the frame size (how much space should be left in
corners). The xlim - ie the range of x-values. Optional values
unit - a string specifying the unit type. If 'date' the vector given as int's representing time since start_date
"""
size = surface.get_size()
tick_mark_width = size[1] / 100
pygame.draw.line(surface,(0,0,0),(0,size[1]-frame_size),(size[0],size[1]-frame_size))
if unit == "date":
tick_marks = 3
else:
tick_marks = 5
step_between_ticks = (size[0] - 2 * frame_size) / (tick_marks-1)
for i in range(0,tick_marks):
tick_mark_placement = size[0] - (i * step_between_ticks + frame_size)
if unit == "date":
tick_mark_value_raw = xlim[1] - ((xlim[1]-xlim[0]) * (i/float(tick_marks-1)))
tick_mark_value = datetime.timedelta(tick_mark_value_raw) + solar_system_object_link.start_date
tick_mark_rendered_value = global_variables.standard_font.render(str(tick_mark_value),True,(0,0,0))
tick_mark_rendered_value_size = global_variables.standard_font.size(str(tick_mark_value))
else:
tick_mark_value = xlim[1] - ((xlim[1]-xlim[0]) * (i/float(tick_marks-1)))
tick_mark_rendered_value = global_variables.standard_font.render("%.3g" %tick_mark_value,True,(0,0,0))
tick_mark_rendered_value_size = global_variables.standard_font.size("%.3g" %tick_mark_value)
pygame.draw.line(surface,(0,0,0),(tick_mark_placement,size[1]-frame_size-tick_mark_width),(tick_mark_placement,size[1]-frame_size+tick_mark_width))
x_correction = 0
y_correction = 0
surface.blit(tick_mark_rendered_value,(tick_mark_placement - tick_mark_rendered_value_size[0]/2, size[1]-frame_size + tick_mark_rendered_value_size[1]))
if unit not in ["","date"]:
unit_rendered_value = global_variables.standard_font.render(unit,True,(0,0,0))
unit_rendered_value_size = global_variables.standard_font.size(unit)
surface.blit(unit_rendered_value,(size[0]-frame_size, size[1]-frame_size))
return surface
def make_linear_y_axis(surface,frame_size,ylim,solar_system_object_link, unit=""):
"""
Function that paints a linear y axis on a surface and returns it. Needs the surface, the frame size (how much space should be left in
corners). The ylim - ie the range of y-values. Optional values
unit - a string specifying the unit type. If 'date' the vector given as int's representing time since start_date
"""
size = surface.get_size()
tick_mark_width = size[0] / 100
pygame.draw.line(surface,(0,0,0),(frame_size,0),(frame_size,size[1]))
if unit == "date":
tick_marks = 3
else:
tick_marks = 5
step_between_ticks = (size[1] - 2 * frame_size) / (tick_marks-1)
for i in range(0,tick_marks):
tick_mark_placement = i * step_between_ticks + frame_size
if unit == "date":
tick_mark_value_raw = ylim[1] - ((ylim[1]-ylim[0]) * (i/float(tick_marks-1)))
tick_mark_value = datetime.timedelta(tick_mark_value_raw) + solar_system_object_link.start_date
tick_mark_rendered_value = global_variables.standard_font.render(str(tick_mark_value),True,(0,0,0))
tick_mark_rendered_value_size = global_variables.standard_font.size(str(tick_mark_value))
else:
tick_mark_value = ylim[1] - ((ylim[1]-ylim[0]) * (i/float(tick_marks-1)))
tick_mark_rendered_value = global_variables.standard_font.render("%.3g" %tick_mark_value,True,(0,0,0))
tick_mark_rendered_value_size = global_variables.standard_font.size("%.3g" %tick_mark_value)
pygame.draw.line(surface,(0,0,0),(frame_size-tick_mark_width,tick_mark_placement),(frame_size+tick_mark_width,tick_mark_placement))
if frame_size-tick_mark_width*2-tick_mark_rendered_value_size[0] < 0: #to make sure that the unit doesn't wrap off screen
x_correction = -(frame_size-tick_mark_width*2-tick_mark_rendered_value_size[0])
y_correction = (size[1] / 100)
else:
x_correction = 0
y_correction = 0
surface.blit(tick_mark_rendered_value,(frame_size-tick_mark_width*2-tick_mark_rendered_value_size[0]+x_correction,tick_mark_placement-tick_mark_rendered_value_size[1]/2 - y_correction))
if unit not in ["","date"]:
unit_rendered_value = global_variables.standard_font.render(unit,True,(0,0,0))
unit_rendered_value_size = global_variables.standard_font.size(unit)
surface.blit(unit_rendered_value,(frame_size - unit_rendered_value_size[0], frame_size - unit_rendered_value_size[1]-(size[1] / 50)))
return surface
def import_datasheet(data_file_name):
data_file = open(data_file_name)
database = {}
headers = data_file.readline()
headers = headers.split("\t")
headers[-1] = headers[-1].rstrip("\n")
headers[-1] = headers[-1].rstrip("\r")
data_types = data_file.readline()
data_types = data_types.split("\t")
if data_types[0] == "explanation":
data_types = data_file.readline()
data_types = data_types.split("\t")
#print data_types
data_types[-1] = data_types[-1].rstrip("\n")
data_types[-1] = data_types[-1].rstrip("\r")
for line in data_file.readlines():
splitline = line.split("\t")
splitline[-1] = splitline[-1].rstrip("\n")
splitline[-1] = splitline[-1].rstrip("\r")
single_entry_data = {}
for i in range(1,len(splitline)):
if data_types[i] == "int":
if splitline[i] == "NA":
single_entry_data[headers[i]] = None
else:
single_entry_data[headers[i]] = int(splitline[i])
elif data_types[i] == "float":
if splitline[i] == "NA":
single_entry_data[headers[i]] = None
else:
single_entry_data[headers[i]] = float(splitline[i])
elif data_types[i] == "string":
if splitline[i] == "NA":
single_entry_data[headers[i]] = None
else:
single_entry_data[headers[i]] = str(splitline[i])
elif data_types[i] == "logical":
if splitline[i] == "NA":
single_entry_data[headers[i]] = None
elif splitline[i] == "True":
single_entry_data[headers[i]] = True
elif splitline[i] == "False":
single_entry_data[headers[i]] = False
else:
print("Problem with: " + str(headers[i]))
elif data_types[i] == "tuple":
if splitline[i] == "NA":
single_entry_data[headers[i]] = None
else:
raw_read = str(splitline[i])
split_read = raw_read.split(" ")
for j in range(0,len(split_read)):
split_read[j] = int(split_read[j])
tuple_read = tuple(split_read)
single_entry_data[headers[i]] = tuple_read
else:
print("Problem with: " + str(headers[i]))
database[splitline[0]] = single_entry_data
data_file.close()
#if verbose:
#print database
return database
def flatten(lst):
list = []
for elem in lst:
for i in elem:
list.append(i)
return list
def listify_tabular_data(data,size,manager,sort_by="rownames",column_order=None,interval = (0,200),reverse_sort=False):
"""
Function that takes tabular data of the form imported by primitives.import_datasheet (a dictionary with rows as keys and values being another dictionary were colums are keys and values are data entries)
returns a surface with a scrolled list with the data, which can be rendered directly on screen.
Optional arguments:
sort_by A string .If given the table will be sorted by this column name. Defaults to sorting by row-title name
column_order a list. If given the columns will appear in this order. Use 'rownames' to refer to the rownames. Omitted entries will not be in the list.
"""
print("1")
#checking that the column_order is correct
collection = ListItemCollection()
original_columns = ["rownames"] + list(data[list(data.keys())[0]].keys())
if column_order is None:
column_order = original_columns
else:
for column_name in column_order:
if column_name not in original_columns:
raise Exception("Recieved a column_order entry that was not located in data columns")
print("2")
#determining the max number of letters in each column
max_letters_per_column = {"rownames":0}
for column_name in column_order:
max_letters_per_column[column_name] = 0
for row in data:
for column_name in column_order:
if column_name == "rownames":
entry = str(row)
else:
entry = str(data[row][column_name])
max_letters_per_column[column_name] = max(max_letters_per_column[column_name],len(entry))
for max_letters_per_column_entry in max_letters_per_column:
max_letters_per_column[max_letters_per_column_entry] = max_letters_per_column[max_letters_per_column_entry] + 2
print("3")
#creating the sorting buttons - this is a time lag thing.
sorting_button_frame = HFrame()
sorting_buttons = {}
seperator = " "
for column_name in column_order:
sorting_buttons[column_name] = Button(column_name)
textSize = global_variables.courier_font.size(seperator[0:max_letters_per_column[column_name]])
sorting_buttons[column_name].set_minimum_size(textSize[0],textSize[1])
sorting_button_frame.add_child(sorting_buttons[column_name])
if textSize[0] != sorting_buttons[column_name].size[0]:
max_letters_per_column[column_name] = sorting_buttons[column_name].size[0] / 10
print("4")
#sorting the rows according to sort_by
if sort_by not in column_order:
print(column_order)
raise Exception("The sort_by variable was not found in the column_order. Remember the rownames must also be present if needed")
if sort_by == "rownames":
sorting_list = list(data.keys())
sorting_list.sort()
else:
temp_dict = {}
#from http://mail.python.org/pipermail/python-list/2002-May/146190.html - thanks xtian
for row in data:
temp_dict[row] = data[row][sort_by]
def sorter(x, y):
return cmp(x[1],y[1])
i = list(temp_dict.items())
i.sort(sorter)
sorting_list = []
for i_entry in i:
sorting_list.append(i_entry[0])
if reverse_sort:
sorting_list.reverse()
print("5")
create_cutoff_buttons = False
if interval[0] < 0:
raise Exception("Can't give an interval below zero")
elif interval[0] == 0:
pass
else:
create_cutoff_buttons = True
print("set create_cutoff_buttons true because interval[0] was above 0")
if len(sorting_list) < interval[1]:
interval = (interval[0],len(sorting_list))
elif len(sorting_list) == interval[1]:
pass
else:
create_cutoff_buttons = True
print("set create_cutoff_buttons true because interval[1] was less than len(sorting_list)")
print("6")
#cutoff_buttons:
if create_cutoff_buttons:
up_button = Button("Previous page")
down_button = Button("Next page")
if interval[0] == 0:
up_button.sensitive = False
if interval[1] == len(sorting_list):
down_button.sensitive = False
cutoff_frame = HFrame()
cutoff_frame.add_child(up_button,down_button)
sorting_list = sorting_list[interval[0]:interval[1]]
print("7")
for rowname in sorting_list:
rowstring = ""
for column_entry in column_order:
if column_entry == "rownames":
data_point_here = rowname
else:
data_point_here = str(data[rowname][column_entry])
#print len(data_point_here)
seperator = " "
seperator = seperator[0:(max_letters_per_column[column_entry] - len(data_point_here))]
rowstring = rowstring + data_point_here + seperator
#rowlistitem = TextListItem(rowstring)
#rowlistitem.get_style()['font']['name'] = "Courier"
collection.append(rowstring)
print("8")
list = fast_list()
print("8.1")
#list.set_selectionmode()
print("8.2")
#print collection
#list.items = collection
print("9")
super_frame = VFrame()
super_frame.align = ALIGN_LEFT
super_frame.add_child(sorting_button_frame,list)
if create_cutoff_buttons:
super_frame.add_child(cutoff_frame)
print("10")
return super_frame
#
#
#def present_tabular_data(data,size,sort_by="rownames",column_order=None):
# """
# Wrapper around listify_tabular_data
# Like that, it takes tabular data of the form imported by primitives.import_datasheet (a dictionary with rows as keys and values being another dictionary were colums are keys and values are data entries)
# but this will return a frame with the output of listify_tabular_data, and buttons for each column_order entry that can be used to sort the list.
# Optional arguments:
# sort_by A string .If given the table will be sorted by this column name. Defaults to sorting by row-title name
# column_order a list. If given the columns will appear in this order. Use 'rownames' to refer to the rownames. Omitted entries will not be in the list.
# """
#
# HFrame()
#
# HFrame.addwidget()
#
#
def nicefy_numbers(number):
"""
Takes a number and returns a string that has had added billion or trillion or whatever
"""
if not (isinstance(number,int) or isinstance(number,int) or isinstance(number,float)):
raise Exception("The received number was not of required class int, long or float")
if isinstance(number,float):
if abs(number) > 1000:
number = int(number)
else:
number = "%.4g" % number
if isinstance(number,int) or isinstance(number,int):
if abs(number) > 1000*1000*1000*1000*1000*3:
number = "%.4g" % number
elif abs(number) > 1000*1000*1000*1000*3:
number = str(int(number / (1000*1000*1000*1000) )) + " trillion"
elif abs(number) > 1000*1000*1000*3:
number = str(int(number / (1000*1000*1000) )) + " billion"
elif abs(number) > 1000*1000*3:
number = str(int(number / (1000*1000) )) + " million"
elif abs(number) > 1000*3:
number = str(int(number / 1000)) + " thousand"
else:
number = str(number)
return number