How to use the mimesis.schema.Field function in mimesis

To help you get started, we’ve selected a few mimesis examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pytest-dev / pytest-mimesis / pytest_mimesis.py View on Github external
def factory(locale):
        if locale not in cached_instances:
            cached_instances[locale] = Field(locale)
        return cached_instances[locale]
github lk-geimfari / mimesis / tests / test_schema.py View on Github external
@pytest.fixture
def field():
    return Field('en')
github lk-geimfari / mimesis / tests / test_schema.py View on Github external
def test_field(locale):
    filed = Field(locale)
    result = filed('full_name')
    assert result
    assert isinstance(result, str)

    with pytest.raises(UnsupportedField):
        filed('unsupported_field')

    with pytest.raises(UndefinedField):
        filed()
github lk-geimfari / mimesis / tests / test_schema.py View on Github external
def test_field_with_custom_providers():
    field = Field(providers=[USASpecProvider])
    assert field('ssn')
    assert field('usa_provider.ssn')
github mimesis-lab / mimesis-factory / mimesis_factory.py View on Github external
def _get_cached_instance(
        cls,
        locale: Optional[str] = None,
        providers: Optional[_Providers] = None,
    ) -> Field:
        if locale is None:
            locale = cls._default_locale

        key = (locale, providers)
        if key not in cls._cached_instances:
            cls._cached_instances[key] = Field(locale, providers=providers)

        return cls._cached_instances[key]
github mimesis-lab / mimesis-factory / mimesis_factory.py View on Github external
from mimesis.providers.base import BaseProvider
from mimesis.schema import Field, Generic

_CacheKey = Tuple[str, Any]
_Providers = Iterable[Type[BaseProvider]]


class MimesisField(declarations.BaseDeclaration):
    """
    Mimesis integration with FactoryBoy starts here.

    This class provides common interface for FactoryBoy,
    but inside it has Mimesis generators.
    """

    _cached_instances: ClassVar[Dict[_CacheKey, Field]] = {}
    _default_locale: ClassVar[str] = locales.DEFAULT_LOCALE

    def __init__(
        self, field: str, locale: Optional[str] = None, **kwargs,
    ) -> None:
        """
        Creates a field instance.

        The created field is lazy. It also receives build time parameters.
        These parameters are not applied yet.

        Args:
            field: name to be passed to ``Field`` from ``Mimesis``.
            locale: locale to use. This parameter has the highest priority.
            kwargs: optional parameters that would be passed to ``Field``.