| Conditions | 8 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 17 |
| CRAP Score | 10.6555 |
| Changes | 3 | ||
| Bugs | 0 | Features | 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:
| 1 | |||
| 5 | 1 | def StarTuple(name, named_fields, elements): |
|
| 6 | 1 | restricted_fields = { |
|
| 7 | # Default dunders |
||
| 8 | '__getnewargs__', |
||
| 9 | '__new__', |
||
| 10 | '__slots__ ', |
||
| 11 | '__repr__', |
||
| 12 | |||
| 13 | # Default #oneders |
||
| 14 | '_asdict', |
||
| 15 | '_make', |
||
| 16 | '_replace', |
||
| 17 | |||
| 18 | # Fields specifier |
||
| 19 | '_fields', |
||
| 20 | |||
| 21 | # Startuple additions |
||
| 22 | 'pack', |
||
| 23 | '_elements', |
||
| 24 | '__str__', |
||
| 25 | '_name', |
||
| 26 | } |
||
| 27 | |||
| 28 | 1 | intersection = restricted_fields.intersection(set(named_fields)) |
|
| 29 | |||
| 30 | 1 | if intersection: |
|
| 31 | 1 | raise ValueError('Restricted field used. Bad fields: {0}'.format(intersection)) |
|
| 32 | |||
| 33 | 1 | named_tuple = collections.namedtuple(name, named_fields) |
|
| 34 | |||
| 35 | # TODO: Auto update and replace! |
||
| 36 | |||
| 37 | 1 | def this_pack(self): |
|
| 38 | 1 | packed = bytes() |
|
| 39 | 1 | for _, value in self._elements.items(): |
|
| 40 | 1 | packed += value.pack(self._asdict()) |
|
| 41 | |||
| 42 | 1 | return packed |
|
| 43 | |||
| 44 | 1 | def this_str(self): |
|
| 45 | import pprint |
||
| 46 | fmt = 'StarTuple: <{0}>\n'.format(str(name)) |
||
| 47 | |||
| 48 | len_of_keys = 0 |
||
| 49 | for key in self._asdict().keys(): |
||
| 50 | if len(key) > len_of_keys: |
||
| 51 | len_of_keys = len(key) |
||
| 52 | |||
| 53 | for key, value in self._asdict().items(): |
||
| 54 | fmt += (' {key:%d}: {value}\n' % len_of_keys).format( |
||
| 55 | key=key, |
||
| 56 | value=pprint.pformat(value, width=150), |
||
| 57 | ) |
||
| 58 | |||
| 59 | return fmt |
||
| 60 | |||
| 61 | 1 | named_tuple.pack = this_pack |
|
| 62 | 1 | named_tuple.__str__ = this_str |
|
| 63 | 1 | named_tuple._elements = elements |
|
| 64 | 1 | named_tuple._name = name |
|
| 65 | |||
| 66 | return named_tuple |
||
| 67 |