res = a if a > b else b
# sorting dict elements to write to a file
with open('../stats/trips-per-week.txt', 'w') as outF:
h_sorted = sorted(tripsPerWeek.items(), key=lambda x: x[0])
for h in h_sorted:
d_sorted = sorted(h[1].items(), key=lambda x: x[0])
for d in d_sorted:
outF.write(str(h[0]) + '\t' + str(d[0]) + '\t' + str(d[1]) + '\n')
# pretty writing to a file using pprint
outF.write(pprint.pformat(tripsPerWeek))
# skip first line in file
with open('data.csv', 'r') as f_in:
next(f_in)
for l in f_in:
# timer
runTime=(time.mktime(datetime.datetime.now().timetuple()))
print("Done in "+str(time.mktime(datetime.datetime.now().timetuple())-runTime)+" seconds")
# iterator — instead of large lists
(х*х for x in data) # general syntax
if any(number < 10 for number in numbers): # will stop calculation if found first number < 10
# switch
keycode = 2
functions = {
1: key_1_pressed,
2: key_2_pressed,
3: key_3_pressed}
functions.get(keycode, unknown_key_pressed)() # includes default behavior
# nice way to return value if any
return se.pool.extract_parsed_url(url).vhost or 'empty'
# nice way to calculate values and write result
def reduce_suggestions(groups):
for key, records in groups:
suggestions = Counter()
for record in records:
tag = MARKUP.get(record.target, UNKNOWN)
suggestions[tag] += record.edge_weight
dump = json.dumps(OrderedDict(suggestions.most_common())).replace('"',"'")