<commit_before>"""Contains basic and fundamental classes and constants"""

# System imports
import collections
import enum
import struct

# Third-party imports

# Local source tree imports
from ofp.v0x01.foundation import exceptions

__all__ = ['OFP_ETH_ALEN', 'OFP_MAX_PORT_NAME_LEN', 'OFP_VERSION',
           'OFP_MAX_TABLE_NAME_LEN', 'SERIAL_NUM_LEN', 'DESC_STR_LEN']

# CONSTANTS
OFP_ETH_ALEN = 6
OFP_MAX_PORT_NAME_LEN = 16
OFP_VERSION = 0x01
OFP_MAX_TABLE_NAME_LEN = 32
SERIAL_NUM_LEN = 32
DESC_STR_LEN = 256

# CLASSES


class GenericType(object):
    """This is a generic type Descriptor

    We can't implemment the __get__ method here
    """
    def __init__(self):
        self._value = None

    def __repr__(self):
        return "{}({})".format(self.__class__.__name__, self._value)

    def __str__(self):
        return str(self._value)

    def __set__(self, instance, value):
        # TODO: Check if value is of the same class
        self._value = value

    def __delete__(self, instance):
        # TODO: This is the right delete way? Or should we delete
        #       the attribute from the instance?
        del self._value

    def __eq__(self, other):
        return self._value == other

    def __ne__(self, other):
        return self._value != other

    def __gt__(self, other):
        return self._value > other

    def __ge__(self, other):
        return self._value >= other

    def __lt__(self, other):
        return self._value <= other

    def __le__(self, other):
        return self._value <= other

    def pack(self):
        """Pack the valeu as a binary representation."""
        try:
            if type(self._value.__class__) is enum.EnumMeta:
                # Gets the respective value from the Enum
                return struct.pack(self._fmt, self._value.value)
            else:
                return struct.pack(self._fmt, self._value)
        except struct.error:
            message = "Value out of the possible range to basic type "
            message = message + self.__class__.__name__
            raise exceptions.OFPBadValueException(message)

    def unpack(self, buff, offset=0):
        """ Unpack a buff and stores at _value property. """
        # TODO: How to deal with this when the attribute from the
        #       owner class is an element from a enum? How to recover
        #       the enum name/reference ?
        try:
            self._value = struct.unpack_from(self._fmt, buff, offset)[0]
        except struct.error:
            raise exceptions.OFPException("Error while unpacking"
                                          "data from buffer")

    def get_size(self):
        """ Return the size of type in bytes. """
        return struct.calcsize(self._fmt)


class MetaStruct(type):
    """MetaClass used to force ordered attributes"""

    @classmethod
    def __prepare__(self, name, bases):
        return collections.OrderedDict()

    def __new__(self, name, bases, classdict):
        classdict['__ordered__'] = [(key, type(value)) for key, value in
                                    classdict.items() if key[0] != '_']
        return type.__new__(self, name, bases, classdict)
<commit_msg>[v0x01] GenericType pack exception improvement<commit_after>"""Contains basic and fundamental classes and constants"""

# System imports
import collections
import enum
import struct

# Third-party imports

# Local source tree imports
from ofp.v0x01.foundation import exceptions

__all__ = ['OFP_ETH_ALEN', 'OFP_MAX_PORT_NAME_LEN', 'OFP_VERSION',
           'OFP_MAX_TABLE_NAME_LEN', 'SERIAL_NUM_LEN', 'DESC_STR_LEN']

# CONSTANTS
OFP_ETH_ALEN = 6
OFP_MAX_PORT_NAME_LEN = 16
OFP_VERSION = 0x01
OFP_MAX_TABLE_NAME_LEN = 32
SERIAL_NUM_LEN = 32
DESC_STR_LEN = 256

# CLASSES


class GenericType(object):
    """This is a generic type Descriptor

    We can't implemment the __get__ method here
    """
    def __init__(self):
        self._value = None

    def __repr__(self):
        return "{}({})".format(self.__class__.__name__, self._value)

    def __str__(self):
        return str(self._value)

    def __set__(self, instance, value):
        # TODO: Check if value is of the same class
        self._value = value

    def __delete__(self, instance):
        # TODO: This is the right delete way? Or should we delete
        #       the attribute from the instance?
        del self._value

    def __eq__(self, other):
        return self._value == other

    def __ne__(self, other):
        return self._value != other

    def __gt__(self, other):
        return self._value > other

    def __ge__(self, other):
        return self._value >= other

    def __lt__(self, other):
        return self._value <= other

    def __le__(self, other):
        return self._value <= other

    def pack(self):
        """Pack the valeu as a binary representation."""
        try:
            if type(self._value.__class__) is enum.EnumMeta:
                # Gets the respective value from the Enum
                return struct.pack(self._fmt, self._value.value)
            else:
                return struct.pack(self._fmt, self._value)
        except struct.error:
            message = "Value out of the possible range to basic type "
            message = message + self.__class__.__name__
            raise exceptions.OFPBadValueException(message) from struct.error

    def unpack(self, buff, offset=0):
        """ Unpack a buff and stores at _value property. """
        # TODO: How to deal with this when the attribute from the
        #       owner class is an element from a enum? How to recover
        #       the enum name/reference ?
        try:
            self._value = struct.unpack_from(self._fmt, buff, offset)[0]
        except struct.error:
            raise exceptions.OFPException("Error while unpacking"
                                          "data from buffer")

    def get_size(self):
        """ Return the size of type in bytes. """
        return struct.calcsize(self._fmt)


class MetaStruct(type):
    """MetaClass used to force ordered attributes"""

    @classmethod
    def __prepare__(self, name, bases):
        return collections.OrderedDict()

    def __new__(self, name, bases, classdict):
        classdict['__ordered__'] = [(key, type(value)) for key, value in
                                    classdict.items() if key[0] != '_']
        return type.__new__(self, name, bases, classdict)

{'fraction_non_alphanumeric': 0.09121989121989121, 'fraction_numerical': 0.006371406371406371, 'mean_word_length': 2.383806519453207, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3599871', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1771, 'n_words': 582}