plato.providers package¶
Contains providers for test data generation.
plato.providers.base module¶
Provider interface and base implemenations of functionality.
The members of this module are of interest if you are implementing your own providers. Otherwise, you probably will not need them.
-
class
plato.providers.base.
AttributeProvider
(parent, attr_name)[source]¶ Bases:
plato.providers.base.Provider
[plato.providers.base.T
],plato.providers.base.WithAttributeAccess
Provider of an attribute of the samples of another provider.
- Parameters
parent – Parent provider to sample.
attr_name – Name of the attribute to provide from the sampled object.
-
sample
(context)[source]¶ Return a single value (sample) for the provider.
- Parameters
context (plato.context.Context) – The sampling context. Use the random number generator from the context or initialize your random number generator from the seed provided in the context to ensure reproducibality.
- Returns
The sampled value.
- Return type
T
-
class
plato.providers.base.
Provider
(*args, **kwargs)[source]¶ Bases:
abc.ABC
,Generic
[plato.providers.base.T
],plato.providers.base.ProviderProtocol
[plato.providers.base.T
]Provider interface.
-
abstract
sample
(context)[source]¶ Return a single value (sample) for the provider.
- Parameters
context (plato.context.Context) – The sampling context. Use the random number generator from the context or initialize your random number generator from the seed provided in the context to ensure reproducibality.
- Returns
The sampled value.
- Return type
T
-
abstract
-
class
plato.providers.base.
ProviderProtocol
(*args, **kwargs)[source]¶ Bases:
Protocol
[plato.providers.base.T
]Defines the Provider protocol.
This should only be used for typing. When implementing a provider use the
Provider
abstract base class.-
sample
(context)[source]¶ Return a single value (sample) for the provider.
- Parameters
context (plato.context.Context) – The sampling context. Use the random number generator from the context or initialize your random number generator from the seed provided in the context to ensure reproducibality.
- Returns
The sampled value.
- Return type
T
-
-
class
plato.providers.base.
WithAttributeAccess
[source]¶ Bases:
Generic
[plato.providers.base.T
]Provider mixin to provide transparent access to attributes.
Attributes existing on the implementing class and special members starting and ending with a double-underscore (
__
) are excluded.Example
from dataclasses import dataclass @dataclass class Dto: foo: str = "foo" bar: str = "bar" class DtoProvider(Provider, WithAttributeAccess): def sample(self, context: Context) -> Dto: return Dto() print(sample(DtoProvider().foo))
foo
plato.providers.common module¶
Commonly used providers.
Bases:
plato.providers.base.Provider
[plato.providers.common.T
],plato.providers.base.WithAttributeAccess
[plato.providers.common.T
]Share the sampled value of a Provider across multiple fields.
Try to avoid sharing values across more than a single
formclass
because the semantics of Shared are a bit unintuitive in other cases. But for completeness sake, this is how it works: the first sampled value is reused within the sameformclass
instance and old child instances. However, if the value is first sampled in a child instance, it will not be reused in the parent instance.- Parameters
provider – Provider to share sampled values of.
Examples
Normal usage:
from dataclasses import asdict from pprint import pprint class CountingProvider(Provider): def __init__(self): self.n = 0 def sample(self, context: Context) -> int: self.n += 1 return self.n counting_provider = CountingProvider() @formclass class MyFormclass: shared: int = Shared(counting_provider) same: int = shared different: int = counting_provider pprint(asdict(sample(MyFormclass())))
{'different': 2, 'same': 1, 'shared': 1}
Discouraged use across multiple
formclass
instances:counting_provider = CountingProvider() @formclass class Child: value: int = Shared(counting_provider) @formclass class Parent: samples_child_first: Child = Child() samples_new_value_in_parent: int = samples_child_first.value reuses_value_sampled_in_parent: Child = Child() pprint(asdict(sample(Parent())))
{'reuses_value_sampled_in_parent': {'value': 2}, 'samples_child_first': {'value': 1}, 'samples_new_value_in_parent': 2}
Return a single value (sample) for the provider.
- Parameters
context (plato.context.Context) – The sampling context. Use the random number generator from the context or initialize your random number generator from the seed provided in the context to ensure reproducibality.
- Returns
The sampled value.
- Return type
T
plato.providers.faker module¶
Support for using the Faker library with Plato.
-
class
plato.providers.faker.
FromFaker
(faker=None)[source]¶ Bases:
object
Create a
Provider
from a Faker instance.All indexing operations and attribute access will be delegated to to the Faker instance, but return
Provider
instances usable in aformclass
.- Parameters
faker – Faker instance used to generate values. If not given a new instance using the default will be created.
Example
fake = FromFaker(Faker(["en-US", "de-DE"])) print(sample(fake["en-US"].street_address())) print(sample(fake["de-DE"].street_address()))
413 Perez Cape Apt. 615 Schaafallee 41