<commit_before>from .debugging import debug_print_dict, logger
from .generators import BaseGenerator


def add_field_generators(field_gens, dct):
    for name, gen in dct.items():
        if isinstance(gen, BaseGenerator):
            field_gens[name] = gen


def find_field_generators(obj):
    """
    Return dictionary with the names and instances of
    all tohu.BaseGenerator occurring in the given
    object's class & instance namespaces.
    """

    cls_dict = obj.__class__.__dict__
    obj_dict = obj.__dict__
    logger.debug(f'[FFF]')
    debug_print_dict(cls_dict, 'cls_dict')
    debug_print_dict(obj_dict, 'obj_dict')

    field_gens = {}
    add_field_generators(field_gens, cls_dict)
    add_field_generators(field_gens, obj_dict)

    return field_gens


def attach_new_init_method(obj):
    """
    Replace the existing obj.__init__() method with a new one
    which calls the original one and in addition performs the
    following actions:

    (1) Finds all instances of tohu.BaseGenerator in the namespace
        and collects them in the dictionary `self.field_gens`.
    (2) ..to do..
    """

    orig_init = obj.__init__

    def new_init(self, *args, **kwargs):
        # Call original __init__ function to ensure we pick up
        # any tohu generators that are defined there.
        orig_init(self, *args, **kwargs)

        # Find field generators
        self.field_gens = find_field_generators(self)
        logger.debug(f'Found {len(self.field_gens)} field generator(s):')
        debug_print_dict(self.field_gens)

    obj.__init__ = new_init


def attach_new_reset_method(obj):
    """
    Attach a new `reset()` method to `obj` which resets the internal
    seed generator of `obj` and then resets each of its constituent
    field generators found in `obj.field_gens`.
    """
    #
    # Create and assign automatically generated reset() method
    #

    def new_reset(self, seed=None):
        logger.debug(f'[EEE] Inside automatically generated reset() method for {self} (seed={seed})')
        logger.debug(f'      TODO: reset internal seed generator and call reset() on each child generator')<commit_msg>Spawn generators before attaching them to new custom generator object; print debugging messages about field generator templates and spawned versions<commit_after>from .debugging import debug_print_dict, logger
from .generators import BaseGenerator


def add_field_generators(field_gens, dct):
    for name, gen in dct.items():
        if isinstance(gen, BaseGenerator):
            field_gens[name] = gen


def find_field_generators(obj):
    """
    Return dictionary with the names and instances of
    all tohu.BaseGenerator occurring in the given
    object's class & instance namespaces.
    """

    cls_dict = obj.__class__.__dict__
    obj_dict = obj.__dict__
    logger.debug(f'[FFF]')
    debug_print_dict(cls_dict, 'cls_dict')
    debug_print_dict(obj_dict, 'obj_dict')

    field_gens = {}
    add_field_generators(field_gens, cls_dict)
    add_field_generators(field_gens, obj_dict)

    return field_gens


def attach_new_init_method(obj):
    """
    Replace the existing obj.__init__() method with a new one
    which calls the original one and in addition performs the
    following actions:

    (1) Finds all instances of tohu.BaseGenerator in the namespace
        and collects them in the dictionary `self.field_gens`.
    (2) ..to do..
    """

    orig_init = obj.__init__

    def new_init(self, *args, **kwargs):
        # Call original __init__ function to ensure we pick up
        # any tohu generators that are defined there.
        orig_init(self, *args, **kwargs)

        # Find field generators
        field_gens_templates = find_field_generators(self)
        logger.debug(f'Found {len(field_gens_templates)} field generator template(s):')
        debug_print_dict(field_gens_templates)

        logger.debug('Spawning field generator templates...')
        self.field_gens = {name: gen._spawn() for (name, gen) in field_gens_templates.items()}
        logger.debug(f'Field generatos attached to custom generator:')
        debug_print_dict(self.field_gens)

    obj.__init__ = new_init


def attach_new_reset_method(obj):
    """
    Attach a new `reset()` method to `obj` which resets the internal
    seed generator of `obj` and then resets each of its constituent
    field generators found in `obj.field_gens`.
    """
    #
    # Create and assign automatically generated reset() method
    #

    def new_reset(self, seed=None):
        logger.debug(f'[EEE] Inside automatically generated reset() method for {self} (seed={seed})')
        logger.debug(f'      TODO: reset internal seed generator and call reset() on each child generator')
{'fraction_non_alphanumeric': 0.08643279797125951, 'fraction_numerical': 0.0008453085376162299, 'mean_word_length': 3.3501838235294117, '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': '2332933', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1344, 'n_words': 434}