Code Duplication    Length = 22-44 lines in 3 locations

pyof/foundation/base.py 1 location

@@ 133-176 (lines=44) @@
130
        else:
131
            return self._value
132
133
    def pack(self, value=None):
134
        r"""Pack the value as a binary representation.
135
136
        Considering an example with UBInt8 class, that inherits from
137
        GenericType:
138
139
        >>> from pyof.foundation.basic_types import UBInt8
140
        >>> objectA = UBInt8(1)
141
        >>> objectB = 5
142
        >>> objectA.pack()
143
        b'\x01'
144
        >>> objectA.pack(objectB)
145
        b'\x05'
146
147
        Args:
148
            value: If the value is None, then we will pack the value of the
149
                current instance. Otherwise, if value is an instance of the
150
                same type as the current instance, then we call the pack of the
151
                value object. Otherwise, we will use the current instance pack
152
                method on the passed value.
153
154
        Returns:
155
            bytes: The binary representation.
156
157
        Raises:
158
            :exc:`~.exceptions.BadValueException`: If the value does not
159
                fit the binary format.
160
        """
161
        if isinstance(value, type(self)):
162
            return value.pack()
163
164
        if value is None:
165
            value = self.value
166
        elif 'value' in dir(value):
167
            # if it is enum or bitmask gets only the 'int' value
168
            value = value.value
169
170
        try:
171
            return struct.pack(self._fmt, value)
172
        except struct.error:
173
            msg = '{} could not pack {} = {}.'.format(type(self).__name__,
174
                                                      type(value).__name__,
175
                                                      value)
176
            raise PackException(msg)
177
178
    def unpack(self, buff, offset=0):
179
        """Unpack *buff* into this object.

pyof/foundation/basic_types.py 2 locations

@@ 331-360 (lines=30) @@
328
                '00:00:00:00:00:00'.
329
        """
330
        super().__init__(hw_address)
331
332
    def pack(self, value=None):
333
        """Pack the value as a binary representation.
334
335
        If the passed value (or the self._value) is zero (int), then the pack
336
        will assume that the value to be packed is '00:00:00:00:00:00'.
337
338
        Returns
339
            bytes: The binary representation.
340
341
        Raises:
342
            struct.error: If the value does not fit the binary format.
343
        """
344
        if isinstance(value, type(self)):
345
            return value.pack()
346
347
        if value is None:
348
            value = self._value
349
350
        if value == 0:
351
            value = '00:00:00:00:00:00'
352
353
        value = value.split(':')
354
355
        try:
356
            return struct.pack('!6B', *[int(x, 16) for x in value])
357
        except struct.error as err:
358
            msg = "HWAddress error. "
359
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
360
                                                         err)
361
            raise exceptions.PackException(msg)
362
363
    def unpack(self, buff, offset=0):
@@ 188-209 (lines=22) @@
185
        """
186
        if isinstance(value, type(self)):
187
            return value.pack()
188
189
        try:
190
            if value is None:
191
                value = self.value
192
            packed = struct.pack(self._fmt, bytes(value, 'ascii'))
193
            return packed[:-1] + b'\0'  # null-terminated
194
        except struct.error as err:
195
            msg = "Char Pack error. "
196
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
197
                                                         err)
198
            raise exceptions.PackException(msg)
199
200
    def unpack(self, buff, offset=0):
201
        """Unpack a binary message into this object's attributes.
202
203
        Unpack the binary value *buff* and update this object attributes based
204
        on the results.
205
206
        Args:
207
            buff (bytes): Binary data package to be unpacked.
208
            offset (int): Where to begin unpacking.
209
210
        Raises:
211
            Exception: If there is a struct unpacking error.
212
        """