Conditions | 5 |
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 |
||
34 | def setup(self): |
||
35 | # Setup stuff goes here. For example, you might establish connections |
||
36 | # to external system once and reuse it. This is called only once by the system. |
||
37 | |||
38 | # TODO Figure out how to use this and make a config.yaml |
||
39 | # |
||
40 | |||
41 | # database = database or self.config.get('default') |
||
42 | # db_config = self.config.get(database, {}) |
||
43 | # params = { |
||
44 | # 'database': db_config.get('database') or database, |
||
45 | # 'server': server or db_config.get('server'), |
||
46 | # 'user': user or db_config.get('user'), |
||
47 | # 'password': password or db_config.get('password') |
||
48 | # } |
||
49 | |||
50 | # Need to get initial BGP RIB, neighbor table, etc. Put into "self". |
||
51 | # Then, write some logic within "poll" that checks again, and detects diffs |
||
52 | # Detects: |
||
53 | # - Diffs in BGP RIB (need to give a threshold like 100s or 1000s or routes different) |
||
54 | # (may want to not only store the previous result, but also the previous 10 or so and do a stddev calc) |
||
55 | |||
56 | # Stores number of BGP neighbors |
||
57 | # self.bgp_neighbors = 0 |
||
58 | |||
59 | # Stores RIB information in-between calls |
||
60 | # self.bgp_rib = {} |
||
61 | |||
62 | # Dictionary for tracking per-device known state |
||
63 | # Top-level keys are the management IPs sent to NAPALM, and |
||
64 | # information on each is contained below that |
||
65 | self.device_state = {} |
||
66 | |||
67 | napalm_config = self._config |
||
68 | |||
69 | # Assign sane defaults for configuration |
||
70 | default_opts = { |
||
71 | "opt1": "val1" |
||
72 | } |
||
73 | for opt_name, opt_val in default_opts.items(): |
||
74 | |||
75 | try: |
||
76 | |||
77 | # key exists but is set to nothing |
||
78 | if not napalm_config[opt_name]: |
||
79 | napalm_config[opt_name] == default_opts |
||
80 | |||
81 | except KeyError: |
||
82 | |||
83 | # key doesn't exist |
||
84 | napalm_config[opt_name] == default_opts |
||
85 | |||
86 | # Assign options to instance |
||
87 | self._devices = napalm_config['devices'] |
||
88 | |||
89 | # Generate dictionary of device objects per configuration |
||
90 | # IP Address(key): Device Object(value) |
||
91 | # |
||
92 | # TODO(mierdin): Yes I know this looks terrible, I will fix it |
||
93 | self.devices = { |
||
94 | str(device['host']): get_network_driver(device['driver'])( |
||
95 | hostname=str(device['host']), |
||
96 | username=device['username'], |
||
97 | password=device['password'], |
||
98 | optional_args={ |
||
99 | 'port': str(device['port']) |
||
100 | }) |
||
101 | for device in self._devices |
||
102 | } |
||
225 |