Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'locale', locales.LIST_OF_LOCALES,
)
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()
import uuid
import random
from mimesis import Person
from mimesis import Address
from mimesis import locales
from mimesis import Text
from collections import namedtuple
Locales = locales
DataEntity = namedtuple
class Random:
@classmethod
def ustr(cls, prefix=None):
prefix = prefix and prefix + "-" or ""
return "{}{}".format(prefix, uuid.uuid4())
@classmethod
def first_name(cls, locale=Locales.EN):
return Person(locale).first_name()
@classmethod
def last_name(cls, locale=Locales.EN):
return Person(locale).last_name()
data_dir = self._data_dir
if not datafile:
datafile = self._datafile
def get_data(locale_name: str) -> JSON:
"""Pull JSON data from file.
:param locale_name: Locale name.
:return: Content of JSON file as dict.
"""
file_path = Path(data_dir).joinpath(locale_name, datafile)
with open(file_path, 'r', encoding='utf8') as f:
return json.load(f)
separator = locales.LOCALE_SEPARATOR
master_locale = locale.split(separator).pop(0)
data = get_data(master_locale)
if separator in locale:
data = self._update_dict(data, get_data(locale))
self._data = data
def override_locale(self, locale: str = locales.EN,
) -> Generator['BaseDataProvider', None, None]:
"""Context manager which allows overriding current locale.
Temporarily overrides current locale for
locale-dependent providers.
:param locale: Locale.
:return: Provider with overridden locale.
"""
try:
origin_locale = self.locale
self._override_locale(locale)
try:
yield self
finally:
self._override_locale(origin_locale)
def _setup_locale(self, locale: str = locales.DEFAULT_LOCALE) -> None:
"""Set up locale after pre-check.
:param str locale: Locale
:raises UnsupportedLocale: When locale is not supported.
:return: Nothing.
"""
if not locale:
locale = locales.DEFAULT_LOCALE
locale = locale.lower()
if locale not in locales.SUPPORTED_LOCALES:
raise UnsupportedLocale(locale)
self.locale = locale