<commit_before>import re
import field as Field


class Query(list):
    fields = ()
    separator = '.'

    def __init__(self, encoded=None, **kwargs):
        list.__init__(self)
        self.encoded = str(encoded)
        self.params = kwargs
        for field in self.fields:
            if isinstance(field, str):
                self.append(field)
                continue
            if field.name in kwargs:
                value = kwargs[field.name]
                self.append(field(value))
            else:
                self.append(field())

    def __bytes__(self):
        return self.encode().encode('utf-8')

    def __str__(self):
        return self.encode()

    def pattern(self, **kwargs):
        patterns = []
        for field in self.fields:
            if isinstance(field, str):
                pattern = re.escape(field)
            else:
                if field.name in kwargs:
                    value = re.escape(kwargs[field.name])
                else:
                    value = field.pattern
                pattern = '(?P<%s>%s)' % (field.name, value)
            patterns.append(pattern)
        return '^%s$' % re.escape(self.separator).join(patterns)

    def encode(self):
        return self.separator.join([str(field) for field in self])

    def decode(self):
        pattern = self.pattern(**self.params)
        matches = re.match(pattern, self.encoded)
        if matches is None:
            return None
        params = matches.groupdict()
        res = {}
        for field in self.fields:
            if isinstance(field, str):
                continue
            if field.name in params:
                value = params[field.name]
                res[field.name] = field(value).decode()
            else:
                res[field.name] = field.default
        return res


class Error(Query):
    fields = ('0.0', Field.ErrorNo)


class Polling(Query):
    fields = (Field.Padding, Field.HostName, '')


class RxInitialize(Query):
    fields = (Field.Data, Field.HexCount, Field.ID, '')


class RxSend(Query):
    fields = (Field.Data, Field.HexSequence, Field.ID, '')


class Receive(Query):
    fields = (Field.Padding,
              Field.HexSequence, Field.ID, Field.HostName, '')
<commit_msg>Fix query<commit_after>import re
import field as Field


class Query(list):
    fields = ()
    separator = '.'

    def __init__(self, encoded=None, **kwargs):
        list.__init__(self)
        self.encoded = str(encoded)
        self.params = kwargs
        for field in self.fields:
            if isinstance(field, str):
                self.append(field)
                continue
            if field.name in kwargs:
                value = kwargs[field.name]
                self.append(field(value))
            else:
                self.append(field())

    def __bytes__(self):
        return self.encode().encode('utf-8')

    def __str__(self):
        return self.encode()

    def pattern(self, **kwargs):
        patterns = []
        for field in self.fields:
            if isinstance(field, str):
                pattern = re.escape(field)
            else:
                if field.name in kwargs:
                    value = re.escape(kwargs[field.name])
                else:
                    value = field.pattern
                pattern = '(?P<%s>%s)' % (field.name, value)
            patterns.append(pattern)
        return '^%s$' % re.escape(self.separator).join(patterns)

    def encode(self):
        return self.separator.join([str(field) for field in self])

    def decode(self):
        pattern = self.pattern(**self.params)
        matches = re.match(pattern, self.encoded)
        if matches is None:
            return None
        params = matches.groupdict()
        res = {}
        for field in self.fields:
            if isinstance(field, str):
                continue
            if field.name in params:
                value = params[field.name]
                res[field.name] = field(value).decode()
            else:
                res[field.name] = field.default
        return res


class Error(Query):
    fields = ('0.0', Field.ErrorNo)


class Polling(Query):
    fields = (Field.Padding, Field.HostName, '')


class RxInitialize(Query):
    fields = (Field.HexCount, Field.ID, Field.Data, '')


class RxSend(Query):
    fields = (Field.HexSequence, Field.ID, Field.Data, '')


class Receive(Query):
    fields = (Field.Padding,
              Field.HexSequence, Field.ID, Field.HostName, '')

{'fraction_non_alphanumeric': 0.11801242236024845, 'fraction_numerical': 0.0013309671694764862, 'mean_word_length': 1.7360436893203883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3568863', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1221, 'n_words': 301}