forked from OpenClassrooms-Student-Center/Python_Testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
49 lines (35 loc) · 1.34 KB
/
utils.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
import json
from datetime import datetime
def load_clubs():
with open('clubs.json') as c:
list_of_clubs = json.load(c)['clubs']
return list_of_clubs
def load_competitions():
with open('competitions.json') as comps:
list_of_competitions = json.load(comps)['competitions']
return list_of_competitions
def sort_competitions_date(comps):
past = []
present = []
for comp in comps:
comp_date = datetime.strptime(comp['date'], '%Y-%m-%d %H:%M:%S')
if comp_date < datetime.now():
past.append(comp)
elif comp_date >= datetime.now():
present.append(comp)
return past, present
def initialize_booked_places(comps, clubs_list):
places = []
for comp in comps:
for club in clubs_list:
places.append({'competition': comp['name'], 'booked': [0, club['name']]})
return places
def update_booked_places(competition, club, places_booked, places_required):
for item in places_booked:
if item['competition'] == competition['name']:
if item['booked'][1] == club['name'] and item['booked'][0] + places_required <= 12:
item['booked'][0] += places_required
break
else:
raise ValueError("You can't book more than 12 places in a competition.")
return places_booked