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

from matcher import Matcher
from StringIO import StringIO

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

    def __init__(self):
        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 __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__()

    def __unicode__(self):
        return u'{' + u', '.join(u'{0}: {1}'.format(key, self[key]) for key in self) + u'}'

<commit_msg>AVM class can now return a dict of attribute key - value pairs. 	modified:   ../pymachine/src/avm.py<commit_after>"""Attribute-value matrix."""

from matcher import Matcher

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

    def __init__(self):
        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."""
        return dict((k, v[AVM.VALUE]) for k, v in self.__data.iteritems())

    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__()

    def __unicode__(self):
        return u'{' + u', '.join(u'{0}: {1}'.format(key, self[key]) for key in self) + u'}'


{'fraction_non_alphanumeric': 0.1204675572519084, 'fraction_numerical': 0.0014312977099236641, 'mean_word_length': 2.5058528428093645, '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': '2787110', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1181, 'n_words': 415}