Code Duplication    Length = 22-44 lines in 4 locations

pyof/foundation/base.py 1 location

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

pyof/foundation/basic_types.py 3 locations

@@ 331-360 (lines=30) @@
328
329
330
class HWAddress(GenericType):
331
    """Defines a hardware address."""
332
333
    # pylint: disable=useless-super-delegation
334
    def __init__(self, hw_address='00:00:00:00:00:00'):
335
        """Create a HWAddress with the parameters below.
336
337
        Args:
338
            hw_address (bytes): Hardware address. Defaults to
339
                '00:00:00:00:00:00'.
340
        """
341
        super().__init__(hw_address)
342
343
    def pack(self, value=None):
344
        """Pack the value as a binary representation.
345
346
        If the passed value (or the self._value) is zero (int), then the pack
347
        will assume that the value to be packed is '00:00:00:00:00:00'.
348
349
        Returns
350
            bytes: The binary representation.
351
352
        Raises:
353
            struct.error: If the value does not fit the binary format.
354
355
        """
356
        if isinstance(value, type(self)):
357
            return value.pack()
358
359
        if value is None:
360
            value = self._value
361
362
        if value == 0:
363
            value = '00:00:00:00:00:00'
@@ 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
        """
203
        if isinstance(value, type(self)):
204
            return value.pack()
205
206
        try:
207
            if value is None:
208
                value = self.value
209
            packed = struct.pack(self._fmt, bytes(value, 'ascii'))
210
            return packed[:-1] + b'\0'  # null-terminated
211
        except struct.error as err:
212
            msg = "Char Pack error. "
@@ 254-284 (lines=31) @@
251
            address (str): IP Address using ipv4. Defaults to '0.0.0.0/32'
252
        """
253
        if address.find('/') >= 0:
254
            address, netmask = address.split('/')
255
        else:
256
            netmask = 32
257
258
        super().__init__(address)
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
        """
276
        if isinstance(value, type(self)):
277
            return value.pack()
278
279
        if value is None:
280
            value = self._value
281
282
        if value.find('/') >= 0:
283
            value = value.split('/')[0]
284
285
        try:
286
            value = value.split('.')
287
            return struct.pack('!4B', *[int(x) for x in value])