Skip to content

Instantly share code, notes, and snippets.

@inesp
Last active October 20, 2019 08:12
Show Gist options
  • Save inesp/6efa458b76c13e96dbd009d02aa560f8 to your computer and use it in GitHub Desktop.
Save inesp/6efa458b76c13e96dbd009d02aa560f8 to your computer and use it in GitHub Desktop.
# Here we define all possible parameter combinations.
# We also add a little bit of validation (which fields
# are required, what are the default values).
from marshmallow import Schema, fields
class PMargharitaSchema(Schema):
with_bazil = fields.Boolean(missing=True)
class PPestoSchema(Schema):
cheeze_type = fields.Str(
required=True,
validate=validate.OneOf(("mozzarella", "mixed"))
)
with_arugula = fields.Boolean(missing=True)
...
class PizzaMaker:
# we can promote "ptype" to outsize of "pizza_properties"
def create_pizza(self, ptype, **pizza_properties):
# we have to get the correct shema class
props_schema = resolve_schema_from(ptype)
if props_schema is None:
raise TypError(....)
# validate parameters: this will raise
# marshmallow.ValidationError if something is invalid
valid_props = props_schema.load(pizza_properties)
pizza_class = get_pizza_class(ptype)
if pizza_class is None:
raise UnknownPizaType(ptype)
pizza = pizza_class(**valid_props)
return pizza
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment