<commit_before>"""Attribute-value matrix."""

from matcher import Matcher
from machine import Machine

class AVM(object):
    TYPE, REQUIRED, DEFAULT, VALUE = xrange(4)

    def __init__(self, name):
        self.name = name
        self.__data = {}  # {key: [type, required, default_value, value]}

    def add_attribute(self, key, datatype, required=False, default_value=None):
        """Adds a new attribute to the "matrix"."""
        if not isinstance(required, bool):
            raise ValueError("required must be a bool, not " + type(required))
        if not isinstance(datatype, Matcher):
            raise ValueError("datatype must be a Matcher, not " +
                             type(datatype))
        self.__data[key] = [datatype, required, default_value, default_value]
        # TODO: do we need a default_value?

    def satisfied(self):
        """Returns @c True, if all required arguments are filled in."""
        for value in self.__data.values():
            if value[AVM.REQUIRED] and value[AVM.VALUE] is None:
                return False
        else:
            return True

    def get_attribute(self, key):
        """Returns the whole attribute data tuple for @p key."""
        return self.__data[key]

    def get_field(self, key, field):
        """
        Returns the specified field from the data tuple for @p key. The valid
        values for @p field are @c TYPE, @c REQUIRED, @c DEFAULT and @c VALUE.
        """
        return self.__data[key][field]

    def get_dict(self):
        """Returns the attribute-value dictionary in a Python dict."""
        ret = dict((k, v[AVM.VALUE]) for k, v in self.__data.iteritems())
        ret['__NAME__'] = self.name
        return ret

    def get_basic_dict(self):
        """
        Returns the attribute-value dictionary in a Python dict, all Machine
        values replaced by their printnames.
        """
        ret = self.get_dict()
        for k, v in ret.iteritems():
            if isinstance(v, Machine):
                ret[k] = unicode(v)
        return ret

    def __getitem__(self, key):
        """Gets the current value of an attribute."""
        return self.__data[key][AVM.VALUE]

    def __setitem__(self, key, value):
        """Sets the current value of an attribute."""
        self.__data[key][AVM.VALUE] = value

    def __iter__(self):
        """Iterates through the attribute keys."""
        return self.__data.__iter__()<commit_msg>modified:   avm.py AVM.clear()<commit_after>"""Attribute-value matrix."""

from matcher import Matcher
from machine import Machine

class AVM(object):
    TYPE, REQUIRED, DEFAULT, VALUE = xrange(4)

    def __init__(self, name):
        self.name = name
        self.__data = {}  # {key: [type, required, default_value, value]}

    def add_attribute(self, key, datatype, required=False, default_value=None):
        """Adds a new attribute to the "matrix"."""
        if not isinstance(required, bool):
            raise ValueError("required must be a bool, not " + type(required))
        if not isinstance(datatype, Matcher):
            raise ValueError("datatype must be a Matcher, not " +
                             type(datatype))
        self.__data[key] = [datatype, required, default_value, default_value]
        # TODO: do we need a default_value?

    def satisfied(self):
        """Returns @c True, if all required arguments are filled in."""
        for value in self.__data.values():
            if value[AVM.REQUIRED] and value[AVM.VALUE] is None:
                return False
        else:
            return True

    def get_attribute(self, key):
        """Returns the whole attribute data tuple for @p key."""
        return self.__data[key]

    def get_field(self, key, field):
        """
        Returns the specified field from the data tuple for @p key. The valid
        values for @p field are @c TYPE, @c REQUIRED, @c DEFAULT and @c VALUE.
        """
        return self.__data[key][field]

    def get_dict(self):
        """Returns the attribute-value dictionary in a Python dict."""
        ret = dict((k, v[AVM.VALUE]) for k, v in self.__data.iteritems())
        ret['__NAME__'] = self.name
        return ret

    def get_basic_dict(self):
        """
        Returns the attribute-value dictionary in a Python dict, all Machine
        values replaced by their printnames.
        """
        ret = self.get_dict()
        for k, v in ret.iteritems():
            if isinstance(v, Machine):
                ret[k] = unicode(v)
        return ret

    def clear(self):
        keys = self.__data.keys()
        for key in keys:
            datatype, required, default_value, _ = self.__data[key]
            self.__data[key] = datatype, required, default_value, default_value

    def __getitem__(self, key):
        """Gets the current value of an attribute."""
        return self.__data[key][AVM.VALUE]

    def __setitem__(self, key, value):
        """Sets the current value of an attribute."""
        self.__data[key][AVM.VALUE] = value

    def __iter__(self):
        """Iterates through the attribute keys."""
        return self.__data.__iter__()
{'fraction_non_alphanumeric': 0.11029126213592233, 'fraction_numerical': 0.0003883495145631068, 'mean_word_length': 2.304041051956382, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1291456', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1409, 'n_words': 492}