| Conditions | 15 |
| Total Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 Config.read() 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 -*- |
||
| 128 | def read(self): |
||
| 129 | """Read the config file, if it exists. Using defaults otherwise.""" |
||
| 130 | for config_file in self.config_file_paths(): |
||
| 131 | if os.path.exists(config_file): |
||
| 132 | try: |
||
| 133 | with open(config_file, encoding='utf-8') as f: |
||
| 134 | self.parser.read_file(f) |
||
| 135 | self.parser.read(f) |
||
| 136 | logger.info("Read configuration file '{}'".format(config_file)) |
||
| 137 | except UnicodeDecodeError as err: |
||
| 138 | logger.error("Cannot decode configuration file '{}': {}".format(config_file, err)) |
||
| 139 | sys.exit(1) |
||
| 140 | # Save the loaded configuration file path (issue #374) |
||
| 141 | self._loaded_config_file = config_file |
||
| 142 | break |
||
| 143 | |||
| 144 | # Quicklook |
||
| 145 | if not self.parser.has_section('quicklook'): |
||
| 146 | self.parser.add_section('quicklook') |
||
| 147 | self.set_default_cwc('quicklook', 'cpu') |
||
| 148 | self.set_default_cwc('quicklook', 'mem') |
||
| 149 | self.set_default_cwc('quicklook', 'swap') |
||
| 150 | |||
| 151 | # CPU |
||
| 152 | if not self.parser.has_section('cpu'): |
||
| 153 | self.parser.add_section('cpu') |
||
| 154 | self.set_default_cwc('cpu', 'user') |
||
| 155 | self.set_default_cwc('cpu', 'system') |
||
| 156 | self.set_default_cwc('cpu', 'steal') |
||
| 157 | # By default I/O wait should be lower than 1/number of CPU cores |
||
| 158 | iowait_bottleneck = (1.0 / multiprocessing.cpu_count()) * 100.0 |
||
| 159 | self.set_default_cwc('cpu', 'iowait', |
||
| 160 | [str(iowait_bottleneck - (iowait_bottleneck * 0.20)), |
||
| 161 | str(iowait_bottleneck - (iowait_bottleneck * 0.10)), |
||
| 162 | str(iowait_bottleneck)]) |
||
| 163 | ctx_switches_bottleneck = 56000 / multiprocessing.cpu_count() |
||
| 164 | self.set_default_cwc('cpu', 'ctx_switches', |
||
| 165 | [str(ctx_switches_bottleneck - (ctx_switches_bottleneck * 0.20)), |
||
| 166 | str(ctx_switches_bottleneck - (ctx_switches_bottleneck * 0.10)), |
||
| 167 | str(ctx_switches_bottleneck)]) |
||
| 168 | |||
| 169 | # Per-CPU |
||
| 170 | if not self.parser.has_section('percpu'): |
||
| 171 | self.parser.add_section('percpu') |
||
| 172 | self.set_default_cwc('percpu', 'user') |
||
| 173 | self.set_default_cwc('percpu', 'system') |
||
| 174 | |||
| 175 | # Load |
||
| 176 | if not self.parser.has_section('load'): |
||
| 177 | self.parser.add_section('load') |
||
| 178 | self.set_default_cwc('load', cwc=['0.7', '1.0', '5.0']) |
||
| 179 | |||
| 180 | # Mem |
||
| 181 | if not self.parser.has_section('mem'): |
||
| 182 | self.parser.add_section('mem') |
||
| 183 | self.set_default_cwc('mem') |
||
| 184 | |||
| 185 | # Swap |
||
| 186 | if not self.parser.has_section('memswap'): |
||
| 187 | self.parser.add_section('memswap') |
||
| 188 | self.set_default_cwc('memswap') |
||
| 189 | |||
| 190 | # NETWORK |
||
| 191 | if not self.parser.has_section('network'): |
||
| 192 | self.parser.add_section('network') |
||
| 193 | self.set_default_cwc('network', 'rx') |
||
| 194 | self.set_default_cwc('network', 'tx') |
||
| 195 | |||
| 196 | # FS |
||
| 197 | if not self.parser.has_section('fs'): |
||
| 198 | self.parser.add_section('fs') |
||
| 199 | self.set_default_cwc('fs') |
||
| 200 | |||
| 201 | # Sensors |
||
| 202 | if not self.parser.has_section('sensors'): |
||
| 203 | self.parser.add_section('sensors') |
||
| 204 | self.set_default_cwc('sensors', 'temperature_core', cwc=['60', '70', '80']) |
||
| 205 | self.set_default_cwc('sensors', 'temperature_hdd', cwc=['45', '52', '60']) |
||
| 206 | self.set_default_cwc('sensors', 'battery', cwc=['80', '90', '95']) |
||
| 207 | |||
| 208 | # Process list |
||
| 209 | if not self.parser.has_section('processlist'): |
||
| 210 | self.parser.add_section('processlist') |
||
| 211 | self.set_default_cwc('processlist', 'cpu') |
||
| 212 | self.set_default_cwc('processlist', 'mem') |
||
| 213 | |||
| 277 |