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
            hw_address (bytes): Hardware address. Defaults to
329
                '00:00:00:00:00:00'.
330
        """
331
        super().__init__(hw_address)
332
333
    def pack(self, value=None):
334
        """Pack the value as a binary representation.
335
336
        If the passed value (or the self._value) is zero (int), then the pack
337
        will assume that the value to be packed is '00:00:00:00:00:00'.
338
339
        Returns
340
            bytes: The binary representation.
341
342
        Raises:
343
            struct.error: If the value does not fit the binary format.
344
        """
345
        if isinstance(value, type(self)):
346
            return value.pack()
347
348
        if value is None:
349
            value = self._value
350
351
        if value == 0:
352
            value = '00:00:00:00:00:00'
353
354
        value = value.split(':')
355
356
        try:
357
            return struct.pack('!6B', *[int(x, 16) for x in value])
358
        except struct.error as err:
359
            msg = "HWAddress error. "
360
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
361
                                                         err)
362
            raise exceptions.PackException(msg)
363
@@ 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
        """