Code Duplication    Length = 31-32 lines in 3 locations

pyof/foundation/basic_types.py 3 locations

@@ 383-414 (lines=32) @@
380
        super().__init__(address)
381
        self.netmask = int(netmask)
382
383
    def pack(self, value=None):
384
        """Pack the value as a binary representation.
385
386
        If the value is None the self._value will be used to pack.
387
388
        Args:
389
            value (str): IP Address with IPv6 format.
390
391
        Returns:
392
            bytes: The binary representation.
393
394
        Raises:
395
            struct.error: If the value does not fit the binary format.
396
397
        """
398
        if isinstance(value, type(self)):
399
            return value.pack()
400
401
        if value is None:
402
            value = self._value
403
404
        if value.find('/') >= 0:
405
            value = value.split('/')[0]
406
407
        try:
408
            value = value.split(':')
409
            return struct.pack('!8H', *[int(x, 16) for x in value])
410
        except struct.error as err:
411
            msg = "IPv6Address error. "
412
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
413
                                                         err)
414
            raise exceptions.PackException(msg)
415
416
    def unpack(self, buff, offset=0):
417
        """Unpack a binary message into this object's attributes.
@@ 280-311 (lines=32) @@
277
        super().__init__(address)
278
        self.netmask = int(netmask)
279
280
    def pack(self, value=None):
281
        """Pack the value as a binary representation.
282
283
        If the value is None the self._value will be used to pack.
284
285
        Args:
286
            value (str): IP Address with ipv4 format.
287
288
        Returns:
289
            bytes: The binary representation.
290
291
        Raises:
292
            struct.error: If the value does not fit the binary format.
293
294
        """
295
        if isinstance(value, type(self)):
296
            return value.pack()
297
298
        if value is None:
299
            value = self._value
300
301
        if value.find('/') >= 0:
302
            value = value.split('/')[0]
303
304
        try:
305
            value = value.split('.')
306
            return struct.pack('!4B', *[int(x) for x in value])
307
        except struct.error as err:
308
            msg = "IPAddress error. "
309
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
310
                                                         err)
311
            raise exceptions.PackException(msg)
312
313
    def unpack(self, buff, offset=0):
314
        """Unpack a binary message into this object's attributes.
@@ 474-504 (lines=31) @@
471
        """
472
        super().__init__(hw_address)
473
474
    def pack(self, value=None):
475
        """Pack the value as a binary representation.
476
477
        If the passed value (or the self._value) is zero (int), then the pack
478
        will assume that the value to be packed is '00:00:00:00:00:00'.
479
480
        Returns
481
            bytes: The binary representation.
482
483
        Raises:
484
            struct.error: If the value does not fit the binary format.
485
486
        """
487
        if isinstance(value, type(self)):
488
            return value.pack()
489
490
        if value is None:
491
            value = self._value
492
493
        if value == 0:
494
            value = '00:00:00:00:00:00'
495
496
        value = value.split(':')
497
498
        try:
499
            return struct.pack('!6B', *[int(x, 16) for x in value])
500
        except struct.error as err:
501
            msg = "HWAddress error. "
502
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
503
                                                         err)
504
            raise exceptions.PackException(msg)
505
506
    def unpack(self, buff, offset=0):
507
        """Unpack a binary message into this object's attributes.