• This repository has been archived on 20/Oct/2022
  • Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Enum handling for Marshmallow

marshmallow-enum

Enum field for use with Marshmallow.

Installation

pip install --user marshmallow_enum

If you're on a version before 3.4, you'll also need to install enum34.

Using The Field

To make use of the field, you must have an existing Enum:

from enum import Enum


class StopLight(Enum):
    green = 1
    yellow = 2
    red = 3

Then, declare it as a field in a schema:

from marshmallow import Schema
from marshmallow_enum import EnumField


class TrafficStop(Schema):
    light_color = EnumField(StopLight)

By default, the field will load and dump based on the name given to an enum value.

schema = TrafficStop()
schema.dump({'light_color': EnumField.red}).data
# {'light_color': 'red'}

schema.load({'light_color': 'red'}).data
# {'light_color': StopLight.red}

Customizing loading and dumping behavior

To customize how an enum is serialized or deserialized, there are three options:

  • Setting by_value=True. This will cause both dumping and loading to use the value of the enum.
  • Setting load_by=EnumField.VALUE. This will cause loading to use the value of the enum.
  • Setting dump_by=EnumField.VALUE. This will cause dumping to use the value of the enum.

If either load_by or dump_by are unset, they will follow from by_value.

Additionally, there is EnumField.NAME to be explicit about the load and dump behavior, this is the same as leaving both by_value and either load_by and/or dump_by unset.

Custom Error Message

A custom error message can be provided via the error keyword argument. It can accept three format values:

  • {input}: The value provided to the schema field
  • {names}: The names of the individual enum members
  • {values}: The values of the individual enum members

Previously, the following inputs were also available but are deprecated and will be removed in 1.6:

  • {name}
  • {value}
  • {choices}

More Repositories

1

flask-allows

Authorization tools for Flask
Python
107
star
2

marshmallow-annotations

Allows declaring marshmallow schema through type annotations
Python
48
star
3

Flask-Transfer

Tired of validating and manipulating uploaded files manually? Try this instead.
Python
39
star
4

OpenWebAmp

It really whips the Python's Ass.
Python
15
star
5

pynads

Dumb implementation of monads in Python.
Python
15
star
6

datestuff

Things and stuff for times, dates and datetimes. Maybe they're useful
Python
14
star
7

flaskbb-swarm

Set up FlaskBB in a Docker Swarm
Shell
13
star
8

ipynb_reader

An IPython Notebook reader for Pelican
Python
10
star
9

aiohttp_docker_webapp

Example of a dockerized aiohttp/gunicorn/nginx application featuring uvloop and supervisord
Shell
7
star
10

IsThatWho

Cast and Crew intersector
Python
6
star
11

rustfuck

Implementing Brainfuck in Rust to learn Rust
Brainfuck
4
star
12

gamenight

organize a gamenight
Python
4
star
13

pykovy

Building order-n markov chains.
Python
4
star
14

Py2ChainMap

Backporting ChainMap from Python 3 to Python 2
Python
3
star
15

owa

Complete rework of OWA in a separate repository.
Python
2
star
16

dotfiles

My dotfiles
Shell
1
star
17

flask-pluggy-poc

playing with pluggy
Python
1
star
18

my-talks

talks I've given places
1
star
19

justanr.github.io

Github suggested I named this ballin-octo-bear. Maybe I will.
HTML
1
star
20

objtoolz

Metaclass, descriptors, etc that I find myself using and wanting. Partially based on toolz
Python
1
star
21

async-breaker

Experimental circuit breaker implementation for Python's asyncio
Python
1
star
22

notebooks

A collection of my IPython notebooks.
Jupyter Notebook
1
star