<commit_before>from solar.dblayer.model import (Model, Field, IndexField,
                                 IndexFieldWrp,
                                 DBLayerException,
                                 requires_clean_state, check_state_for,
                                 StrInt,
                                 IndexedField)
from types import NoneType
from operator import itemgetter
from enum import Enum
from itertools import groupby

InputTypes = Enum('InputTypes',
                  'simple list hash list_hash')

class DBLayerSolarException(DBLayerException):
    pass


class InputsFieldWrp(IndexFieldWrp):

    _simple_types = (NoneType, int, float, basestring, str, unicode)

    def __init__(self, *args, **kwargs):
        super(InputsFieldWrp, self).__init__(*args, **kwargs)
        # TODO: add cache for lookup
        self._cache = {}

    def _input_type(self, resource, name):
        # XXX: it could be worth to precalculate it
        if ':' in name:
            name = name.split(":", 1)[0]
        schema = resource.meta_inputs[name]['schema']
        if isinstance(schema, self._simple_types):
            return InputTypes.simple
        if isinstance(schema, list):
            if len(schema) > 0 and isinstance(schema[0], dict):
                return InputTypes.list_hash
            return InputTypes.list
        if isinstance(schema, dict):
            return InputTypes.hash
        raise Exception("Unknown type")

    def _edges(self):
        inst = self._instance
        start = inst.key
        my_ind_name = '{}_recv_bin'.format(self.fname)
        res = inst._get_index(my_ind_name,
                              startkey=start + '|',
                              endkey=start + '|~',
                              return_terms=True,
                              max_results=99999).results
        vals = map(itemgetter(0), res)
        for val in vals:
            data = val.split('|')
            dlen = len(data)
            my_resource = data[0]
            my_input = data[1]
            other_resource = data[2]
            other_input = data[3]
            if dlen == 5:
                meta = None
            elif dlen == 7:
                meta = {'destination_key': data[5],
                        'tag': data[4]}
            else:
                raise Exception("Unsupported case")
            yield (my_resource, my_input), (other_resource, other_input), meta


    def _connect_my_simple(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        types_mapping = '|{}_{}'.format(my_type.value, other_type.value)
        my_ind_name = '{}_recv_bin'.format(self.fname)
        my_ind_val = '{}|{}|{}|{}'.format(my_resource.key,
                                          my_inp_name,
                                          other_resource.key,
                                          other_inp_name)
        my_ind_val += types_mapping

        real_my_type = self._input_type(my_resource, my_inp_name)
        if real_my_type == InputTypes.simple:
            for ind_name, ind_value in my_resource._riak_object.indexes:
                if ind_name == my_ind_name:
                    mr, mn, _ = ind_value.split('|', 2)
                    if mr == my_resource.key and mn == my_inp_name:
                        my_resource._remove_index(ind_name, ind_value)
                        break

        my_resource._add_index(my_ind_name, my_ind_val)
        return my_inp_name

    def _connect_other_simple(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        other_ind_name = '{}_emit_bin'.format(self.fname)
        other_ind_val = '{}|{}|{}|{}'.format(other_resource.key,
                                             other_inp_name,
                                             my_resource.key,
                                             my_inp_name)

        real_my_type = self._input_type(my_resource, my_inp_name)
        if real_my_type == InputTypes.simple:
            for ind_name, ind_value in my_resource._riak_object.indexes:
                if ind_name == other_ind_name:
                    mr, mn = ind_value.rsplit('|')[2:]
                    if mr == my_resource.key and mn == my_inp_name:
                        my_resource._remove_index(ind_name, ind_value)
                        break

        my_resource._add_index(other_ind_name,
                               other_ind_val)
        return other_inp_name


    def __contains__(self, name):
        try:
            self._has_own_input(name)
        except Exception:
            return False
        else:
            return True

    def __iter__(self):
        for name in self._instance._data_container:
            yield name

    def _connect_my_list(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        ret = self._connect_my_simple(my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type)
        return ret

    def _connect_my_hash(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        my_key, my_val = my_inp_name.split(':', 1)
        if '|' in my_val:
            my_val, my_tag = my_val.split('|', 1)
        else:
            my_tag = other_resource.name
        types_mapping = '|{}_{}'.format(my_type.value, other_type.value)
        my_ind_name = '{}_recv_bin'.format(self.fname)
        my_ind_val = '{}|{}|{}|{}|{}|{}'.format(my_resource.key,
                                                my_key,
                                                other_resource.key,
                                                other_inp_name,
                                                my_tag,<commit_msg>Corrected __iter__ for inputs<commit_after>from solar.dblayer.model import (Model, Field, IndexField,
                                 IndexFieldWrp,
                                 DBLayerException,
                                 requires_clean_state, check_state_for,
                                 StrInt,
                                 IndexedField)
from types import NoneType
from operator import itemgetter
from enum import Enum
from itertools import groupby

InputTypes = Enum('InputTypes',
                  'simple list hash list_hash')

class DBLayerSolarException(DBLayerException):
    pass


class InputsFieldWrp(IndexFieldWrp):

    _simple_types = (NoneType, int, float, basestring, str, unicode)

    def __init__(self, *args, **kwargs):
        super(InputsFieldWrp, self).__init__(*args, **kwargs)
        # TODO: add cache for lookup
        self._cache = {}

    def _input_type(self, resource, name):
        # XXX: it could be worth to precalculate it
        if ':' in name:
            name = name.split(":", 1)[0]
        schema = resource.meta_inputs[name]['schema']
        if isinstance(schema, self._simple_types):
            return InputTypes.simple
        if isinstance(schema, list):
            if len(schema) > 0 and isinstance(schema[0], dict):
                return InputTypes.list_hash
            return InputTypes.list
        if isinstance(schema, dict):
            return InputTypes.hash
        raise Exception("Unknown type")

    def _edges(self):
        inst = self._instance
        start = inst.key
        my_ind_name = '{}_recv_bin'.format(self.fname)
        res = inst._get_index(my_ind_name,
                              startkey=start + '|',
                              endkey=start + '|~',
                              return_terms=True,
                              max_results=99999).results
        vals = map(itemgetter(0), res)
        for val in vals:
            data = val.split('|')
            dlen = len(data)
            my_resource = data[0]
            my_input = data[1]
            other_resource = data[2]
            other_input = data[3]
            if dlen == 5:
                meta = None
            elif dlen == 7:
                meta = {'destination_key': data[5],
                        'tag': data[4]}
            else:
                raise Exception("Unsupported case")
            yield (my_resource, my_input), (other_resource, other_input), meta


    def _connect_my_simple(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        types_mapping = '|{}_{}'.format(my_type.value, other_type.value)
        my_ind_name = '{}_recv_bin'.format(self.fname)
        my_ind_val = '{}|{}|{}|{}'.format(my_resource.key,
                                          my_inp_name,
                                          other_resource.key,
                                          other_inp_name)
        my_ind_val += types_mapping

        real_my_type = self._input_type(my_resource, my_inp_name)
        if real_my_type == InputTypes.simple:
            for ind_name, ind_value in my_resource._riak_object.indexes:
                if ind_name == my_ind_name:
                    mr, mn, _ = ind_value.split('|', 2)
                    if mr == my_resource.key and mn == my_inp_name:
                        my_resource._remove_index(ind_name, ind_value)
                        break

        my_resource._add_index(my_ind_name, my_ind_val)
        return my_inp_name

    def _connect_other_simple(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        other_ind_name = '{}_emit_bin'.format(self.fname)
        other_ind_val = '{}|{}|{}|{}'.format(other_resource.key,
                                             other_inp_name,
                                             my_resource.key,
                                             my_inp_name)

        real_my_type = self._input_type(my_resource, my_inp_name)
        if real_my_type == InputTypes.simple:
            for ind_name, ind_value in my_resource._riak_object.indexes:
                if ind_name == other_ind_name:
                    mr, mn = ind_value.rsplit('|')[2:]
                    if mr == my_resource.key and mn == my_inp_name:
                        my_resource._remove_index(ind_name, ind_value)
                        break

        my_resource._add_index(other_ind_name,
                               other_ind_val)
        return other_inp_name


    def __contains__(self, name):
        try:
            self._has_own_input(name)
        except Exception:
            return False
        else:
            return True

    def __iter__(self):
        for name in self._instance._data_container[self.fname]:
            yield name

    def _connect_my_list(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        ret = self._connect_my_simple(my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type)
        return ret

    def _connect_my_hash(self, my_resource, my_inp_name, other_resource, other_inp_name, my_type, other_type):
        my_key, my_val = my_inp_name.split(':', 1)
        if '|' in my_val:
            my_val, my_tag = my_val.split('|', 1)
        else:
            my_tag = other_resource.name
        types_mapping = '|{}_{}'.format(my_type.value, other_type.value)
        my_ind_name = '{}_recv_bin'.format(self.fname)
        my_ind_val = '{}|{}|{}|{}|{}|{}'.format(my_resource.key,
                                                my_key,
                                                other_resource.key,
                                                other_inp_name,
                                                my_tag,
{'fraction_non_alphanumeric': 0.09023797116553761, 'fraction_numerical': 0.003821434775056453, 'mean_word_length': 1.61051915665382, 'pattern_counts': {'":': 2, '<': 3, '<?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': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7570973', 'n_tokens_mistral': 3301, 'n_tokens_neox': 3205, 'n_words': 656}