Skip to main content

Ordered dataclass serializer mixin for sloted dataclasses

Project description

Slot Machine

tests

Have you ever found yourself ruminating 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: !IntegerSampler 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: !IntegerSampler 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.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

slot_machine_serializers-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for slot_machine_serializers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 903d34dd89be0643c895b5a7ceeedfc341ec4c1b541d4cc00a2dc8cd40f2285f
MD5 3b96efe4f40d28cf60960580a3614684
BLAKE2b-256 cc7494bf55f742019df72573977af69997b5b5fdb66ae0f300cf1a0c1c808895

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slot_machine_serializers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97c92cb115fb052b33c5628a11323c7cbd3cd31fc2e9515ec477f1cc0a05fdb6
MD5 9c6a89f4210797c842dad37d143ff9bd
BLAKE2b-256 e7dff8d8ad036a9a03094182b3174eb854e9414cf4b44a33b326aad7625a44eb

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