Conditions | 10 |
Total Lines | 85 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 glances.web_list.GlancesWebList.load() 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 | # -*- coding: utf-8 -*- |
||
43 | def load(self, config): |
||
44 | """Load the web list from the configuration file.""" |
||
45 | web_list = [] |
||
46 | |||
47 | if config is None: |
||
48 | logger.debug("No configuration file available. Cannot load ports list.") |
||
49 | elif not config.has_section(self._section): |
||
50 | logger.debug("No [%s] section in the configuration file. Cannot load ports list." % self._section) |
||
51 | else: |
||
52 | logger.debug("Start reading the [%s] section in the configuration file" % self._section) |
||
53 | |||
54 | refresh = int(config.get_value(self._section, 'refresh', default=self._default_refresh)) |
||
55 | timeout = int(config.get_value(self._section, 'timeout', default=self._default_timeout)) |
||
56 | |||
57 | # Read the web/url list |
||
58 | for i in range(1, 256): |
||
59 | new_web = {} |
||
60 | postfix = 'web_%s_' % str(i) |
||
61 | |||
62 | # Read mandatories configuration key: host |
||
63 | new_web['url'] = config.get_value(self._section, '%s%s' % (postfix, 'url')) |
||
64 | if new_web['url'] is None: |
||
65 | continue |
||
66 | url_parse = urlparse(new_web['url']) |
||
67 | if not bool(url_parse.scheme) or not bool(url_parse.netloc): |
||
68 | logger.error('Bad URL (%s) in the [%s] section of configuration file.' % (new_web['url'], |
||
69 | self._section)) |
||
70 | continue |
||
71 | |||
72 | # Read optionals configuration keys |
||
73 | # Default description is the URL without the http:// |
||
74 | new_web['description'] = config.get_value(self._section, |
||
75 | '%sdescription' % postfix, |
||
76 | default="%s" % url_parse.netloc) |
||
77 | |||
78 | # Default status |
||
79 | new_web['status'] = None |
||
80 | new_web['elapsed'] = 0 |
||
81 | |||
82 | # Refresh rate in second |
||
83 | new_web['refresh'] = refresh |
||
84 | |||
85 | # Timeout in second |
||
86 | new_web['timeout'] = int(config.get_value(self._section, |
||
87 | '%stimeout' % postfix, |
||
88 | default=timeout)) |
||
89 | |||
90 | # RTT warning |
||
91 | new_web['rtt_warning'] = config.get_value(self._section, |
||
92 | '%srtt_warning' % postfix, |
||
93 | default=None) |
||
94 | if new_web['rtt_warning'] is not None: |
||
95 | # Convert to second |
||
96 | new_web['rtt_warning'] = int(new_web['rtt_warning']) / 1000.0 |
||
97 | |||
98 | # Indice |
||
99 | new_web['indice'] = 'web_' + str(i) |
||
100 | |||
101 | # ssl_verify |
||
102 | new_web['ssl_verify'] = config.get_value(self._section, |
||
103 | '%sssl_verify' % postfix, |
||
104 | default=True) |
||
105 | # Proxy |
||
106 | http_proxy = config.get_value(self._section, |
||
107 | '%shttp_proxy' % postfix, |
||
108 | default=None) |
||
109 | |||
110 | https_proxy = config.get_value(self._section, |
||
111 | '%shttps_proxy' % postfix, |
||
112 | default=None) |
||
113 | |||
114 | if https_proxy is None and http_proxy is None: |
||
115 | new_web['proxies'] = None |
||
116 | else: |
||
117 | new_web['proxies'] = {'http' : http_proxy, |
||
118 | 'https' : https_proxy } |
||
119 | |||
120 | # Add the server to the list |
||
121 | logger.debug("Add Web URL %s to the static list" % new_web['url']) |
||
122 | web_list.append(new_web) |
||
123 | |||
124 | # Ports list loaded |
||
125 | logger.debug("Web list loaded: %s" % web_list) |
||
126 | |||
127 | return web_list |
||
128 | |||
136 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.