--- a/misc.py Tue Jun 20 09:39:42 2017 +0300 +++ b/misc.py Tue Jun 20 10:27:52 2017 +0300 @@ -1,25 +1,25 @@ from datetime import datetime, date, time, timedelta -def tänään(): +def today(): return date.today() #return date(2017, 1, 10) -def nyt(): +def now(): return datetime.now() - #return datetime.combine(tänään(), datetime.now().time()) + #return datetime.combine(today(), datetime.now().time()) -def lue_csv(tiedosto, muunnokset = None): +def read_csv(file, transformations = None): import csv - lukija = csv.reader(tiedosto) - otsakkeet = next(lukija) - for i in range(len(otsakkeet)): - otsakkeet[i] = otsakkeet[i].replace('\ufeff', '').strip() + lukija = csv.reader(file) + keys = next(lukija) + for i in range(len(keys)): + keys[i] = keys[i].replace('\ufeff', '').strip() for rivi in lukija: - tietue = dict(zip(otsakkeet, rivi)) - if muunnokset: - for avain, muunnos in muunnokset.items(): - tietue[avain] = muunnos(tietue[avain]) - yield tietue + entry = dict(zip(keys, rivi)) + if transformations: + for key, transformation in transformations.items(): + entry[key] = transformation(entry[key]) + yield entry def is_night_time(time): return time.hour >= 23 or time.hour < 5