Conditions | 5 |
Total Lines | 54 |
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:
1 | from datetime import datetime |
||
23 | def setup(self): |
||
24 | # Need to get initial BGP RIB, neighbor table, etc. Put into "self". |
||
25 | # Then, write some logic within "poll" that checks again, and |
||
26 | # detects diffs |
||
27 | # Detects: |
||
28 | # - Diffs in BGP RIB (need to give a threshold like 100s or 1000s or |
||
29 | # routes different) |
||
30 | # (may want to not only store the previous result, but also the |
||
31 | # previous 10 or so and do a stddev calc) |
||
32 | |||
33 | # Stores number of BGP neighbors |
||
34 | # self.bgp_neighbors = 0 |
||
35 | |||
36 | # Stores RIB information in-between calls |
||
37 | # self.bgp_rib = {} |
||
38 | |||
39 | # Dictionary for tracking per-device known state |
||
40 | # Top-level keys are the management IPs sent to NAPALM, and |
||
41 | # information on each is contained below that |
||
42 | self.device_state = {} |
||
43 | |||
44 | napalm_config = self._config |
||
45 | |||
46 | # Assign sane defaults for configuration |
||
47 | default_opts = { |
||
48 | "opt1": "val1" |
||
49 | } |
||
50 | for opt_name, opt_val in default_opts.items(): |
||
51 | |||
52 | try: |
||
53 | |||
54 | # key exists but is set to nothing |
||
55 | if not napalm_config[opt_name]: |
||
56 | napalm_config[opt_name] == default_opts |
||
57 | |||
58 | except KeyError: |
||
59 | |||
60 | # key doesn't exist |
||
61 | napalm_config[opt_name] == default_opts |
||
62 | |||
63 | # Assign options to instance |
||
64 | self._devices = napalm_config['devices'] |
||
65 | |||
66 | # Generate dictionary of device objects per configuration |
||
67 | # IP Address(key): Device Object(value) |
||
68 | self.devices = { |
||
69 | str(device['host']): get_network_driver(device['driver'])( |
||
70 | hostname=str(device['host']), |
||
71 | username=device['username'], |
||
72 | password=device['password'], |
||
73 | optional_args={ |
||
74 | 'port': str(device['port']) |
||
75 | }) |
||
76 | for device in self._devices |
||
77 | } |
||
183 |