osm_poi_matchmaker.libs.opening_hours   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 102
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 29

6 Methods

Rating   Name   Duplication   Size   Complexity  
A OpeningHours.__init__() 0 27 3
A OpeningHours.public_holiday_open() 0 3 1
A OpeningHours.nonstop() 0 3 1
A OpeningHours.lunch_break_start() 0 8 1
A OpeningHours.lunch_break_stop() 0 8 1
F OpeningHours.process() 0 52 18
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
introduced by
Class 'OpeningHours' inherits from object, can be safely removed from bases in python3
Loading history...
best-practice introduced by
Too many instance attributes (9/7)
Loading history...
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,
0 ignored issues
show
best-practice introduced by
Too many arguments (33/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (37/15).
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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():
0 ignored issues
show
Coding Style Naming introduced by
Variable name "v" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
38
            same = self.df_oh.loc[
39
                (self.df_oh['open'] == v['open']) & (self.df_oh['close'] == v['close'])].index.tolist()
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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'])][
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
42
                    'did'].tolist()
43
                self.df_dup.at[k, 'same'] = collections.OrderedDict(zip(same_id, same))
44
45
    @property
46
    def nonstop(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
98
        oh = ''
0 ignored issues
show
Coding Style Naming introduced by
Variable name "oh" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
99
        oh_list = []
100
        for k, v in self.df_dup.iterrows():
0 ignored issues
show
Coding Style Naming introduced by
Variable name "v" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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']))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
137
                                                    self.__lunch_break_stop, self.df_dup.at[k, 'close']))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
138
                oh = '; '.join(oh_list)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "oh" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
139
                oh = oh + oh_ph
0 ignored issues
show
Coding Style Naming introduced by
Variable name "oh" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
introduced by
The variable oh_ph does not seem to be defined for all execution paths.
Loading history...
143
            except:
0 ignored issues
show
Coding Style Best Practice introduced by
General except handlers without types should be used sparingly.

Typically, you would use general except handlers when you intend to specifically handle all types of errors, f.e. when logging. Otherwise, such general error handlers can mask errors in your application that you want to know of.

Loading history...
144
                return '24/7'
145
        elif oh_list == []:
146
            return None
147
        else:
148
            return oh
149