| Conditions | 18 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 32 |
| CRAP Score | 18 |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 PackedBitField.find_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 | 1 | import re |
|
| 59 | 1 | def find_value(self, item): |
|
| 60 | """ |
||
| 61 | Take a value, determine if it matches one, and only one, of the member fields |
||
| 62 | """ |
||
| 63 | # pylint: disable=too-many-branches |
||
| 64 | |||
| 65 | # Split the member fields into bitfields and enums |
||
| 66 | 1 | member_enums = [k for k in self._fields if not isinstance(k, starstruct.bitfield.BitField)] |
|
| 67 | 1 | member_bitfields = [k for k in self._fields if isinstance(k, starstruct.bitfield.BitField)] |
|
| 68 | |||
| 69 | # See if the supplied value is an enum or bitfield value |
||
| 70 | 1 | matches = [] |
|
| 71 | 1 | for key in member_bitfields: |
|
| 72 | 1 | try: |
|
| 73 | 1 | matches.append((key.find_value(item), key)) |
|
| 74 | 1 | except ValueError: |
|
| 75 | # This just means it isn't a member of this bitfield |
||
| 76 | 1 | pass |
|
| 77 | |||
| 78 | # Also check for matches in the enums. This helps guard against |
||
| 79 | # ambiguous inputs where the bitfield and enum types overlap. |
||
| 80 | 1 | if isinstance(item, tuple(member_enums)): |
|
| 81 | 1 | for key in member_enums: |
|
| 82 | 1 | if isinstance(item, key): |
|
| 83 | # This is guaranteed a unique match, so return now |
||
| 84 | 1 | return (item, key) |
|
| 85 | 1 | elif isinstance(item, str): |
|
| 86 | # If it's a string, then check it against the enum fields |
||
| 87 | # (bitfields should already have been validated) |
||
| 88 | 1 | for key in member_enums: |
|
| 89 | 1 | try: |
|
| 90 | 1 | matches.append((getattr(key, item), key)) |
|
| 91 | 1 | except AttributeError: |
|
| 92 | # This is the normal error to throw if the enum name is |
||
| 93 | # not valid for this enumeration type. Check the next enum. |
||
| 94 | 1 | pass |
|
| 95 | else: |
||
| 96 | # Lastly, assume that the item is an integer value, attempt to |
||
| 97 | # convert it to one of the enum values to ensure it is a valid |
||
| 98 | # value. But if it matches more than one member field, we are |
||
| 99 | # unable to pack this properly. |
||
| 100 | 1 | for key in member_enums: |
|
| 101 | 1 | try: |
|
| 102 | 1 | matches.append((key(item), key)) |
|
| 103 | 1 | except ValueError: |
|
| 104 | # This just means that the value is not valid for a |
||
| 105 | # specific enum type, check all enums for a match before |
||
| 106 | # raising a ValueError |
||
| 107 | 1 | pass |
|
| 108 | |||
| 109 | 1 | if len(matches) == 1: |
|
| 110 | 1 | return matches[0] |
|
| 111 | 1 | elif len(matches) < 1: |
|
| 112 | 1 | msg = '{} is not a valid {}'.format(item, list(self._fields)) |
|
| 113 | 1 | raise ValueError(msg) |
|
| 114 | 1 | elif len(matches) > 1: |
|
| 115 | 1 | msg = '{} is not a unique {}'.format(item, list(self._fields)) |
|
| 116 | 1 | raise ValueError(msg) |
|
| 117 | |||
| 173 |