<commit_before>from ofp.v0x02.types import UBInt8, UBInt16, UBInt32
from ofp.v0x02.consts import *

import collections

class Test(type):
    @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 not in
                                    ('__init__', '__module__','__qualname__')]
        return type.__new__(self, name, bases, classdict)

class GenericStruct(metaclass=Test):
    def __init__(self, *args, **kwargs):
        for _attr, _class in self.__ordered__:
            if not callable(getattr(self, _attr)):
                # TODO: Validade data
                try:
                    setattr(self, _attr, kwargs[_attr])
                except KeyError:
                    pass

    def get_size(self):
        tot = 0
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                tot += (getattr(self, _attr).get_size())
            elif not callable(attr):
                tot += (_class(attr).get_size())
        return tot

    def build(self):
        hex = b''
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                hex += getattr(self, _attr).build()
                print("{} {} {}".format(_attr, attr,getattr(self, _attr).build()))
            elif not callable(attr):
                hex += _class(attr).build()
                print("{} {} {}".format(_attr, attr,_class(attr).build()))
        return hex

class GenericMessage(GenericStruct):
    def __init__(self, header, *args, **kwargs):
        """Receives the header and update the length field in it"""
        super().__init__(*args, **kwargs)
        self.header = header
        self.header.length = self.get_size()
        
    

class OFPHeader(GenericStruct):
    version = UBInt8()
    xid = UBInt32()
    length = UBInt16()
    ofp_type = UBInt8()

class OFPHello(GenericMessage):
    header = OFPHeader(version = 1 , xid = 10, ofp_type=0, length=2)
    x = UBInt32()

    def __init__(self, xid = 0, x = 0):
        header = OFPHeader(version = 1 , xid = xid, ofp_type=2, length=0)
        self.x = x
        super(OFPHello, self).__init__(header)



hello = OFPHello(xid=10,x=3)
print(hello.get_size())
print(hello.header.length)
print(hello.build())
<commit_msg>Pep 8 format<commit_after>from ofp.v0x02.types import UBInt8, UBInt16, UBInt32
from ofp.v0x02.consts import *

import collections

class Test(type):
    @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 not in 
                            ('__module__','__qualname__')]
        return type.__new__(self, name, bases, classdict)

class GenericStruct(metaclass=Test):
    def __init__(self, *args, **kwargs):
        for _attr, _class in self.__ordered__:
            if not callable(getattr(self, _attr)):
                # TODO: Validade data
                try:
                    setattr(self, _attr, kwargs[_attr])
                except KeyError:
                    pass

    def get_size(self):
        tot = 0
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                tot += (getattr(self, _attr).get_size())
            elif not callable(attr):
                tot += (_class(attr).get_size())
        return tot

    def build(self):
        hex = b''
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                hex += getattr(self, _attr).build()
                print("{} {} {}".format(_attr, attr,getattr(self, _attr).build()))
            elif not callable(attr):
                hex += _class(attr).build()
                print("{} {} {}".format(_attr, attr,_class(attr).build()))
        return hex

class GenericMessage(GenericStruct):
    def __init__(self, header, *args, **kwargs):
        """Receives the header and update the length field in it"""
        super().__init__(*args, **kwargs)
        self.header = header
        self.header.length = self.get_size()
        
    

class OFPHeader(GenericStruct):
    version = UBInt8()
    xid = UBInt32()
    length = UBInt16()
    ofp_type = UBInt8()

class OFPHello(GenericMessage):
    header = OFPHeader(version = 1 , xid = 10, ofp_type=0, length=2)
    x = UBInt32()

    def __init__(self, xid = 0, x = 0):
        header = OFPHeader(version = 1 , xid = xid, ofp_type=2, length=0)
        self.x = x
        super(OFPHello, self).__init__(header)



hello = OFPHello(xid=10,x=3)
print(hello.get_size())
print(hello.header.length)
print(hello.build())

{'fraction_non_alphanumeric': 0.12190287413280476, 'fraction_numerical': 0.013280475718533202, 'mean_word_length': 2.1225247524752477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6040409', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1538, 'n_words': 389}