Conditions | 18 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
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:
If many parameters/temporary variables are present:
Complex classes like doorpi.action.SingleActions.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 |
||
21 | def time_tick(last_tick): |
||
|
|||
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 |
||
68 | last_time_tick_second = timestamp_now |
||
69 | #doorpi.DoorPi().event_handler('OnTimeTick', __name__) |
||
70 | |||
71 | return True |
||
72 | |||
118 | class TimeTickDestroyAction(SingleAction): pass |