Coverage for src/botadi/mokadi/mokadi_helper.py: 81%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Small functions.
5"""
6import datetime
9def convert_into_days(date, lang="fr", format="%Y-%m-%dT%H:%M:%S"):
10 """
11 Convert a date into the number of days from today.
13 @param date str or datetime
14 @param lang language
15 @param format format to use
16 @return string
17 """
18 if lang != "fr":
19 raise NotImplementedError(
20 "Language '{0}' is not supported.".format(lang))
21 now = datetime.datetime.now()
22 if isinstance(date, str):
23 date = datetime.datetime.strptime(date, format)
24 delta = now - date
25 delay = delta.days
26 if delay == 0:
27 hour = delta.seconds // 3600
28 if hour <= 1:
29 return "il y a moins d'une heure"
30 elif hour <= 2:
31 return "il y a deux heures"
32 elif hour <= 3:
33 return "il y a trois heures"
34 elif hour <= 6:
35 return "il y a six heures"
36 elif hour <= 12:
37 return "il y a douze heures"
38 else:
39 return "aujourd'hui"
40 elif delay == 1:
41 return "hier"
42 elif delay == 2:
43 return "avant-hier"
44 elif delay < 0:
45 return "dans le futur"
46 else:
47 return "il y a {0} jours".format(delay)
50_int_values = dict(un=1, deux=2, trois=3, quatre=4, cinq=5, six=6, sept=7,
51 huit=8, neuf=9, dix=10, onze=11, douze=12, vingt=20, cent=100,
52 cents=100, mille=1000)
55def parse_string_int(text: str) -> int:
56 """
57 Converts a string into an integer.
58 It can also be a string like *'un'*.
60 @param text text to convert
61 @return number
62 """
63 if not text:
64 raise ValueError("Text to convert cannot be empty.")
65 if not isinstance(text, str):
66 raise ValueError("Text to convert cannot be a string.")
67 try:
68 return int(text)
69 except ValueError as e:
70 if text in _int_values:
71 return _int_values[text]
72 raise ValueError(
73 "Unable to convert '{0}' into int.".format(text)) from e