Code Duplication    Length = 22-44 lines in 4 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 3 locations

@@ 331-360 (lines=30) @@
328
329
    def __init__(self, value=None):  # noqa
330
        """The constructor takes the parameters below.
331
332
        Args:
333
            value (bytes): Hardware address. Defaults to
334
                '00:00:00:00:00:00'.
335
        """
336
        if value is None:
337
            value = '00:00:00:00:00:00'
338
        super().__init__(value)
339
340
    def pack(self, value=None):
341
        """Pack the value as a binary representation.
342
343
        If the passed value (or the self._value) is zero (int), then the pack
344
        will assume that the value to be packed is '00:00:00:00:00:00'.
345
346
        Returns
347
            bytes: The binary representation.
348
349
        Raises:
350
            struct.error: If the value does not fit the binary format.
351
        """
352
        if isinstance(value, type(self)):
353
            return value.pack()
354
355
        if value is None:
356
            value = self._value
357
358
        if value == 0:
359
            value = '00:00:00:00:00:00'
360
361
        value = value.split(':')
362
363
        try:
@@ 188-209 (lines=22) @@
185
        Args:
186
            value: The character to be build.
187
            length (int): Character size.
188
        """
189
        super().__init__(value)
190
        self.length = length
191
        self._fmt = '!{}{}'.format(self.length, 's')
192
193
    def pack(self, value=None):
194
        """Pack the value as a binary representation.
195
196
        Returns:
197
            bytes: The binary representation.
198
199
        Raises:
200
            struct.error: If the value does not fit the binary format.
201
        """
202
        if isinstance(value, type(self)):
203
            return value.pack()
204
205
        try:
206
            if value is None:
207
                value = self.value
208
            packed = struct.pack(self._fmt, bytes(value, 'ascii'))
209
            return packed[:-1] + b'\0'  # null-terminated
210
        except struct.error as err:
211
            msg = "Char Pack error. "
212
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
@@ 254-284 (lines=31) @@
251
        if value is None:
252
            value = "0.0.0.0/32"
253
        if value.find('/') >= 0:
254
            value, netmask = value.split('/')
255
        else:
256
            netmask = 32
257
258
        super().__init__(value)
259
        self.netmask = int(netmask)
260
261
    def pack(self, value=None):
262
        """Pack the value as a binary representation.
263
264
        If the value is None the self._value will be used to pack.
265
266
        Args:
267
            value (str): IP Address with ipv4 format.
268
269
        Returns:
270
            bytes: The binary representation.
271
272
        Raises:
273
            struct.error: If the value does not fit the binary format.
274
        """
275
        if isinstance(value, type(self)):
276
            return value.pack()
277
278
        if value is None:
279
            value = self._value
280
281
        if value.find('/') >= 0:
282
            value = value.split('/')[0]
283
284
        try:
285
            value = value.split('.')
286
            return struct.pack('!4B', *[int(x) for x in value])
287
        except struct.error as err: