Code Duplication    Length = 22-31 lines in 3 locations

pyof/foundation/basic_types.py 3 locations

@@ 328-357 (lines=30) @@
325
class HWAddress(GenericType):
326
    """Defines a hardware address."""
327
328
    def __init__(self, hw_address='00:00:00:00:00:00'):  # noqa
329
        """The constructor takes the parameters below.
330
331
        Args:
332
            hw_address (bytes): Hardware address. Defaults to
333
                '00:00:00:00:00:00'.
334
        """
335
        super().__init__(hw_address)
336
337
    def pack(self, value=None):
338
        """Pack the value as a binary representation.
339
340
        If the passed value (or the self._value) is zero (int), then the pack
341
        will assume that the value to be packed is '00:00:00:00:00:00'.
342
343
        Returns
344
            bytes: The binary representation.
345
346
        Raises:
347
            struct.error: If the value does not fit the binary format.
348
        """
349
        if isinstance(value, type(self)):
350
            return value.pack()
351
352
        if value is None:
353
            value = self._value
354
355
        if value == 0:
356
            value = '00:00:00:00:00:00'
357
358
        value = value.split(':')
359
360
        try:
@@ 184-205 (lines=22) @@
181
182
    def __init__(self, value=None, length=0):
183
        """The constructor takes the optional parameters below.
184
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'))
@@ 251-281 (lines=31) @@
248
        Args:
249
            address (str): IP Address using ipv4.
250
                Defaults to '0.0.0.0/32'
251
        """
252
        if address.find('/') >= 0:
253
            address, netmask = address.split('/')
254
        else:
255
            netmask = 32
256
257
        super().__init__(address)
258
        self.netmask = int(netmask)
259
260
    def pack(self, value=None):
261
        """Pack the value as a binary representation.
262
263
        If the value is None the self._value will be used to pack.
264
265
        Args:
266
            value (str): IP Address with ipv4 format.
267
268
        Returns
269
            bytes: The binary representation.
270
271
        Raises:
272
            struct.error: If the value does not fit the binary format.
273
        """
274
        if isinstance(value, type(self)):
275
            return value.pack()
276
277
        if value is None:
278
            value = self._value
279
280
        if value.find('/') >= 0:
281
            value = value.split('/')[0]
282
283
        try:
284
            value = value.split('.')