-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotal_duration.py
62 lines (42 loc) · 1.58 KB
/
total_duration.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
import second_task
import datetime
from collections import defaultdict
def count_time(data):
"""Counts total time for each person
:param data: data of people in JSON format
:return: dict of IDs persons
"""
for i in data:
time_date_to = datetime.datetime.strptime(i["To"], "%d.%m.%Y %H:%M:%S")
time_date_from = datetime.datetime.strptime(i["From"], "%d.%m.%Y %H:%M:%S")
total_time = time_date_to - time_date_from
i["Total_time"] = int(total_time.seconds / 60)
result = defaultdict(int)
for value in data:
result[value['Member1_ID']] += value['Total_time']
result[value['Member2_ID']] += value['Total_time']
return dict(result)
def search_persons(per, ids):
""" Searches persons by ID from small_data_persons.json
:param per data of persons
:param ids: data of IDs
:return: list of dicts
"""
output = []
for key, value in ids.items():
for i in per:
if key == i['ID']:
output.append({"Name": i["Name"], "ID": key, "Total_time": value})
return list(sorted(output, key=lambda x: x["Total_time"], reverse=True))
def main():
contacts_file = 'small_data_contacts.json'
persons_file = 'small_data_persons.json'
file = 'output.xlsx'
contacts = second_task.read_json(contacts_file)
persons = second_task.read_json(persons_file)
count_time(contacts)
wf = search_persons(persons, count_time(contacts))
second_task.write__to_excel(wf, file)
print(count_time(contacts))
if __name__ == '__main__':
main()