<commit_before>"""
A very light-weight "model" structure to lazy-load (or generate) and save YAML
from Python objects composed of specialized fields
"""

import os

from yaml import safe_load as yaml_load, dump as yaml_dump


class NoValueError(Exception):
    """
    Raised when a field has no value, no generator and no default
    """
    def __init__(self, cls, var_name, *args):
        super(NoValueError, self).__init__(*args)
        self.cls = cls
        self.var_name = var_name


class OnAccess(object):  # pylint:disable=too-few-public-methods
    """
    Mark a field as having a one-time call associated with it's retrieval
    """
    var_name = None
    func = None

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

    def property_value(self):
        """
        Generate a property to put in place of this object instance on the
        concrete model object
        """
        def getter(self_):
            """
            Getter for the attribute value that checks for a cached value
            before trying to generate/acquire it
            """
            # pylint:disable=protected-access
            try:
                return self_._lazy_vals[self.var_name]
            except KeyError:
                self_._lazy_vals[self.var_name] = self.func(self_)
                return self_._lazy_vals[self.var_name]

        def setter(self_, value):
            """
            Basic setter to write the value to the cache dict
            """
            # pylint:disable=protected-access
            self_._lazy_vals[self.var_name] = value
<commit_msg>Add i/o translations to YAML model<commit_after>"""
A very light-weight "model" structure to lazy-load (or generate) and save YAML
from Python objects composed of specialized fields
"""

import os

from yaml import safe_load as yaml_load, dump as yaml_dump


class NoValueError(Exception):
    """
    Raised when a field has no value, no generator and no default
    """
    def __init__(self, cls, var_name, *args):
        super(NoValueError, self).__init__(*args)
        self.cls = cls
        self.var_name = var_name


class OnAccess(object):  # pylint:disable=too-few-public-methods
    """
    Mark a field as having a one-time call associated with it's retrieval
    """
    var_name = None
    func = None

    def __init__(self, func, input_transform=None, output_transform=None):
        self.func = func
        self.input_transform = input_transform
        self.output_transform = output_transform

    def property_value(self):
        """
        Generate a property to put in place of this object instance on the
        concrete model object
        """
        def getter(self_):
            """
            Getter for the attribute value that checks for a cached value
            before trying to generate/acquire it
            """
            # pylint:disable=protected-access
            try:
                value = self_._lazy_vals[self.var_name]
            except KeyError:
                self_._lazy_vals[self.var_name] = self.func(self_)
                value = self_._lazy_vals[self.var_name]

            if self.output_transform:
                return self.output_transform(value)
            else:
                return value

        def setter(self_, value):
            """
            Basic setter to write the value to the cache dict
            """
            if self.input_transform:
                value = self.input_transform(value)

            # pylint:disable=protected-access
            self_._lazy_vals[self.var_name] = value

{'fraction_non_alphanumeric': 0.07866741321388578, 'fraction_numerical': 0.0, 'mean_word_length': 2.082830025884383, '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': '6159600', 'n_tokens_mistral': 999, 'n_tokens_neox': 959, 'n_words': 306}