| Conditions | 28 |
| Total Lines | 90 |
| Code Lines | 57 |
| 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 db_sync_tool.__main__.build_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 | #!/usr/bin/env python3 |
||
| 191 | def build_config(args): |
||
| 192 | """ |
||
| 193 | Building an optional config |
||
| 194 | :param args: |
||
| 195 | :return: |
||
| 196 | """ |
||
| 197 | config = defaultdict(dict) |
||
| 198 | |||
| 199 | if not args.type is None: |
||
| 200 | config['type'] = args.type |
||
| 201 | |||
| 202 | if not args.target_path is None: |
||
| 203 | config['target']['path'] = args.target_path |
||
| 204 | |||
| 205 | if not args.target_name is None: |
||
| 206 | config['target']['name'] = args.target_name |
||
| 207 | |||
| 208 | if not args.target_host is None: |
||
| 209 | config['target']['host'] = args.target_host |
||
| 210 | |||
| 211 | if not args.target_user is None: |
||
| 212 | config['target']['user'] = args.target_user |
||
| 213 | |||
| 214 | if not args.target_password is None: |
||
| 215 | config['target']['password'] = args.target_password |
||
| 216 | |||
| 217 | if not args.target_key is None: |
||
| 218 | config['target']['key'] = args.target_key |
||
| 219 | |||
| 220 | if not args.target_port is None: |
||
| 221 | config['target']['port'] = args.target_port |
||
| 222 | |||
| 223 | if not args.target_dump_dir is None: |
||
| 224 | config['target']['dump_dir'] = args.target_dump_dir |
||
| 225 | |||
| 226 | if not args.target_db_name is None: |
||
| 227 | config['target']['db']['name'] = args.target_db_name |
||
| 228 | |||
| 229 | if not args.target_db_host is None: |
||
| 230 | config['target']['db']['host'] = args.target_db_host |
||
| 231 | |||
| 232 | if not args.target_db_user is None: |
||
| 233 | config['target']['db']['user'] = args.target_db_user |
||
| 234 | |||
| 235 | if not args.target_db_password is None: |
||
| 236 | config['target']['db']['password'] = args.target_db_password |
||
| 237 | |||
| 238 | if not args.target_db_port is None: |
||
| 239 | config['target']['db']['port'] = args.target_db_port |
||
| 240 | |||
| 241 | if not args.origin_path is None: |
||
| 242 | config['origin']['path'] = args.origin_path |
||
| 243 | |||
| 244 | if not args.origin_name is None: |
||
| 245 | config['origin']['name'] = args.origin_name |
||
| 246 | |||
| 247 | if not args.origin_host is None: |
||
| 248 | config['origin']['host'] = args.origin_host |
||
| 249 | |||
| 250 | if not args.origin_user is None: |
||
| 251 | config['origin']['user'] = args.origin_user |
||
| 252 | |||
| 253 | if not args.origin_password is None: |
||
| 254 | config['origin']['password'] = args.origin_password |
||
| 255 | |||
| 256 | if not args.origin_key is None: |
||
| 257 | config['origin']['key'] = args.origin_key |
||
| 258 | |||
| 259 | if not args.origin_port is None: |
||
| 260 | config['origin']['port'] = args.origin_port |
||
| 261 | |||
| 262 | if not args.origin_dump_dir is None: |
||
| 263 | config['origin']['dump_dir'] = args.origin_dump_dir |
||
| 264 | |||
| 265 | if not args.origin_db_name is None: |
||
| 266 | config['origin']['db']['name'] = args.origin_db_name |
||
| 267 | |||
| 268 | if not args.origin_db_host is None: |
||
| 269 | config['origin']['db']['host'] = args.origin_db_host |
||
| 270 | |||
| 271 | if not args.origin_db_user is None: |
||
| 272 | config['origin']['db']['user'] = args.origin_db_user |
||
| 273 | |||
| 274 | if not args.origin_db_password is None: |
||
| 275 | config['origin']['db']['password'] = args.origin_db_password |
||
| 276 | |||
| 277 | if not args.origin_db_port is None: |
||
| 278 | config['origin']['db']['port'] = args.origin_db_port |
||
| 279 | |||
| 280 | return config |
||
| 281 | |||
| 285 |