Ordered dataclass serializer mixin for sloted dataclasses
Project description
Slot Machine
Have you ever found yourself unclear over which configuration to choose?
Don't choose.
Randomize.
With slot_machine
you can now sample any configuration.
Can't settle on a background color? Randomize...
Looking for a reasonable model parameter? Randomize...
Don't know what to eat on Sunday? Randomize...
With slot_machine
your next choice will be one you look forward to.
Examples
Simply add your favorite samplers to the configuration:
chicken_nuggets: !SampleRange 5..20
price: !SampleUniform 9.99..20
Specify your dataclass and get rolling:
from dataclasses import dataclass
from slot_machine import SlotsSerializer
@dataclass(slots=True)
class Basket(SlotsSerializer):
chicken_nuggets: int
price: float
yaml_file = """
basket:
chicken_nuggets: !SampleRange 5..10
price: !SampleUniform 9.99..20
"""
surprise_basket = Basket.from_yaml(yaml_file)
Build your own
from dataclasses import dataclass
from slot_machine import SlotsSerializer, MappingSampler
@dataclass(slots=True)
class Basket(SlotsSerializer):
chicken_nuggets: int
splits: list[int]
class SampleSplits(MappingSampler):
@classmethod
def get_sample(cls, n_splits: str, values: str):
n_splits = int(n_splits)
values = list(map(int, values.split("..")))
return sorted([random.randint(*values) for _ in range(n_splits)])
yaml_file = """
basket:
chicken_nuggets: 100
splits: !SampleSplits
n_splits: 3
values: 0..100
"""
surprise_basket = Basket.from_yaml(yaml_file)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file slot_machine_serializers-0.4.0.tar.gz
.
File metadata
- Download URL: slot_machine_serializers-0.4.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b54312f5d61086769975956e56bbb468945a7475c5d736573a76a8ddaa9560c0 |
|
MD5 | 780373515589ffcd6dc3c15b279e9384 |
|
BLAKE2b-256 | ddda632583e04996eebf1a10ce925acfc777cbf53079668f63f44acaf161b878 |
File details
Details for the file slot_machine_serializers-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: slot_machine_serializers-0.4.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b81fcef766dedaef7ed5883267043f1b7a8f11343ef191614f8b10f32e0cade0 |
|
MD5 | 2213c393cf3f08600d41da5d00b5ad16 |
|
BLAKE2b-256 | 184df59d7fd206c20aa08553c8d24c6631078bef0e592ee7c9e05c852e6c8ab0 |