<commit_before># -*- encoding: utf-8 -*-

from yamlns import namespace as ns
import os


class BadUser(Exception):
    def __init__(self, user):
        super(BadUser, self).__init__(
            "User not found '{}'".format(user))

class BadField(Exception):
    def __init__(self, field):
        super(BadField, self).__init__(
            "Unrecognized user field '{}'".format(field))

class UserInfo(object):
    """This mock object returns user info related to provided keys.
    In your system the substitute of this object should use the ERP
    to get such information.
    """

    def __init__(self, datadir):
        self.datadir = datadir

    def _userData(self, user):
        try:
            return ns.load(os.path.join(self.datadir, user+'.yaml'))
        except Exception:
            raise BadUser(user)

    def language(self, user):
        userdata = self._userData(user)
        try:
            return userdata.lang
        except AttributeError:
            return 'es'

    def getFields(self, user, fields):
        userdata = self._userData(user)

        for field in fields:
            if field not in userdata:
                raise BadField(field)

        return ns([
            (key, value)
            for key,value in userdata.iteritems()
            if key in fields
            ])



# vim: ts=4 sw=4 et<commit_msg>iteritems is not Py3 compatible<commit_after># -*- encoding: utf-8 -*-

from yamlns import namespace as ns
import os


class BadUser(Exception):
    def __init__(self, user):
        super(BadUser, self).__init__(
            "User not found '{}'".format(user))

class BadField(Exception):
    def __init__(self, field):
        super(BadField, self).__init__(
            "Unrecognized user field '{}'".format(field))

class UserInfo(object):
    """This mock object returns user info related to provided keys.
    In your system the substitute of this object should use the ERP
    to get such information.
    """

    def __init__(self, datadir):
        self.datadir = datadir

    def _userData(self, user):
        try:
            return ns.load(os.path.join(self.datadir, user+'.yaml'))
        except Exception:
            raise BadUser(user)

    def language(self, user):
        userdata = self._userData(user)
        try:
            return userdata.lang
        except AttributeError:
            return 'es'

    def getFields(self, user, fields):
        userdata = self._userData(user)

        for field in fields:
            if field not in userdata:
                raise BadField(field)

        return ns([
            (key, value)
            for key,value in userdata.items()
            if key in fields
            ])



# vim: ts=4 sw=4 et
{'fraction_non_alphanumeric': 0.09904622157006603, 'fraction_numerical': 0.0025678650036683784, 'mean_word_length': 2.08135593220339, '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': '4711707', 'n_tokens_mistral': 786, 'n_tokens_neox': 761, 'n_words': 247}