-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
89 lines (63 loc) · 2.15 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
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
import re
from datetime import datetime, timedelta
from data import USERS
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
TIME_REGEX = re.compile(r"(\d{1,2})[hH](\d{1,2})?")
def parse_hour(string: str, now=datetime.utcnow()) -> datetime:
match = TIME_REGEX.fullmatch(string)
if match is None:
return None
else:
hours, minutes = match.groups()
minutes = int(minutes or 0)
hours = int(hours)
date = datetime(year=now.year, month=now.month, day=now.day,
hour=hours, minute=minutes)
if now.hour > 15:
date += timedelta(days=1)
return date
assert parse_hour("9H30", now=datetime(year=1984, month=1, day=10, hour=6))\
== datetime(year=1984, month=1, day=10, hour=9, minute=30)
assert parse_hour("8h", now=datetime(year=1984, month=1, day=10, hour=18))\
== datetime(year=1984, month=1, day=11, hour=8, minute=00)
def add_dico(name, id, distance):
distance = int(distance)
USERS[id]= {"name": name, "distance": distance}
def time_to_int(string: str, identifiant: int):
'''Parse and substract the hour'''
date = parse_hour(string)
d = USERS[identifiant]["distance"]
date -= timedelta(minutes=d)
response = date.strftime('%HH%M')
return response
def belong(list: list, x: int):
if len(list) == 0:
return False
for i in range(len(list)):
if list[i] == x:
return True
return False
def message(list: list, string: str) -> str:
'''Create the final recap message.'''
msg = "Liste des participants pour " + str(string) + " : \n"
if len(list) == 0:
return "Il n'y a pas de participant"
for i in range(len(list)):
id = list[i]
t = time_to_int(string, id)
msg = msg + "<@" + str(id) + "> doit partir à **" + str(t) + "**\n"
return msg
def print_ruler():
print("====================")
def printd():
print("USERS = ", end='')
print(USERS)