Conditions | 10 |
Total Lines | 57 |
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.keyboard.GPIO.__init__() 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 |
||
19 | def __init__(self, input_pins, output_pins, conf_pre, conf_post, keyboard_name, |
||
20 | bouncetime=200, polarity=0, pressed_on_key_down=True, *args, **kwargs): |
||
21 | logger.debug("__init__(input_pins = %s, output_pins = %s, bouncetime = %s, polarity = %s)", |
||
22 | input_pins, output_pins, bouncetime, polarity) |
||
23 | self.keyboard_name = keyboard_name |
||
24 | self._polarity = polarity |
||
25 | self._InputPins = map(int, input_pins) |
||
26 | self._OutputPins = map(int, output_pins) |
||
27 | self._pressed_on_key_down = pressed_on_key_down |
||
28 | |||
29 | RPiGPIO.setwarnings(False) |
||
30 | |||
31 | section_name = conf_pre+'keyboard'+conf_post |
||
32 | if doorpi.DoorPi().config.get(section_name, 'mode', "BOARD").upper() == "BOARD": |
||
33 | RPiGPIO.setmode(RPiGPIO.BOARD) |
||
34 | else: |
||
35 | RPiGPIO.setmode(RPiGPIO.BCM) |
||
36 | |||
37 | # issue 134 |
||
38 | pull_up_down = doorpi.DoorPi().config.get(section_name, 'pull_up_down', "PUD_OFF").upper() |
||
39 | if pull_up_down == "PUD_DOWN": |
||
40 | pull_up_down = RPiGPIO.PUD_DOWN |
||
41 | elif pull_up_down == "PUD_UP": |
||
42 | pull_up_down = RPiGPIO.PUD_UP |
||
43 | else: |
||
44 | pull_up_down = RPiGPIO.PUD_OFF |
||
45 | |||
46 | # issue #133 |
||
47 | try: |
||
48 | RPiGPIO.setup(self._InputPins, RPiGPIO.IN, pull_up_down=pull_up_down) |
||
49 | except TypeError: |
||
50 | logger.warning('you use an old version of GPIO library - fallback to single-register of input pins') |
||
51 | for input_pin in self._InputPins: |
||
52 | RPiGPIO.setup(input_pin, RPiGPIO.IN, pull_up_down=pull_up_down) |
||
53 | |||
54 | for input_pin in self._InputPins: |
||
55 | RPiGPIO.add_event_detect( |
||
56 | input_pin, |
||
57 | RPiGPIO.BOTH, |
||
58 | callback=self.event_detect, |
||
59 | bouncetime=int(bouncetime) |
||
60 | ) |
||
61 | self._register_EVENTS_for_pin(input_pin, __name__) |
||
62 | |||
63 | # issue #133 |
||
64 | try: |
||
65 | RPiGPIO.setup(self._OutputPins, RPiGPIO.OUT) |
||
66 | except TypeError: |
||
67 | logger.warning('you use an old version of GPIO library - fallback to single-register of input pins') |
||
68 | for output_pin in self._OutputPins: |
||
69 | RPiGPIO.setup(output_pin, RPiGPIO.OUT) |
||
70 | |||
71 | # use set_output to register status @ dict self._OutputStatus |
||
72 | for output_pin in self._OutputPins: |
||
73 | self.set_output(output_pin, 0, False) |
||
74 | |||
75 | self.register_destroy_action() |
||
76 | |||
124 |