Code Duplication    Length = 22-44 lines in 4 locations

pyof/foundation/base.py 1 location

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

pyof/foundation/basic_types.py 3 locations

@@ 331-360 (lines=30) @@
328
    def get_size(self, value=None):
329
        """Return the ip address size in bytes.
330
331
        Args:
332
            value: In structs, the user can assign other value instead of
333
                this class' instance. Here, in such cases, ``self`` is a class
334
                attribute of the struct.
335
336
        Returns:
337
            int: The address size in bytes.
338
339
        """
340
        return 4
341
342
    def __deepcopy__(self, memo):
343
        """Improve deepcopy speed."""
344
        return IPAddress(address=self._value, netmask=self.netmask)
345
346
347
class HWAddress(GenericType):
348
    """Defines a hardware address."""
349
350
    # pylint: disable=useless-super-delegation
351
    def __init__(self, hw_address='00:00:00:00:00:00'):
352
        """Create a HWAddress with the parameters below.
353
354
        Args:
355
            hw_address (bytes): Hardware address. Defaults to
356
                '00:00:00:00:00:00'.
357
        """
358
        super().__init__(hw_address)
359
360
    def pack(self, value=None):
361
        """Pack the value as a binary representation.
362
363
        If the passed value (or the self._value) is zero (int), then the pack
@@ 188-209 (lines=22) @@
185
        return DPID(dpid=self._value)
186
187
188
class Char(GenericType):
189
    """Build a double char type according to the length."""
190
191
    def __init__(self, value=None, length=0):
192
        """Create a Char with the optional parameters below.
193
194
        Args:
195
            value: The character to be build.
196
            length (int): Character size.
197
        """
198
        super().__init__(value)
199
        self.length = length
200
        self._fmt = '!{}{}'.format(self.length, 's')
201
202
    def pack(self, value=None):
203
        """Pack the value as a binary representation.
204
205
        Returns:
206
            bytes: The binary representation.
207
208
        Raises:
209
            struct.error: If the value does not fit the binary format.
210
211
        """
212
        if isinstance(value, type(self)):
@@ 254-284 (lines=31) @@
251
        return Char(value=self._value, length=self.length)
252
253
254
class IPAddress(GenericType):
255
    """Defines a IP address."""
256
257
    netmask = UBInt32()
258
    max_prefix = UBInt32(32)
259
260
    def __init__(self, address="0.0.0.0/32", netmask=None):
261
        """Create an IPAddress with the parameters below.
262
263
        Args:
264
            address (str): IP Address using ipv4. Defaults to '0.0.0.0/32'
265
        """
266
        if '/' in address:
267
            address, netmask = address.split('/')
268
        else:
269
            netmask = 32 if netmask is None else netmask
270
271
        super().__init__(address)
272
        self.netmask = int(netmask)
273
274
    def pack(self, value=None):
275
        """Pack the value as a binary representation.
276
277
        If the value is None the self._value will be used to pack.
278
279
        Args:
280
            value (str): IP Address with ipv4 format.
281
282
        Returns:
283
            bytes: The binary representation.
284
285
        Raises:
286
            struct.error: If the value does not fit the binary format.
287