]> AND Private Git Repository - predictops.git/blob - predictops/source/ramadan.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Refactoring, fin du lever/coucher de soleil, et début de sentinelles
[predictops.git] / predictops / source / ramadan.py
1 from configparser import ConfigParser
2 from convertdate import islamic
3 from datetime import datetime, timedelta
4 from logging import getLogger
5 from logging.config import fileConfig
6 from pathlib import Path
7
8
9 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
10 logger = getLogger()
11
12
13 class Ramadan:
14
15     _start = None
16     _end = None
17
18     def __init__(self, config_file):
19
20         self._config = ConfigParser()
21         self._config.read(config_file)
22
23         # Collecting holidays features
24         self._features = [section for section in self._config
25                           if self._config[section].getboolean('binary')
26                           or self._config[section].getboolean('categorical')
27                           or self._config[section].getboolean('numerical')]
28
29         self._dated_features = {}
30
31     @property
32     def start(self):
33         return self._start
34
35     @start.setter
36     def start(self, x):
37         self._start = x
38
39     @property
40     def end(self):
41         return self._end
42
43     @end.setter
44     def end(self, x):
45         self._end = x
46
47     @property
48     def dated_features(self):
49         if self._dated_features == {}:
50             logger.info("Adding Ramadan features")
51             date = self._start
52             while date <= self._end:
53                 year, month, day = date.year, date.month, date.day
54                 eve = datetime(year, month, day) - timedelta(days=1)
55                 tomorrow = datetime(year, month, day) + timedelta(days=1)
56                 Hegirian_month = islamic.from_gregorian(year, month, day)[1]
57                 dict_hour = {
58                     'ramadanEve': False,
59                     'ramadan': False,
60                     'ramadanDayAfter': False
61                 }
62                 if Hegirian_month == 8 and\
63                    islamic.from_gregorian(tomorrow.year, tomorrow.month, tomorrow.day)[1] == 9:
64                     dict_hour['ramadanEve'] = True
65                 elif Hegirian_month == 9:
66                     dict_hour['ramadan'] = True
67                 elif Hegirian_month == 10 and\
68                         islamic.from_gregorian(eve.year, eve.month, eve.day)[1] == 9:
69                     dict_hour['ramadanDayAfter'] = True
70                 self._dated_features[date] = dict_hour
71                 date += timedelta(hours=1)
72         return self._dated_features