-
Notifications
You must be signed in to change notification settings - Fork 0
/
timezone.py
94 lines (79 loc) · 2.94 KB
/
timezone.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
# timezone hausaufgabe
# https://github.com/likyng/pyha/blob/master/timezone.py
def readFile(filename):
import csv
# context manager takes care of closing the file regardless of errors.
try:
with open(filename, "r") as file:
data = file.readlines()
except IOError:
print("Die Datei existiert nicht")
except ValueError as e:
print("Die Daten lassen sich nicht korrekt verarbeiten:", e)
except:
print("Irgendetwas anderes lief schief")
readData = csv.reader(data)
#transform in list
list = []
for entry in readData:
list.append(entry)
return list
def numzones_per_country():
result = {}
countries = readFile("zone.csv")
for element in countries:
if str(element[1]) in result:
result[str(element[1])] += 1
else:
result[str(element[1])] = 1
print(result)
numzones_per_country()
def numzones_per_continent():
result = {}
continents = readFile("zone.csv")
for element in continents:
if str(element[2]).split('/', maxsplit = 1)[0] in result:
result[str(element[2]).split('/', maxsplit = 1)[0]] += 1
else:
result[str(element[2]).split('/', maxsplit = 1)[0]] = 1
print(result)
numzones_per_continent()
# timezone hausaufgabe nr 2 (fortgeschrittene)
def zone_countries():
import time
result = {}
#read needed files
timezones = readFile("timezone.csv")
zone = readFile("zone.csv")
country = dict(readFile("country.csv"))
temp = [int(timezones[0][0]), timezones[0][1], timezones[0][2]]
for region in timezones:
#if latest timezone found, skip future entries
if int(region[0]) < temp[0]:
continue
#if no time for timezone specified, skip
elif region[2] == "":
continue
#checking if entry is older than time()
elif int(region[0]) == temp[0] and float(region[2]) <= time.time():
temp[1] = region[1] #copy timezone
temp[2] = region[2] #copy timestamp
#if newer than time(), use entry from step before
elif int(region[0]) == temp[0] and float(region[2]) >= time.time():
if str(temp[1]) in result:
result[str(temp[1])] += ", %s" % country[zone[temp[0]-1][1]]
#replaces the abbreviation with the complete name
else:
result[str(region[1])] = country[zone[temp[0]-1][1]]
temp[0] += 1 #going to next ID
#last entry is older than time() and will be used
else:
temp[1] = region[1] #go to next country
temp[2] = region[2]
if str(temp[1]) in result:
result[str(temp[1])] += ", %s" % country[(zone[temp[0]-1][1])]
else:
result[str(region[1])] = country[zone[temp[0]-1][1]]
temp[0] += 1
print(result)
zone_countries()