lk-geimfari / mimesis

Mimesis is a robust data generator for Python that can produce a wide range of fake data in multiple languages.
https://mimesis.name
MIT License
4.44k stars 336 forks source link

Locale overrider support #562

Closed lk-geimfari closed 5 years ago

lk-geimfari commented 6 years ago

Feature request

Thesis

So, the idea is very simple - use context manager to temporarily override current locale for provider's instance, just like that:

>>> from mimesis import Person
>>> from mimesis import locales

>>> person = Person(locales.EN)
>>> person.full_name()
'Ozie Melton'

>>> with locales.override(person, locales.RU):
...     person.full_name()

'Симона Богданова'

>>> person.full_name()
'Waldo Foster'

Reasoning

Sometimes we need only some data from other provider and creating an instance each time is not really good, so it's better just override locale.

lk-geimfari commented 6 years ago

@sobolevn What do you think?

sobolevn commented 6 years ago

Why not? Seems like an easy feature.

lk-geimfari commented 5 years ago

I'm thinking about moving it to BaseDataProvider too.

lk-geimfari commented 5 years ago

How it will work:

from mimesis import Person

person = Person('en')
with person.override_locale('ru'):
    person.full_name()

Reasoning:

This feature is something like pull, i.e it's useful only for locale-dependent providers.

lk-geimfari commented 5 years ago

@sobolevn Do you mind?

sobolevn commented 5 years ago

Agree 👍