<commit_before>import random

import faker

from mongoframes.queries import Q

__all__ = [
    'DictOf',
    'Faker',
    'Lambda',
    'ListOf',
    'Reference',
    'Static',
    'SubFactory',
    'Unique'
    ]


class Maker:
    """
    A base class for all Maker classes.
    """

    def __call__(self, *args):
        if args:
            return self._finish(*args)
        return self._assemble()

    def reset(self):
        """Reset the maker instance"""
        pass

    def _assemble(self):
        return None

    def _finish(self, value):
        return value

    @staticmethod
    def get_fake():
        """Return a shared faker factory used to generate fake data"""
        if not hasattr(Maker, '_fake'):
            Maker._fake = faker.Factory.create()
        return Maker._fake


class DictOf(Maker):
    """
    Make a dictionary of key/values where each value is a set value or generated
    using a maker.
    """

    def __init__(self, table):

        # The table of keyword arguments that will be used to generate the
        # dictionary.
        self._table = table

    def _assemble(self):
        table = {}
        for k, v in self._table.items():
            if isinstance(v, Maker):
                table[k] = v._assemble()
            else:
                table[k] = None
        return table

    def _finish(self, value):
        table = {}
        for k, v in self._table.items():
            if isinstance(v, Maker):
                table[k] = slef._table[k]._finish(value[k])
            else:
                table[k] = slef._table[k]

        return table


class Faker(Maker):
    """
    Use any faker provider to generate a value (see
    http://fake-factory.readthedocs.io/)
    """

    def __init__(self, provider, assembler=True, **kwargs):

        # The provider that will be used to generate the value
        self._provider = provider

        # Flag indicating if the providers should be called in _assemble (True)
        # or _finish (False).
        self._assembler = assembler

        # The keyword arguments for the provider
        self._kwargs = kwargs

    def _assemble(self):
        if not self._assembler:
            return None
        return getattr(self.get_fake(), self._provider)(**self._kwargs)

    def _finish(self, value):
        if self._assembler:
            return value
        return getattr(self.get_fake(), self._provider)(**self._kwargs)


class Lambda(Maker):
    """
    Use a lambda function to generate a value.
    """

    def __init__(self, func, assembler=True):

        # The function to call
        self._func = func

        # Flag indicating if the lambda function should be called in _assemble
        # (True) or _finish (False).
        self._assembler = assembler

    def _assemble(self):
        if self._assembler:
            return self._func()
        return None

    def _finish(self, value):
        if self._assembler:
            return value
        return self._func()


class ListOf(Maker):
    """<commit_msg>Added support for configuring lambda to finish and assemble.<commit_after>import random

import faker

from mongoframes.queries import Q

__all__ = [
    'DictOf',
    'Faker',
    'Lambda',
    'ListOf',
    'Reference',
    'Static',
    'SubFactory',
    'Unique'
    ]


class Maker:
    """
    A base class for all Maker classes.
    """

    def __call__(self, *args):
        if args:
            return self._finish(*args)
        return self._assemble()

    def reset(self):
        """Reset the maker instance"""
        pass

    def _assemble(self):
        return None

    def _finish(self, value):
        return value

    @staticmethod
    def get_fake():
        """Return a shared faker factory used to generate fake data"""
        if not hasattr(Maker, '_fake'):
            Maker._fake = faker.Factory.create()
        return Maker._fake


class DictOf(Maker):
    """
    Make a dictionary of key/values where each value is a set value or generated
    using a maker.
    """

    def __init__(self, table):

        # The table of keyword arguments that will be used to generate the
        # dictionary.
        self._table = table

    def _assemble(self):
        table = {}
        for k, v in self._table.items():
            if isinstance(v, Maker):
                table[k] = v._assemble()
            else:
                table[k] = None
        return table

    def _finish(self, value):
        table = {}
        for k, v in self._table.items():
            if isinstance(v, Maker):
                table[k] = slef._table[k]._finish(value[k])
            else:
                table[k] = slef._table[k]

        return table


class Faker(Maker):
    """
    Use any faker provider to generate a value (see
    http://fake-factory.readthedocs.io/)
    """

    def __init__(self, provider, assembler=True, **kwargs):

        # The provider that will be used to generate the value
        self._provider = provider

        # Flag indicating if the providers should be called in _assemble (True)
        # or _finish (False).
        self._assembler = assembler

        # The keyword arguments for the provider
        self._kwargs = kwargs

    def _assemble(self):
        if not self._assembler:
            return None
        return getattr(self.get_fake(), self._provider)(**self._kwargs)

    def _finish(self, value):
        if self._assembler:
            return value
        return getattr(self.get_fake(), self._provider)(**self._kwargs)


class Lambda(Maker):
    """
    Use a lambda function to generate a value.
    """

    def __init__(self, func, assembler=True, finisher=False):

        # The function to call
        self._func = func

        # Flag indicating if the lambda function should be called in _assemble
        self._assembler = assembler

        # Flag indicating if the lambda function should be called in _finish
        self._finisher = finisher

    def _assemble(self):
        if self._assembler:
            return self._func()
        return None

    def _finish(self, value):
        if self._finisher:
            return self._func(value)
        return value


class ListOf(Maker):
    """
{'fraction_non_alphanumeric': 0.09623430962343096, 'fraction_numerical': 0.0, 'mean_word_length': 1.9623450905624404, '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': '4923953', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1721, 'n_words': 568}