Conditions | 18 |
Total Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 ReconnectingPacketBuffer.__run() 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 |
||
99 | def __run(self): |
||
100 | while self.running: |
||
101 | if not self.connected: |
||
102 | if not self.last_connect_attempt or time.time() - self.last_connect_attempt > self.reconnect_wait_time: |
||
103 | try: |
||
104 | self.last_connect_attempt = time.time() |
||
105 | self.packet_layer.connect(*self.connect_args, **self.connect_kwargs) |
||
106 | self.connected = True |
||
107 | except IOError: |
||
108 | echo_colorized_warning('Could not connect, will reattempt.') |
||
109 | try: |
||
110 | self.packet_layer.close() |
||
111 | except IOError: |
||
112 | pass |
||
113 | self.__increment_wait_time() |
||
114 | else: |
||
115 | time.sleep(1) |
||
116 | else: |
||
117 | io_occured = False |
||
118 | |||
119 | # lets attempt to read in a packet |
||
120 | try: |
||
121 | read_packet = self.packet_layer.read() |
||
122 | self.__reset_wait_time() |
||
123 | if read_packet: |
||
124 | with self.lock: |
||
125 | self.from_packet_layer[str(aprs_util.hash_frame(read_packet))] = read_packet |
||
126 | io_occured = True |
||
127 | except IOError: |
||
128 | echo_colorized_warning('Read failed. Will disconnect and attempt to reconnect.') |
||
129 | try: |
||
130 | self.packet_layer.close() |
||
131 | except IOError: |
||
132 | pass |
||
133 | self.connected = False |
||
134 | continue |
||
135 | |||
136 | # lets try to write a packet, if any are waiting. |
||
137 | write_packet = None |
||
138 | with self.lock: |
||
139 | if self.to_packet_layer: |
||
140 | write_packet = self.to_packet_layer.popitem()[1] |
||
141 | if write_packet: |
||
142 | try: |
||
143 | self.packet_layer.write(write_packet) |
||
144 | io_occured = True |
||
145 | self.__reset_wait_time() |
||
146 | except IOError: |
||
147 | echo_colorized_warning('Write failed. Will disconnect and attempt to reconnect.') |
||
148 | self.to_packet_layer[str(aprs_util.hash_frame(read_packet))] = write_packet |
||
149 | try: |
||
150 | self.packet_layer.close() |
||
151 | except IOError: |
||
152 | pass |
||
153 | self.connected = False |
||
154 | continue |
||
155 | |||
156 | if not io_occured: |
||
157 | time.sleep(1) |
||
158 | try: |
||
159 | self.packet_layer.close() |
||
160 | except IOError: |
||
161 | pass |
||
162 | |||
192 |