Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement truth probability for boolean generator #1793

Merged
merged 1 commit into from
Feb 13, 2023
Merged

Implement truth probability for boolean generator #1793

merged 1 commit into from
Feb 13, 2023

Conversation

Kolyunya
Copy link
Contributor

@Kolyunya Kolyunya commented Feb 11, 2023

What was wrong

Currently, it's impossible to generate booleans that are biased towards True or False values.

How this fixes it

This patch adds an optional truth_probability argument to the pybool() method which allows to generate booleans that are biased towards True or False values.

Having the new argument is optional and defaults to 50%, this patch is fully backward compatible.

Usage example

Imagine we need to generate random booleans but we only want 10% of them to be equal to True. After this patch it would be possible using the following code:

boolean = faker.pybool(10)
boolean = faker.pybool(truth_probability=10)

Side notes

Our PHP friends already have this feature implemented which proves that it is actually useful for the end-users.

PHP implementation has chanceOfGettingTrue as the name of the argument, but I believe that truth_probability would fit better.

Seems like it's impossible to use parametrize in that test case because it extends unittest.TestCase, that's why I added a helper method to the test class.

Copy link
Collaborator

@fcurella fcurella left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@fcurella fcurella merged commit e2b8bad into joke2k:master Feb 13, 2023
@Kolyunya
Copy link
Contributor Author

@fcurella my pleasure!

@Kolyunya Kolyunya deleted the implement-truth-probability-for-boolean-generator branch February 13, 2023 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants