time_tick()   F
last analyzed

Complexity

Conditions 18

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
dl 0
loc 51
rs 2.882
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like time_tick() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
logger = logging.getLogger(__name__)
6
logger.debug("%s loaded", __name__)
7
8
from doorpi.action.base import SingleAction
9
import time
10
import datetime
11
import doorpi
12
13
MINUTE_RANGE = range(0, 60)
14
HOUR_RANGE = range(0, 23)
15
16
last_time_tick_second = 0
17
18
def destroy_time_tick():
19
    doorpi.DoorPi().event_handler.unregister_source(__name__, True)
20
21
def time_tick(last_tick):
0 ignored issues
show
Unused Code introduced by Thomas Meissner
The argument last_tick seems to be unused.
Loading history...
22
23
    timestamp_now = time.time()
24
    timestamp_past = last_time_tick_second
25
26
    datetime_now = datetime.datetime.fromtimestamp(timestamp_now)
27
    datetime_past = datetime.datetime.fromtimestamp(timestamp_past)
28
29
    if datetime_now.year != datetime_past.year:
30
        doorpi.DoorPi().event_handler('OnTimeYear', __name__)
31
        if datetime_now.year % 2 is 0: doorpi.DoorPi().event_handler('OnTimeYearEvenNumber', __name__)
32
        else: doorpi.DoorPi().event_handler('OnTimeYearUnevenNumber', __name__)
33
34
    if datetime_now.month != datetime_past.month:
35
        doorpi.DoorPi().event_handler('OnTimeMonth', __name__)
36
        if datetime_now.month % 2 is 0: doorpi.DoorPi().event_handler('OnTimeMonthEvenNumber', __name__)
37
        else: doorpi.DoorPi().event_handler('OnTimeMonthUnevenNumber', __name__)
38
39
    if datetime_now.day != datetime_past.day:
40
        doorpi.DoorPi().event_handler('OnTimeDay', __name__)
41
        if datetime_now.day % 2 is 0: doorpi.DoorPi().event_handler('OnTimeDayEvenNumber', __name__)
42
        else: doorpi.DoorPi().event_handler('OnTimeDayUnevenNumber', __name__)
43
44
    if datetime_now.hour != datetime_past.hour:
45
        doorpi.DoorPi().event_handler('OnTimeHour', __name__)
46
        if datetime_now.hour % 2 is 0: doorpi.DoorPi().event_handler('OnTimeHourEvenNumber', __name__)
47
        else: doorpi.DoorPi().event_handler('OnTimeHourUnevenNumber', __name__)
48
49
        for hour in HOUR_RANGE:
50
            if hour is datetime_now.hour: doorpi.DoorPi().event_handler('OnTimeHour%s'%hour, __name__)
51
52
    if datetime_now.minute != datetime_past.minute:
53
        doorpi.DoorPi().event_handler('OnTimeMinute', __name__)
54
        if datetime_now.minute % 2 is 0: doorpi.DoorPi().event_handler('OnTimeMinuteEvenNumber', __name__)
55
        else: doorpi.DoorPi().event_handler('OnTimeMinuteUnevenNumber', __name__)
56
57
        for minute in MINUTE_RANGE:
58
            if minute is datetime_now.minute: doorpi.DoorPi().event_handler('OnTimeMinute%s'%minute, __name__)
59
60
        if datetime_now.minute % 5 is 0: doorpi.DoorPi().event_handler('OnTimeMinuteEvery5', __name__)
61
62
    if datetime_now.second != datetime_past.second:
63
        doorpi.DoorPi().event_handler('OnTimeSecond', __name__)
64
        if datetime_now.second % 2 is 0: doorpi.DoorPi().event_handler('OnTimeSecondEvenNumber', __name__)
65
        else: doorpi.DoorPi().event_handler('OnTimeSecondUnevenNumber', __name__)
66
67
    global last_time_tick_second
0 ignored issues
show
Coding Style introduced by Thomas Meissner
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
68
    last_time_tick_second = timestamp_now
69
    #doorpi.DoorPi().event_handler('OnTimeTick', __name__)
70
71
    return True
72
73
def get(parameters):
74
    parameter_list = parameters.split(',')
75
    if len(parameter_list) is not 1: return None
76
77
    last_tick = parameter_list[0]
78
79
    # register timebased_events
80
    doorpi.DoorPi().event_handler.register_event('OnTimeSecond', __name__)
81
    doorpi.DoorPi().event_handler.register_event('OnTimeSecondEvenNumber', __name__)
82
    doorpi.DoorPi().event_handler.register_event('OnTimeSecondUnevenNumber', __name__)
83
84
    doorpi.DoorPi().event_handler.register_event('OnTimeMinute', __name__)
85
    doorpi.DoorPi().event_handler.register_event('OnTimeMinuteEvenNumber', __name__)
86
    doorpi.DoorPi().event_handler.register_event('OnTimeMinuteUnevenNumber', __name__)
87
    for minute in MINUTE_RANGE:
88
        doorpi.DoorPi().event_handler.register_event('OnTimeMinute%s'%minute, __name__)
89
    doorpi.DoorPi().event_handler.register_event('OnTimeMinuteEvery5', __name__)
90
91
    doorpi.DoorPi().event_handler.register_event('OnTimeHour', __name__)
92
    doorpi.DoorPi().event_handler.register_event('OnTimeHourEvenNumber', __name__)
93
    doorpi.DoorPi().event_handler.register_event('OnTimeHourUnevenNumber', __name__)
94
    for hour in HOUR_RANGE:
95
        doorpi.DoorPi().event_handler.register_event('OnTimeHour%s'%hour, __name__)
96
97
    doorpi.DoorPi().event_handler.register_event('OnTimeDay', __name__)
98
    doorpi.DoorPi().event_handler.register_event('OnTimeDayEvenNumber', __name__)
99
    doorpi.DoorPi().event_handler.register_event('OnTimeDayUnevenNumber', __name__)
100
101
    doorpi.DoorPi().event_handler.register_event('OnTimeWeek', __name__)
102
    doorpi.DoorPi().event_handler.register_event('OnTimeWeekEvenNumber', __name__)
103
    doorpi.DoorPi().event_handler.register_event('OnTimeWeekUnevenNumber', __name__)
104
105
    doorpi.DoorPi().event_handler.register_event('OnTimeMonth', __name__)
106
    doorpi.DoorPi().event_handler.register_event('OnTimeMonthEvenNumber', __name__)
107
    doorpi.DoorPi().event_handler.register_event('OnTimeMonthUnevenNumber', __name__)
108
109
    doorpi.DoorPi().event_handler.register_event('OnTimeYear', __name__)
110
    doorpi.DoorPi().event_handler.register_event('OnTimeYearEvenNumber', __name__)
111
    doorpi.DoorPi().event_handler.register_event('OnTimeYearUnevenNumber', __name__)
112
113
    doorpi.DoorPi().event_handler.register_action('OnShutdown', TimeTickDestroyAction(destroy_time_tick))
114
115
    return TimeTickAction(time_tick, last_tick)
116
117
class TimeTickAction(SingleAction): pass
118
class TimeTickDestroyAction(SingleAction): pass