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.39k stars 330 forks source link

Generate structured and non contradictory data #430

Closed duckyou closed 6 years ago

duckyou commented 6 years ago

What if we implement something like this:

>>> import json
>>> from mimesis.cards import PersonCard
>>> json.dumps(next(p), ident=4)
{
    "first_name": "Julio",
    "last_name": "Santos",
    "email": "juliosantos85@mail.com",
    "age": 33,
    "child_count": 3,
    "gender": "male",
    "work_experience": 11
    ...
}

Or upgrade schema provider to return non contradictory data. What do you think about this?

lk-geimfari commented 6 years ago

I think that it's partially duplicate schema.Field.

lk-geimfari commented 6 years ago

:thinking: It's better to add good documentation about schema.Field to docs I think. Structured data is too specific case which better to solve using custom schema (schema.Field, schema.Schema).

duckyou commented 6 years ago

@lk-geimfari Schema field can return contradict data. For example female first name and male last name.

lk-geimfari commented 6 years ago

@duckyou But you can use **kwargs for it.

_ = Field('ru')

name = _('name', gender=Gender.FEMALE)
surname = _('surname', gender=Gender.FEMALE)
duckyou commented 6 years ago

@lk-geimfari But if i want random gender? Ok... Some time ago there was a proposal to implement feature which is allow use enums with random.choice function.

Something like this:

class MimesisEnumMeta(EnumMeta):
    """Updated metaclass with index support."""

    def __getitem__(self, key):
        """Return enum item if it exist.

        :param key: index or name
        :return: enum item
        """
        if isinstance(key, int):
            key = self._member_names_[key]
        return self._member_map_[key]

class EnumMixin(metaclass=MimesisEnumMeta):
    """Enum mixin with updated metaclass."""

    pass

class Gender(EnumMixin, Enum):
    """Represents genders.

    Value for a lot of methods which
    takes argument ``gender``.
    """

    FEMALE = 'female'
    MALE = 'male'

We can use like:

_ = Field('ru')

gender_enum = random.choice(Gender)

name = _('name', gender=gender_enum)
surname = _('surname', gender=gender_enum)
lk-geimfari commented 6 years ago

There is no need to implement it in a core. A user can do it manually:

random_gender = lambda: random.choice(list(Gender)) 
name = _('name', gender=random_gender())
duckyou commented 6 years ago

@lk-geimfari You goddamn right :bangbang: Also last questions: what about build-in schemas? Implement some schemas or omit it?

lk-geimfari commented 6 years ago

@duckyou I think that we should implement it as a third-party package, for example, named mimesis-schemas. I think that this is a compromise between the simplicity of the core and its capabilities.

from mimesis_schema import SomeCard
# ...
# ....
lk-geimfari commented 6 years ago

In core package, we need to improve existing features and fix bugs. It's our priority.

duckyou commented 6 years ago

@lk-geimfari you are reading my mind :wink: :+1: Can you plz create mimesis_schema repo?

lk-geimfari commented 6 years ago

@duckyou You can do it by yourself too. You're a member of mimesis-lab

Anyway, i have created repo for you: https://github.com/mimesis-lab/mimesis_schema

sobolevn commented 6 years ago

Nice! Closing.