<commit_before>#!/usr/bin/env python
from construct import *


class Across10Internal(Adapter):
    LEVEL_MAPPING = {
        11: 81, 12: 11, 13: 82, 14: 84, 15: 17, 16: 83, 17: 18,
        18: 19, 19: 20, 20: 21, 21: 22, 22: 80, 23: 38
    }

    ACROSS10_LEVELS = 24

    def __init__(self, subcon):
        super(Across10Internal, self).__init__(subcon)

    def _decode(self, obj, context):
        assert obj < self.ACROSS10_LEVELS
        if obj in self.LEVEL_MAPPING:
            return self.LEVEL_MAPPING[obj]
        return obj

    def _encode(self, obj, context):
        for k, v in self.LEVEL_MAPPING.iteritems():
            if obj == v:
                return k
        assert obj < self.ACROSS10_LEVELS
        return obj


class SlicingAdapter(Adapter):
    """
    Adapter to convert a dict of several arrays of the same length
    into an array of dicts.

    {"a": [0, 1], "b": [2, 3]} -> [{"a": 0, "b": 2}, {"a": 1, "b": 3}]
    """

    def _decode(self, obj, context):
        result = ListContainer()
        lengths = set([len(x) for x in obj.values()])
        assert len(lengths) == 1
        for i in xrange(lengths.pop()):
            result.append(Container((k, v[i]) for k, v in obj.items()))
        return result

    def _encode(self, obj, context):
        result = Container()
        keys = [x for x in obj[0].keys()]
        assert all([x for x in v.keys()] == keys for v in obj)
        for k in keys:
            result[k] = ListContainer(x[k] for x in obj)
        return result


# noinspection PyPep8,PyUnresolvedReferences
Event = Struct(
    "time"   / Float64l,
    "object" / Int16sl,
    "type"   / Int8ul,
               Int8ul,
    "volume" / Float32l,
    IfThenElse(this.type == 0,
               Check(this.object >= 0),
               Check(this.object == -1))
)

# noinspection PyPep8,PyUnresolvedReferences
Across10Header = Struct(
    "version"      / Computed(lambda ctx: 100),
    "link_number"  / Computed(lambda ctx: 0),
    "internal_num" / Across10Internal(Int32ul)
)

# noinspection PyPep8,PyUnresolvedReferences
Across12Header = Struct(
    "version"      / Const(Int32ul, 120),
    "link_number"  / Int32ul,
    "internal_num" / Int32ul,
    IfThenElse(this.link_number > 0,
               Check(this.internal_num == 0xFFFFFFFF),
               Check(this.internal_num <= 90))
)

# noinspection PyProtectedMember,PyPep8,PyUnresolvedReferences
Replay = Struct(
    "frames_num" / Int32ul,
    Embedded(Select(Across12Header, Across10Header)),<commit_msg>Event types<commit_after>#!/usr/bin/env python
from construct import *


class Across10Internal(Adapter):
    LEVEL_MAPPING = {
        11: 81, 12: 11, 13: 82, 14: 84, 15: 17, 16: 83, 17: 18,
        18: 19, 19: 20, 20: 21, 21: 22, 22: 80, 23: 38
    }

    ACROSS10_LEVELS = 24

    def __init__(self, subcon):
        super(Across10Internal, self).__init__(subcon)

    def _decode(self, obj, context):
        assert obj < self.ACROSS10_LEVELS
        if obj in self.LEVEL_MAPPING:
            return self.LEVEL_MAPPING[obj]
        return obj

    def _encode(self, obj, context):
        for k, v in self.LEVEL_MAPPING.iteritems():
            if obj == v:
                return k
        assert obj < self.ACROSS10_LEVELS
        return obj


class SlicingAdapter(Adapter):
    """
    Adapter to convert a dict of several arrays of the same length
    into an array of dicts.

    {"a": [0, 1], "b": [2, 3]} -> [{"a": 0, "b": 2}, {"a": 1, "b": 3}]
    """

    def _decode(self, obj, context):
        result = ListContainer()
        lengths = set([len(x) for x in obj.values()])
        assert len(lengths) == 1
        for i in xrange(lengths.pop()):
            result.append(Container((k, v[i]) for k, v in obj.items()))
        return result

    def _encode(self, obj, context):
        result = Container()
        keys = [x for x in obj[0].keys()]
        assert all([x for x in v.keys()] == keys for v in obj)
        for k in keys:
            result[k] = ListContainer(x[k] for x in obj)
        return result


# noinspection PyPep8,PyUnresolvedReferences
Event = Struct(
    "time"   / Float64l,
    "object" / Int16sl,
    "type"   / Padded(2, Enum(Int8ul, object_taken=0, bounce=1, failure=2,
                                      success=3, apple=4, changedir=5,
                                      right_volt=6, left_volt=7)),
    "volume" / Float32l,
    IfThenElse(this.type == "object_taken",
               Check(this.object >= 0),
               Check(this.object == -1))
)

# noinspection PyPep8,PyUnresolvedReferences
Across10Header = Struct(
    "version"      / Computed(lambda ctx: 100),
    "link_number"  / Computed(lambda ctx: 0),
    "internal_num" / Across10Internal(Int32ul)
)

# noinspection PyPep8,PyUnresolvedReferences
Across12Header = Struct(
    "version"      / Const(Int32ul, 120),
    "link_number"  / Int32ul,
    "internal_num" / Int32ul,
    IfThenElse(this.link_number > 0,
               Check(this.internal_num == 0xFFFFFFFF),
               Check(this.internal_num <= 90))
)

# noinspection PyProtectedMember,PyPep8,PyUnresolvedReferences
Replay = Struct(
    "frames_num" / Int32ul,
    Embedded(Select(Across12Header, Across10Header)),
{'fraction_non_alphanumeric': 0.12576687116564417, 'fraction_numerical': 0.047354294478527605, 'mean_word_length': 2.359304571796523, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1253058', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1720, 'n_words': 466}