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 -*- |
||
88 | def read(self): |
||
89 | """Read the config file, if it exists. Using defaults otherwise.""" |
||
90 | for config_file in self.config_file_paths(): |
||
91 | if os.path.exists(config_file): |
||
92 | try: |
||
93 | with open(config_file, encoding='utf-8') as f: |
||
94 | self.parser.read_file(f) |
||
95 | self.parser.read(f) |
||
96 | logger.info("Read configuration file '{}'".format(config_file)) |
||
97 | except UnicodeDecodeError as err: |
||
98 | logger.error("Cannot decode configuration file '{}': {}".format(config_file, err)) |
||
99 | sys.exit(1) |
||
100 | # Save the loaded configuration file path (issue #374) |
||
101 | self._loaded_config_file = config_file |
||
102 | break |
||
103 | |||
104 | # Quicklook |
||
105 | if not self.parser.has_section('quicklook'): |
||
106 | self.parser.add_section('quicklook') |
||
107 | self.set_default_cwc('quicklook', 'cpu') |
||
108 | self.set_default_cwc('quicklook', 'mem') |
||
109 | self.set_default_cwc('quicklook', 'swap') |
||
110 | |||
111 | # CPU |
||
112 | if not self.parser.has_section('cpu'): |
||
113 | self.parser.add_section('cpu') |
||
114 | self.set_default_cwc('cpu', 'user') |
||
115 | self.set_default_cwc('cpu', 'system') |
||
116 | self.set_default_cwc('cpu', 'steal') |
||
117 | # By default I/O wait should be lower than 1/number of CPU cores |
||
118 | iowait_bottleneck = (1.0 / multiprocessing.cpu_count()) * 100.0 |
||
119 | self.set_default_cwc('cpu', 'iowait', |
||
120 | [str(iowait_bottleneck - (iowait_bottleneck * 0.20)), |
||
121 | str(iowait_bottleneck - (iowait_bottleneck * 0.10)), |
||
122 | str(iowait_bottleneck)]) |
||
123 | ctx_switches_bottleneck = 56000 / multiprocessing.cpu_count() |
||
124 | self.set_default_cwc('cpu', 'ctx_switches', |
||
125 | [str(ctx_switches_bottleneck - (ctx_switches_bottleneck * 0.20)), |
||
126 | str(ctx_switches_bottleneck - (ctx_switches_bottleneck * 0.10)), |
||
127 | str(ctx_switches_bottleneck)]) |
||
128 | |||
129 | # Per-CPU |
||
130 | if not self.parser.has_section('percpu'): |
||
131 | self.parser.add_section('percpu') |
||
132 | self.set_default_cwc('percpu', 'user') |
||
133 | self.set_default_cwc('percpu', 'system') |
||
134 | |||
135 | # Load |
||
136 | if not self.parser.has_section('load'): |
||
137 | self.parser.add_section('load') |
||
138 | self.set_default_cwc('load', cwc=['0.7', '1.0', '5.0']) |
||
139 | |||
140 | # Mem |
||
141 | if not self.parser.has_section('mem'): |
||
142 | self.parser.add_section('mem') |
||
143 | self.set_default_cwc('mem') |
||
144 | |||
145 | # Swap |
||
146 | if not self.parser.has_section('memswap'): |
||
147 | self.parser.add_section('memswap') |
||
148 | self.set_default_cwc('memswap') |
||
149 | |||
150 | # NETWORK |
||
151 | if not self.parser.has_section('network'): |
||
152 | self.parser.add_section('network') |
||
153 | self.set_default_cwc('network', 'rx') |
||
154 | self.set_default_cwc('network', 'tx') |
||
155 | |||
156 | # FS |
||
157 | if not self.parser.has_section('fs'): |
||
158 | self.parser.add_section('fs') |
||
159 | self.set_default_cwc('fs') |
||
160 | |||
161 | # Sensors |
||
162 | if not self.parser.has_section('sensors'): |
||
163 | self.parser.add_section('sensors') |
||
164 | self.set_default_cwc('sensors', 'temperature_core', cwc=['60', '70', '80']) |
||
165 | self.set_default_cwc('sensors', 'temperature_hdd', cwc=['45', '52', '60']) |
||
166 | self.set_default_cwc('sensors', 'battery', cwc=['80', '90', '95']) |
||
167 | |||
168 | # Process list |
||
169 | if not self.parser.has_section('processlist'): |
||
170 | self.parser.add_section('processlist') |
||
171 | self.set_default_cwc('processlist', 'cpu') |
||
172 | self.set_default_cwc('processlist', 'mem') |
||
173 | |||
237 |