Conditions | 8 |
Total Lines | 60 |
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 | 1 | def this_pack(self): |
|
36 | 1 | packed = bytes() |
|
37 | 1 | for _, value in self._elements.items(): |
|
38 | 1 | packed += value.pack(self._asdict()) |
|
39 | |||
40 | 1 | return packed |
|
41 | |||
42 | 1 | def this_str(self): |
|
43 | import pprint |
||
44 | fmt = 'StarTuple: <{0}>\n'.format(str(name)) |
||
45 | |||
46 | len_of_keys = 0 |
||
47 | for key in self._asdict().keys(): |
||
48 | if len(key) > len_of_keys: |
||
49 | len_of_keys = len(key) |
||
50 | |||
51 | for key, value in self._asdict().items(): |
||
52 | fmt += (' {key:%d}: {value}\n' % len_of_keys).format( |
||
53 | key=key, |
||
54 | value=pprint.pformat(value, width=150), |
||
55 | ) |
||
56 | |||
57 | return fmt |
||
58 | |||
59 | 1 | named_tuple.pack = this_pack |
|
60 | 1 | named_tuple.__str__ = this_str |
|
61 | 1 | named_tuple._elements = elements |
|
62 | 1 | named_tuple._name = name |
|
63 | |||
64 | return named_tuple |
||
65 |