| Conditions | 13 |
| Total Lines | 34 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like completely_empty.completely_empty() 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 collections |
||
| 4 | def completely_empty(val): |
||
| 5 | if type(val) == str: |
||
| 6 | if val == '': |
||
| 7 | return True |
||
| 8 | else: |
||
| 9 | return False |
||
| 10 | |||
| 11 | if type(val) == list: |
||
| 12 | if len(val) == 0: |
||
| 13 | return True |
||
| 14 | else: |
||
| 15 | return all([completely_empty(i) for i in val]) |
||
| 16 | |||
| 17 | if type(val) == dict: |
||
| 18 | if len(val) == 0: |
||
| 19 | return True |
||
| 20 | elif len(val.keys()) == 1 and '' in val: |
||
| 21 | return True |
||
| 22 | return False |
||
| 23 | |||
| 24 | if type(val) == tuple: |
||
| 25 | return completely_empty(list(val)) |
||
| 26 | |||
| 27 | try: |
||
| 28 | val.__getitem__(0) |
||
| 29 | except IndexError: |
||
| 30 | return True |
||
| 31 | except AttributeError: |
||
| 32 | pass |
||
| 33 | |||
| 34 | if isinstance(val, collections.Iterable): |
||
| 35 | return all([completely_empty(i) for i in val]) |
||
| 36 | else: |
||
| 37 | return False |
||
| 38 | |||
| 53 |