| Conditions | 11 |
| Total Lines | 70 |
| Code Lines | 49 |
| Lines | 70 |
| Ratio | 100 % |
| Changes | 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:
Complex classes like src.record.Record._parse() 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 | # MIT License |
||
| 68 | def _parse(self): |
||
| 69 | header_offset = 0 |
||
| 70 | |||
| 71 | header_length_varint = Varint( |
||
| 72 | # A varint is encoded on *at most* 9 bytes |
||
| 73 | bytes(self)[header_offset:9 + header_offset] |
||
| 74 | ) |
||
| 75 | |||
| 76 | # Let's keep track of how many bytes of the Record header (including |
||
| 77 | # the header length itself) we've succesfully parsed |
||
| 78 | parsed_header_bytes = len(header_length_varint) |
||
| 79 | |||
| 80 | if len(bytes(self)) < int(header_length_varint): |
||
| 81 | raise MalformedRecord( |
||
| 82 | "Not enough bytes to fully read the record header!" |
||
| 83 | ) |
||
| 84 | |||
| 85 | header_offset += len(header_length_varint) |
||
| 86 | self._header_bytes = bytes(self)[:int(header_length_varint)] |
||
| 87 | |||
| 88 | col_idx = 0 |
||
| 89 | field_offset = int(header_length_varint) |
||
| 90 | while header_offset < int(header_length_varint): |
||
| 91 | serial_type_varint = Varint( |
||
| 92 | bytes(self)[header_offset:9 + header_offset] |
||
| 93 | ) |
||
| 94 | serial_type = int(serial_type_varint) |
||
| 95 | col_length = None |
||
| 96 | |||
| 97 | try: |
||
| 98 | col_length, _ = self.column_types[serial_type] |
||
| 99 | except KeyError as col_type_ex: |
||
| 100 | if serial_type >= 13 and (1 == serial_type % 2): |
||
| 101 | col_length = (serial_type - 13) // 2 |
||
| 102 | elif serial_type >= 12 and (0 == serial_type % 2): |
||
| 103 | col_length = (serial_type - 12) // 2 |
||
| 104 | else: |
||
| 105 | raise ValueError( |
||
| 106 | "Unknown serial type {}".format(serial_type) |
||
| 107 | ) from col_type_ex |
||
| 108 | |||
| 109 | try: |
||
| 110 | field_obj = Field( |
||
| 111 | col_idx, |
||
| 112 | serial_type, |
||
| 113 | bytes(self)[field_offset:field_offset + col_length] |
||
| 114 | ) |
||
| 115 | except MalformedField as ex: |
||
| 116 | _LOGGER.warning( |
||
| 117 | "Caught %r while instantiating field %d (%d)", |
||
| 118 | ex, col_idx, serial_type |
||
| 119 | ) |
||
| 120 | raise MalformedRecord from ex |
||
| 121 | except Exception as ex: |
||
| 122 | _LOGGER.warning( |
||
| 123 | "Caught %r while instantiating field %d (%d)", |
||
| 124 | ex, col_idx, serial_type |
||
| 125 | ) |
||
| 126 | # pdb.set_trace() |
||
| 127 | raise |
||
| 128 | |||
| 129 | self._fields[col_idx] = field_obj |
||
| 130 | col_idx += 1 |
||
| 131 | field_offset += col_length |
||
| 132 | |||
| 133 | parsed_header_bytes += len(serial_type_varint) |
||
| 134 | header_offset += len(serial_type_varint) |
||
| 135 | |||
| 136 | if field_offset > len(bytes(self)): |
||
| 137 | raise MalformedRecord |
||
| 138 | |||
| 163 |