Faker Optional
Wrapper over other Faker providers to return their value or None. Useful to create data of type Optional[Any].
Installing¶
pip install faker-optional
A Simple Example¶
from faker import Faker
from faker_optional import OptionalProvider
fake = Faker()
fake.add_provider(OptionalProvider)
fake.optional_int()
# None
fake.optional_int()
# 1234
Usage¶
OptionalProvider uses existent faker providers to create the data, so you can use the provider method arguments.
For example, optional_int uses the python provider pyint, so you can use the min_value, max_value, and step arguments. Every optional_ method accepts the float ratio argument between 0 and 1, with a default value of 0.5 to define what percent of results should be None, a greater value will mean that less results will be None.
Check the supported methods. If you want one that's not included, it's really easy to implement it yourself, please make a pull request.
References¶
As most open sourced programs, faker-optional is standing on the shoulders of giants, namely:
- Pytest
- Testing framework, enhanced by the awesome pytest-cases library that made the parametrization of the tests a lovely experience.
- Mypy
- Python static type checker.
- Flakehell
- Python linter with lots of checks.
- Black
- Python formatter to keep a nice style without effort.
- Autoimport
- Python formatter to automatically fix wrong import statements.
- isort
- Python formatter to order the import statements.
- Pip-tools
- Command line tool to manage the dependencies.
- Mkdocs
- To build this documentation site, with the Material theme.
- Safety
- To check the installed dependencies for known security vulnerabilities.
- Bandit
- To finds common security issues in Python code.
- Yamlfix
- YAML fixer.
Contributing¶
For guidance on setting up a development environment, and how to make a contribution to faker-optional, see Contributing to faker-optional.