Conditions | 8 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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:
1 | from datetime import datetime |
||
104 | def poll(self): |
||
105 | # This is where the crux of the sensor work goes. |
||
106 | # This is called every self._poll_interval. |
||
107 | # For example, let's assume you want to query ec2 and get |
||
108 | # health information about your instances: |
||
109 | # some_data = aws_client.get('') |
||
110 | # payload = self._to_payload(some_data) |
||
111 | # # _to_triggers is something you'd write to convert the data format you have |
||
112 | # # into a standard python dictionary. This should follow the payload schema |
||
113 | # # registered for the trigger. |
||
114 | # self.sensor_service.dispatch(trigger, payload) |
||
115 | # # You can refer to the trigger as dict |
||
116 | # # { "name": ${trigger_name}, "pack": ${trigger_pack} } |
||
117 | # # or just simply by reference as string. |
||
118 | # # i.e. dispatch(${trigger_pack}.${trigger_name}, payload) |
||
119 | # # E.g.: dispatch('examples.foo_sensor', {'k1': 'stuff', 'k2': 'foo'}) |
||
120 | # # trace_tag is a tag you would like to associate with the dispacthed TriggerInstance |
||
121 | # # Typically the trace_tag is unique and a reference to an external event. |
||
122 | |||
123 | for hostname, device_obj in self.devices.items(): |
||
124 | |||
125 | try: |
||
126 | last_bgp_peers = self.device_state[hostname]["last_bgp_peers"] |
||
127 | except KeyError: |
||
128 | |||
129 | # Get current BGP peers (instead of setting to 0 and |
||
130 | # triggering every time sensor starts initially) |
||
131 | try: |
||
132 | self.device_state[hostname] = { |
||
133 | "last_bgp_peers": self.get_number_of_peers(device_obj) |
||
134 | } |
||
135 | continue |
||
136 | # Any connection-related exception raised here is |
||
137 | # driver-specific, so we have to catch "Exception" |
||
138 | except Exception as e: |
||
139 | self._logger.debug("Caught exception on connect: %s" % e) |
||
140 | continue |
||
141 | |||
142 | try: |
||
143 | this_bgp_peers = self.get_number_of_peers(device_obj) |
||
144 | # Any connection-related exception raised here is |
||
145 | # driver-specific, so we have to catch "Exception" |
||
146 | except Exception as e: |
||
147 | self._logger.debug("Caught exception on get peers: %s" % e) |
||
148 | continue |
||
149 | |||
150 | if this_bgp_peers > last_bgp_peers: |
||
151 | self._logger.info( |
||
152 | "Peer count went UP to %s" % str(this_bgp_peers) |
||
153 | ) |
||
154 | self._bgp_peer_trigger(BGP_PEER_INCREASE, hostname, |
||
155 | last_bgp_peers, this_bgp_peers) |
||
156 | |||
157 | elif this_bgp_peers < last_bgp_peers: |
||
158 | self._logger.info( |
||
159 | "BGP neighbors went DOWN to %s" % str(this_bgp_peers) |
||
160 | ) |
||
161 | |||
162 | self._bgp_peer_trigger(BGP_PEER_DECREASE, hostname, |
||
163 | last_bgp_peers, this_bgp_peers) |
||
164 | |||
165 | elif this_bgp_peers == last_bgp_peers: |
||
166 | self._logger.info( |
||
167 | "BGP neighbors STAYED at %s" % str(this_bgp_peers) |
||
168 | ) |
||
169 | |||
170 | # Save this state for the next poll |
||
171 | self.device_state[hostname]["last_bgp_peers"] = this_bgp_peers |
||
172 | |||
225 |