misc.py

changeset 24
e6bdb9c54096
parent 21
6a0394d5a159
child 71
d2e19670b772
equal deleted inserted replaced
23:3a495bc4b7b5 24:e6bdb9c54096
1 from datetime import datetime, date, time, timedelta 1 from datetime import datetime, date, time, timedelta
2 2
3 def tänään(): 3 def today():
4 return date.today() 4 return date.today()
5 #return date(2017, 1, 10) 5 #return date(2017, 1, 10)
6 6
7 def nyt(): 7 def now():
8 return datetime.now() 8 return datetime.now()
9 #return datetime.combine(tänään(), datetime.now().time()) 9 #return datetime.combine(today(), datetime.now().time())
10 10
11 def lue_csv(tiedosto, muunnokset = None): 11 def read_csv(file, transformations = None):
12 import csv 12 import csv
13 lukija = csv.reader(tiedosto) 13 lukija = csv.reader(file)
14 otsakkeet = next(lukija) 14 keys = next(lukija)
15 for i in range(len(otsakkeet)): 15 for i in range(len(keys)):
16 otsakkeet[i] = otsakkeet[i].replace('\ufeff', '').strip() 16 keys[i] = keys[i].replace('\ufeff', '').strip()
17 for rivi in lukija: 17 for rivi in lukija:
18 tietue = dict(zip(otsakkeet, rivi)) 18 entry = dict(zip(keys, rivi))
19 if muunnokset: 19 if transformations:
20 for avain, muunnos in muunnokset.items(): 20 for key, transformation in transformations.items():
21 tietue[avain] = muunnos(tietue[avain]) 21 entry[key] = transformation(entry[key])
22 yield tietue 22 yield entry
23 23
24 def is_night_time(time): 24 def is_night_time(time):
25 return time.hour >= 23 or time.hour < 5 25 return time.hour >= 23 or time.hour < 5

mercurial