Mimesis: Support `choice` provider

Created on 21 Jul 2018  路  20Comments  路  Source: lk-geimfari/mimesis

I want to be able to select any value from a given sequence.

Like:

mimesis('multiple_choice', items=['a', 'b', 'c'], number=2)
# => ['b', 'a']

mimesis('multiple_choice', items=['a', 'b', 'c'])
# => ['b']

mimesis('choice', items=['a', 'b', 'c'])
# => 'a'

It seems to be imposible right now.

enhancement help wanted

All 20 comments

I like it. Do you want to implement it in AbstractField?

Sorry, but currently I don't have any time for it.

@sobolevn No no, I meant where it should be implemented in your vision?

I would say that this can be a separate provider.

@sobolevn I have implemented this pretty simple solution to do this. What do you think?

from random import Random


class Choice(object):
    def __init__(self, seed=None):
        self.random = Random()
        if seed is not None:
            self.random.seed = seed

    def __call__(self, mode='choice', items=None, number= 1):
        if mode == 'multiple_choice':
            return [self.random.choice(items) for _ in range(number)]
        return self.random.choice(items)

Result:

>>> from mimesis.providers import Choice
>>> choice = Choice()

>>> choice('multiple_choice', items=['a', 'b', 'c'], number=2)
['c', 'c']

>>> choice('multiple_choice', items=['a', 'b', 'c'])
['a']

>>> choice('choice', items=['a', 'b', 'c'])
'b'

@sobolevn Do I correctly understand you?

Yeap, that's it!

I would also recommend to add unique=True param to select only unique items.

@sobolevn Sure, I thought about it too.

@sobolevn How should be selected unique? For example:

choice('multiple_choice, 'items=[1, 2, 3, 1, 1], number=2) selects [1, 1] but there are not unique. Should we return just one element, i.e [1]?

@sobolevn Also I suggest don't use choice and multiple_choice, cuz the choice mode can be detected automatically when number is greater than 1.

Yes, something like that.

But, that's not currently included as a provider. So, I won't be able to call this function from pytest-mimesis or mimesis-factory.

@sobolevn Looks like I misunderstood you.

Now I understand what you meant. I'll do it in in this weekends.

Hi, I started to look at this as a 'hacktoberfest' challenge, but it seems the 'choice' functionality has already been implemented (in mimesis/mimesis/choice.py)? I am assuming that:

that's not currently included as a provider. So, I won't be able to call this function from pytest-mimesis or mimesis-factory

is the reason this issue is not yet closed & has a recent label inviting contributions? Can you please confirm or otherwise that there is still work to do for this issue, namely making 'choice' into a provider. If so, I am happy to try to implement this & put in a PR.

@sadielbartholomew Hi! Yeah, it is a reason why this issue still open. We need to add as a provider. @sobolevn your few words about this feature, please.

P.S And yes, you can work on it, if you're still interested.

Thanks @lk-geimfari, that's great. I'll start looking at this on my lunch break at ~12:00 UTC time.

I've started to implement this (it's currently not correct/finished so please do not review it yet) in a branch here. I'll complete it, including editing the tests & documentation accordingly, in the next few days & put up a PR.

@sadielbartholomew Deal! We'll happy to merge your PR. Thank you and happy hacking!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lk-geimfari picture lk-geimfari  路  6Comments

lk-geimfari picture lk-geimfari  路  5Comments

lk-geimfari picture lk-geimfari  路  3Comments

chris-canipe picture chris-canipe  路  5Comments

lk-geimfari picture lk-geimfari  路  4Comments