1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
try: |
4
|
|
|
import logging |
5
|
|
|
import sys |
6
|
|
|
import collections |
7
|
|
|
import pandas as pd |
8
|
|
|
except ImportError as err: |
9
|
|
|
logging.error('Error %s import module: %s', __name__, err) |
10
|
|
|
logging.exception('Exception occurred') |
11
|
|
|
|
12
|
|
|
sys.exit(128) |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class OpeningHours(object): |
|
|
|
|
16
|
|
|
|
17
|
|
|
def __init__(self, non_stop, mo_o, tu_o, we_o, th_o, fr_o, sa_o, su_o, mo_c, tu_c, we_c, th_c, |
|
|
|
|
18
|
|
|
fr_c, sa_c, su_c, summer_mo_o, summer_tu_o, summer_we_o, summer_th_o, summer_fr_o, |
19
|
|
|
summer_sa_o, summer_su_o, summer_mo_c, summer_tu_c, summer_we_c, summer_th_c, |
20
|
|
|
summer_fr_c, summer_sa_c, summer_su_c, lb_start, lb_stop, public_holiday_open=None): |
|
|
|
|
21
|
|
|
self.__non_stop = non_stop |
22
|
|
|
self.opening_hours = {'mo': [mo_o, mo_c, summer_mo_o, summer_mo_c, 0], |
23
|
|
|
'tu': [tu_o, tu_c, summer_tu_o, summer_tu_c, 1], |
24
|
|
|
'we': [we_o, we_c, summer_we_o, summer_we_c, 2], |
25
|
|
|
'th': [th_o, th_c, summer_th_o, summer_th_c, 3], |
26
|
|
|
'fr': [fr_o, fr_c, summer_fr_o, summer_fr_c, 4], |
27
|
|
|
'sa': [sa_o, sa_c, summer_sa_o, summer_sa_c, 5], |
28
|
|
|
'su': [su_o, su_c, summer_su_o, summer_su_c, 6]} |
29
|
|
|
self.__lunch_break_start = lb_start |
30
|
|
|
self.__lunch_break_stop = lb_stop |
31
|
|
|
self.week_days = {0: 'mo', 1: 'tu', 2: 'we', 3: 'th', 4: 'fr', 5: 'sa', 6: 'su'} |
32
|
|
|
self.oh_types = ('open', 'close', 'summer_open', 'summer_close', 'did') |
33
|
|
|
self.df_oh = pd.DataFrame.from_dict(self.opening_hours, orient='index', columns=self.oh_types) |
|
|
|
|
34
|
|
|
self.df_dup = self.df_oh.sort_values('did').drop_duplicates(['open', 'close'], keep='first') |
35
|
|
|
self.df_dup['same'] = None |
36
|
|
|
self.__public_holiday_open = public_holiday_open |
37
|
|
|
for k, v in self.df_dup.iterrows(): |
|
|
|
|
38
|
|
|
same = self.df_oh.loc[ |
39
|
|
|
(self.df_oh['open'] == v['open']) & (self.df_oh['close'] == v['close'])].index.tolist() |
|
|
|
|
40
|
|
|
if same is not None: |
41
|
|
|
same_id = self.df_oh.loc[(self.df_oh['open'] == v['open']) & (self.df_oh['close'] == v['close'])][ |
|
|
|
|
42
|
|
|
'did'].tolist() |
43
|
|
|
self.df_dup.at[k, 'same'] = collections.OrderedDict(zip(same_id, same)) |
44
|
|
|
|
45
|
|
|
@property |
46
|
|
|
def nonstop(self): |
|
|
|
|
47
|
|
|
return self.__non_stop |
48
|
|
|
|
49
|
|
|
@nonstop.setter |
50
|
|
|
def nonstop(self, value): |
51
|
|
|
self.__non_stop = value |
52
|
|
|
|
53
|
|
|
@property |
54
|
|
|
def public_holiday_open(self): |
|
|
|
|
55
|
|
|
return self.__public_holiday_open |
56
|
|
|
|
57
|
|
|
@public_holiday_open.setter |
58
|
|
|
def public_holiday_open(self, value): |
59
|
|
|
self.__public_holiday_open = value |
60
|
|
|
|
61
|
|
|
@property |
62
|
|
|
def lunch_break_start(self) -> str: |
63
|
|
|
"""Get lunch break start for opening hours |
64
|
|
|
|
65
|
|
|
Returns: |
66
|
|
|
str: Stored value of launch break start, value like '12:00' |
67
|
|
|
""" |
68
|
|
|
return self.__lunch_break_start |
69
|
|
|
|
70
|
|
|
@lunch_break_start.setter |
71
|
|
|
def lunch_break_start(self, value: str): |
72
|
|
|
"""Set lunch break start for opening hours |
73
|
|
|
|
74
|
|
|
Args: |
75
|
|
|
data (str): Store value of launch break start with value like '12:00' |
76
|
|
|
""" |
77
|
|
|
self.__lunch_break_start = value |
78
|
|
|
|
79
|
|
|
@property |
80
|
|
|
def lunch_break_stop(self) -> str: |
81
|
|
|
"""Get lunch break stop for opening hours |
82
|
|
|
|
83
|
|
|
Returns: |
84
|
|
|
str: Stored value of launch break stop, value like '12:30' |
85
|
|
|
""" |
86
|
|
|
return self.__lunch_break_stop |
87
|
|
|
|
88
|
|
|
@lunch_break_stop.setter |
89
|
|
|
def lunch_break_stop(self, value: str): |
90
|
|
|
"""Set lunch break stop for opening hours |
91
|
|
|
|
92
|
|
|
Args: |
93
|
|
|
data (str): Store value of launch break stop with value like '12:30' |
94
|
|
|
""" |
95
|
|
|
self.__lunch_break_stop = value |
96
|
|
|
|
97
|
|
|
def process(self): |
|
|
|
|
98
|
|
|
oh = '' |
|
|
|
|
99
|
|
|
oh_list = [] |
100
|
|
|
for k, v in self.df_dup.iterrows(): |
|
|
|
|
101
|
|
|
if v['open'] is not None and v['close'] is not None: |
102
|
|
|
# Order by week days |
103
|
|
|
ordered = collections.OrderedDict(sorted(v['same'].items(), key=lambda x: x[0])) |
104
|
|
|
same = list(ordered.values()) |
105
|
|
|
# Public Holidays |
106
|
|
|
if self.__public_holiday_open is None: |
107
|
|
|
oh_ph = '' |
108
|
|
|
elif self.__public_holiday_open is True: |
109
|
|
|
oh_ph = '; PH on' |
110
|
|
|
elif self.__public_holiday_open is False: |
111
|
|
|
oh_ph = '; PH off' |
112
|
|
|
else: |
113
|
|
|
oh_ph = '' |
114
|
|
|
# Try to merge days interval |
115
|
|
|
if len(ordered) >= 2: |
116
|
|
|
same_id = list(ordered.keys()) |
117
|
|
|
diffs = [same_id[i + 1] - same_id[i] for i in range(len(same_id) - 1)] |
118
|
|
|
# Diffs list contains only 1 to make day interval |
119
|
|
|
if diffs.count(1) == len(diffs): |
120
|
|
|
days = '{}-{}'.format(list(ordered.values())[0], list(ordered.values())[-1]) |
121
|
|
|
# Make list of days |
122
|
|
|
else: |
123
|
|
|
days = ','.join(same) |
124
|
|
|
# Make list of days |
125
|
|
|
else: |
126
|
|
|
days = ','.join(same) |
127
|
|
|
if self.__lunch_break_start is None and self.__lunch_break_stop is None: |
128
|
|
|
# If open and close are equals we handles as closed |
129
|
|
|
if self.df_dup.at[k, 'open'] != self.df_dup.at[k, 'close']: |
130
|
|
|
oh_list.append( |
131
|
|
|
'{} {}-{}'.format(days.title(), self.df_dup.at[k, 'open'], self.df_dup.at[k, 'close'])) |
|
|
|
|
132
|
|
|
else: |
133
|
|
|
# If open and close are equals we handles as closed |
134
|
|
|
if self.df_dup.at[k, 'open'] != self.df_dup.at[k, 'close']: |
135
|
|
|
oh_list.append( |
136
|
|
|
'{} {}-{},{}-{}'.format(days.title(), self.df_dup.at[k, 'open'], self.__lunch_break_start, |
|
|
|
|
137
|
|
|
self.__lunch_break_stop, self.df_dup.at[k, 'close'])) |
|
|
|
|
138
|
|
|
oh = '; '.join(oh_list) |
|
|
|
|
139
|
|
|
oh = oh + oh_ph |
|
|
|
|
140
|
|
|
if self.__non_stop is True or 'Mo-Su 00:00-24:00' in oh: |
141
|
|
|
try: |
142
|
|
|
return '24/7{}'.format(oh_ph) |
|
|
|
|
143
|
|
|
except: |
|
|
|
|
144
|
|
|
return '24/7' |
145
|
|
|
elif oh_list == []: |
146
|
|
|
return None |
147
|
|
|
else: |
148
|
|
|
return oh |
149
|
|
|
|