| Conditions | 29 |
| Total Lines | 84 |
| Lines | 20 |
| Ratio | 23.81 % |
| Changes | 16 | ||
| Bugs | 4 | Features | 2 |
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 node_config() 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 | import os |
||
| 141 | def node_config(queue_id): |
||
| 142 | # Find first node with queue and record node config |
||
| 143 | parallel_env = queue_id.split(':')[0] |
||
| 144 | queue_name = queue_id.split(':')[1] |
||
| 145 | host_group=0 |
||
| 146 | with os.popen('qconf -sq '+queue_name) as f: |
||
| 147 | for line in f: |
||
| 148 | if line.split(' ')[0] == 'hostlist': |
||
| 149 | new_line = re.sub(' +',' ',line) |
||
| 150 | host_group = new_line.split(' ')[1] |
||
| 151 | |||
| 152 | config = {} |
||
| 153 | host_name = '' |
||
| 154 | found = False |
||
| 155 | if host_group[0] is '@': |
||
| 156 | #Is a host group |
||
| 157 | with os.popen('qconf -shgrp_resolved '+host_group) as f: |
||
| 158 | for line in f: |
||
| 159 | for host_name in line.split(' '): |
||
| 160 | with os.popen('qhost -q -h '+host_name) as f: |
||
| 161 | header = f.readline(); # read header |
||
| 162 | f.readline(); # read separator |
||
| 163 | new_header = re.sub(' +',' ',header).strip() |
||
| 164 | if (new_header.split(' ')[3]) == 'LOAD': #sge <=6.2u4 style |
||
| 165 | for line in f: |
||
| 166 | if line[0] != ' ': |
||
| 167 | name = line.split(' ')[0] |
||
| 168 | if name != 'global': |
||
| 169 | new_line = re.sub(' +',' ',line).strip() |
||
| 170 | if new_line.split(' ')[3] != '-': |
||
| 171 | config['max task'] = int(new_line.split(' ')[2]) |
||
| 172 | config['max thread'] = int(new_line.split(' ')[2]) |
||
| 173 | config['max memory'] = new_line.split(' ')[4] |
||
| 174 | found = True |
||
| 175 | break |
||
| 176 | else: |
||
| 177 | for line in f: |
||
| 178 | if line[0] != ' ': |
||
| 179 | name = line.split(' ')[0] |
||
| 180 | if name != 'global': |
||
| 181 | new_line = re.sub(' +',' ',line).strip() |
||
| 182 | if new_line.split(' ')[3] != '-': |
||
| 183 | config['max task'] = int(new_line.split(' ')[4]) |
||
| 184 | config['max thread'] = int(new_line.split(' ')[5]) |
||
| 185 | config['max memory'] = new_line.split(' ')[7] |
||
| 186 | found = True |
||
| 187 | break |
||
| 188 | if found: break |
||
| 189 | else: |
||
| 190 | #Is a host |
||
| 191 | host_name = host_group |
||
| 192 | with os.popen('qhost -q -h '+host_name) as f: |
||
| 193 | header = f.readline(); # read header |
||
| 194 | f.readline(); # read separator |
||
| 195 | new_header = re.sub(' +',' ',header).strip() |
||
| 196 | if (new_header.split(' ')[3]) == 'LOAD': #sge <=6.2u4 style |
||
| 197 | for line in f: |
||
| 198 | if line[0] != ' ': |
||
| 199 | name = line.split(' ')[0] |
||
| 200 | View Code Duplication | if name != 'global': |
|
|
|
|||
| 201 | new_line = re.sub(' +',' ',line).strip() |
||
| 202 | if new_line.split(' ')[3] != '-': |
||
| 203 | config['max task'] = int(new_line.split(' ')[2]) |
||
| 204 | config['max thread'] = int(new_line.split(' ')[2]) |
||
| 205 | config['max memory'] = new_line.split(' ')[4] |
||
| 206 | else: |
||
| 207 | config['max task'] = 0 |
||
| 208 | config['max thread'] = 0 |
||
| 209 | config['max memory'] = 0 |
||
| 210 | else: |
||
| 211 | for line in f: |
||
| 212 | if line[0] != ' ': |
||
| 213 | name = line.split(' ')[0] |
||
| 214 | View Code Duplication | if name != 'global': |
|
| 215 | new_line = re.sub(' +',' ',line).strip() |
||
| 216 | if new_line.split(' ')[3] != '-': |
||
| 217 | config['max task'] = int(new_line.split(' ')[4]) |
||
| 218 | config['max thread'] = int(new_line.split(' ')[5]) |
||
| 219 | config['max memory'] = new_line.split(' ')[7] |
||
| 220 | else: |
||
| 221 | config['max task'] = 0 |
||
| 222 | config['max thread'] = 0 |
||
| 223 | config['max memory'] = 0 |
||
| 224 | return config |
||
| 225 | |||
| 377 |