Passed
Push — master ( a7e03b...19948d )
by KAMI
02:53
created

OpeningHours.lunch_break()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 (10/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, lunch_break_start, lunch_break_stop, public_holiday_open=None):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (119/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 = lunch_break_start
30
        self.lunch_break_stop = lunch_break_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.nonstop = 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:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
63
        return self.lunch_break_start
64
65
    @lunchbreak_start.setter
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable lunchbreak_start does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
Undefined variable 'lunchbreak_start'
Loading history...
66
    def lunch_break_start(self, data: str):
0 ignored issues
show
Bug introduced by
method already defined line 62
Loading history...
67
        self.lunch_break_start = data
68
69
    @property
70
    def lunch_break_stop(self) -> str:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
71
        return self.lunch_break_stop
72
73
    @lunch_break_stop.setter
74
    def lunch_break_stop(self, data: str):
75
        self.lunch_break_stop = data
76
77
    def process(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
78
        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...
79
        oh_list = []
80
        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...
81
            if v['open'] is not None and v['close'] is not None:
82
                # Order by week days
83
                ordered = collections.OrderedDict(sorted(v['same'].items(), key=lambda x: x[0]))
84
                same = list(ordered.values())
85
                # Public Holidays
86
                if self.__public_holiday_open is None:
87
                    oh_ph = ''
88
                elif self.__public_holiday_open is True:
89
                    oh_ph = '; PH on'
90
                elif self.__public_holiday_open is False:
91
                    oh_ph = '; PH off'
92
                else:
93
                    oh_ph = ''
94
                # Try to merge days interval
95
                if len(ordered) >= 2:
96
                    same_id = list(ordered.keys())
97
                    diffs = [same_id[i + 1] - same_id[i] for i in range(len(same_id) - 1)]
98
                    # Diffs list contains only 1 to make day interval
99
                    if diffs.count(1) == len(diffs):
100
                        days = '{}-{}'.format(list(ordered.values())[0], list(ordered.values())[-1])
101
                    # Make list of days
102
                    else:
103
                        days = ','.join(same)
104
                # Make list of days
105
                else:
106
                    days = ','.join(same)
107
                if self.lunchbreak['start'] is None and self.lunchbreak['stop'] is None:
0 ignored issues
show
Bug introduced by
The Instance of OpeningHours does not seem to have a member named lunchbreak.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
108
                    # If open and close are equals we handles as closed
109
                    if self.df_dup.at[k, 'open'] != self.df_dup.at[k, 'close']:
110
                        oh_list.append(
111
                            '{} {}-{}'.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...
112
                else:
113
                    # If open and close are equals we handles as closed
114
                    if self.df_dup.at[k, 'open'] != self.df_dup.at[k, 'close']:
115
                        oh_list.append(
116
                            '{} {}-{},{}-{}'.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 (116/100).

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

Loading history...
117
                                                    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 (103/100).

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

Loading history...
118
                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...
119
                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...
120
        if self.non_stop is True or 'Mo-Su 00:00-24:00' in oh:
121
            try:
122
                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...
123
            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...
124
                return '24/7'
125
        elif oh_list == []:
126
            return None
127
        else:
128
            return oh
129