| Conditions | 12 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 37 |
| CRAP Score | 12 |
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 Dictionary.update_value() 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 | """Converter classes for builtin container types.""" |
||
| 30 | 1 | def update_value(self, data, strict=False): |
|
| 31 | 1 | cls = self.__class__ |
|
| 32 | 1 | value = cls.create_default() |
|
| 33 | |||
| 34 | # Convert object attributes to a dictionary |
||
| 35 | 1 | attrs = common.attrs[cls].copy() |
|
| 36 | 1 | if isinstance(data, cls): |
|
| 37 | 1 | dictionary = {} |
|
| 38 | 1 | for k, v in data.items(): |
|
| 39 | 1 | if k in attrs: |
|
| 40 | 1 | dictionary[k] = v |
|
| 41 | 1 | for k, v in data.__dict__.items(): |
|
| 42 | 1 | if k in attrs: |
|
| 43 | 1 | dictionary[k] = v |
|
| 44 | else: |
||
| 45 | 1 | dictionary = to_dict(data) |
|
| 46 | |||
| 47 | # Map object attributes to types |
||
| 48 | 1 | for name, data2 in dictionary.items(): |
|
| 49 | |||
| 50 | 1 | try: |
|
| 51 | 1 | converter = attrs.pop(name) |
|
| 52 | 1 | except KeyError: |
|
| 53 | 1 | if strict: |
|
| 54 | 1 | msg = "Ignored unknown nested file attribute: %s = %r" |
|
| 55 | 1 | log.warning(msg, name, data2) |
|
| 56 | 1 | continue |
|
| 57 | else: |
||
| 58 | 1 | converter = standard.match(name, data2, nested=True) |
|
| 59 | 1 | common.attrs[cls][name] = converter |
|
| 60 | |||
| 61 | 1 | try: |
|
| 62 | 1 | attr = self[name] |
|
| 63 | 1 | except KeyError: |
|
| 64 | 1 | attr = converter.create_default() |
|
| 65 | |||
| 66 | 1 | if all((isinstance(attr, converter), |
|
| 67 | issubclass(converter, Container))): |
||
| 68 | 1 | attr.update_value(data2, strict=strict) |
|
| 69 | else: |
||
| 70 | 1 | attr = converter.to_value(data2) |
|
| 71 | |||
| 72 | 1 | value[name] = attr |
|
| 73 | |||
| 74 | # Create default values for unmapped types |
||
| 75 | 1 | for name, converter in attrs.items(): |
|
| 76 | 1 | value[name] = converter.create_default() |
|
| 77 | 1 | msg = "Default value for missing nested object attribute: %s = %r" |
|
| 78 | 1 | log.info(msg, name, value[name]) |
|
| 79 | |||
| 80 | # Apply the new value |
||
| 81 | 1 | self.clear() |
|
| 82 | 1 | self.update(value) |
|
| 83 | |||
| 207 |