| Conditions | 17 |
| Total Lines | 59 |
| Code Lines | 39 |
| 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 file_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 |
||
| 112 | def build_config(args): |
||
| 113 | """ |
||
| 114 | Building an optional config |
||
| 115 | :param args: |
||
| 116 | :return: |
||
| 117 | """ |
||
| 118 | config = defaultdict(dict) |
||
| 119 | config['target'] = defaultdict(dict) |
||
| 120 | config['origin'] = defaultdict(dict) |
||
| 121 | |||
| 122 | if not args.target_host is None: |
||
| 123 | config['target']['host'] = args.target_host |
||
| 124 | |||
| 125 | if not args.target_user is None: |
||
| 126 | config['target']['user'] = args.target_user |
||
| 127 | |||
| 128 | if not args.target_password is None: |
||
| 129 | config['target']['password'] = args.target_password |
||
| 130 | |||
| 131 | if not args.target_key is None: |
||
| 132 | config['target']['ssh_key'] = args.target_key |
||
| 133 | |||
| 134 | if not args.target_port is None: |
||
| 135 | config['target']['port'] = args.target_port |
||
| 136 | |||
| 137 | if not args.origin_host is None: |
||
| 138 | config['origin']['host'] = args.origin_host |
||
| 139 | |||
| 140 | if not args.origin_user is None: |
||
| 141 | config['origin']['user'] = args.origin_user |
||
| 142 | |||
| 143 | if not args.origin_password is None: |
||
| 144 | config['origin']['password'] = args.origin_password |
||
| 145 | |||
| 146 | if not args.origin_key is None: |
||
| 147 | config['origin']['ssh_key'] = args.origin_key |
||
| 148 | |||
| 149 | if not args.origin_port is None: |
||
| 150 | config['origin']['port'] = args.origin_port |
||
| 151 | |||
| 152 | if not args.files_origin is None: |
||
| 153 | if 'config' not in config['files']: |
||
| 154 | config['files']['config'] = [] |
||
| 155 | config['files']['config'].append({}) |
||
| 156 | config['files']['config'][0]['origin'] = args.files_origin |
||
| 157 | |||
| 158 | if not args.files_target is None: |
||
| 159 | if 'config' not in config['files']: |
||
| 160 | config['files']['config'] = [] |
||
| 161 | config['files']['config'].append({}) |
||
| 162 | config['files']['config'][0]['target'] = args.files_target |
||
| 163 | |||
| 164 | if not args.files_exclude is None: |
||
| 165 | config['files']['config'][0]['exclude'] = args.files_exclude.split(',') |
||
| 166 | |||
| 167 | if not args.files_option is None: |
||
| 168 | config['files']['option'] = args.files_option.split(',') |
||
| 169 | |||
| 170 | return config |
||
| 171 | |||
| 175 |