Code Duplication    Length = 23-26 lines in 2 locations

starstruct/elementlength.py 1 location

@@ 16-41 (lines=26) @@
13
    The length StarStruct element class.
14
    """
15
16
    def __init__(self, field, mode=Mode.Native, alignment=1):
17
        """Initialize a StarStruct element object."""
18
19
        # All of the type checks have already been performed by the class
20
        # factory
21
        if isinstance(field[0], str):
22
            self.name = field[0]
23
            self.object_length = True
24
        elif isinstance(field[0], bytes):
25
            self.name = field[0].decode('utf-8')
26
            self.object_length = False
27
28
        self.ref = field[2]
29
30
        self._mode = mode
31
        self._alignment = alignment
32
33
        # Validate that the format specifiers are valid struct formats, this
34
        # doesn't have to be done now because the format will be checked when
35
        # any struct functions are called, but it's better to inform the user of
36
        # any errors earlier.
37
        # The easiest way to perform this check is to create a "Struct" class
38
        # instance, this will also increase the efficiency of all struct related
39
        # functions called.
40
        self.format = mode.value + field[1]
41
        self._struct = struct.Struct(self.format)
42
43
    @staticmethod
44
    def valid(field):

starstruct/elementfixedpoint.py 1 location

@@ 95-117 (lines=23) @@
92
        packed_struct = example_struct.make(my_data)
93
94
    """
95
96
    def __init__(self, field, mode=Mode.Native, alignment=1):
97
        """Initialize a StarStruct element object."""
98
99
        # TODO: Add checks in the class factory?
100
        self.name = field[0]
101
102
        # the ref attribute is required, but this element doesn't quite have
103
        # one, instead use the ref field to hold the fixed point format
104
        # attributes
105
        self.ref = {}
106
107
        self.ref['precision'] = field[3]
108
109
        if len(field) == 5:
110
            self.ref['decimal_prec'] = field[4]
111
        else:
112
            self.ref['decimal_prec'] = None
113
114
        self._mode = mode
115
        self._alignment = alignment
116
117
        self.format = mode.value + field[2]
118
        self._struct = struct.Struct(self.format)
119
120
    @staticmethod