Skip to main content

Ordered dataclass serializer mixin for sloted dataclasses

Project description

Slot Machine

tests

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: !RangeSampler 5..20
price: !UniformSampler 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: !RangeSampler 5..10
  price: !UniformSampler 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 SplitsSampler(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: !SplitsSampler
    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

slot_machine_serializers-0.2.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

slot_machine_serializers-0.2.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file slot_machine_serializers-0.2.2.tar.gz.

File metadata

File hashes

Hashes for slot_machine_serializers-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1375517c1d8de25d6a2f5b64d64454def21369bcd84a7d5bd8dfb9b312e013b5
MD5 9ce88991010a5b91f83d9ce5765b1642
BLAKE2b-256 72e78d25d5704f9123b8b8492ecf4f7a1bdd41bfa2a4b4c71a4417d7733c7373

See more details on using hashes here.

File details

Details for the file slot_machine_serializers-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for slot_machine_serializers-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c622225d1abebebd238ea449bc14862822a421d25ca762c5ef58d01d590f6d1b
MD5 60fb6719135e40295294258003c1a4ac
BLAKE2b-256 3e50e0d2820835281574d6a737fba25c82ba76af5afdf008becb2a81b911e829

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page