marshmallow-enum


Namemarshmallow-enum JSON
Version 1.5.1 PyPI version JSON
download
home_page
SummaryEnum field for Marshmallow
upload_time2019-08-21 01:07:46
maintainer
docs_urlNone
authorAlec Nikolas Reiter
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # marshmallow-enum

Enum field for use with Marshmallow.

## Installation

```bash
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:

```python
from enum import Enum


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

Then, declare it as a field in a schema:

```python
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.

```python
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}`


Version 1.5
===========

Allow unicode enum values (sergeyzsg)
Add support for Marshmallow 3 (svidela)

Version 1.4
===========

* Deprecate the `{choices}`, `{name}` and `{value}` format keys for custom error messages
* Allow configure loading and dumping independently



            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "marshmallow-enum",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Alec Nikolas Reiter",
    "author_email": "alecreiter@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8e/8c/ceecdce57dfd37913143087fffd15f38562a94f0d22823e3c66eac0dca31/marshmallow-enum-1.5.1.tar.gz",
    "platform": "",
    "description": "# marshmallow-enum\n\nEnum field for use with Marshmallow.\n\n## Installation\n\n```bash\npip install --user marshmallow_enum\n```\n\nIf you're on a version before 3.4, you'll also need to install `enum34`.\n\n## Using The Field\n\nTo make use of the field, you must have an existing Enum:\n\n```python\nfrom enum import Enum\n\n\nclass StopLight(Enum):\n    green = 1\n    yellow = 2\n    red = 3\n```\n\nThen, declare it as a field in a schema:\n\n```python\nfrom marshmallow import Schema\nfrom marshmallow_enum import EnumField\n\n\nclass TrafficStop(Schema):\n    light_color = EnumField(StopLight)\n```\n\nBy default, the field will load and dump based on the _name_ given to an enum value.\n\n```python\nschema = TrafficStop()\nschema.dump({'light_color': EnumField.red}).data\n# {'light_color': 'red'}\n\nschema.load({'light_color': 'red'}).data\n# {'light_color': StopLight.red}\n```\n\n### Customizing loading and dumping behavior\n\nTo customize how an enum is serialized or deserialized, there are three options:\n\n-   Setting `by_value=True`. This will cause both dumping and loading to use the value of the enum.\n-   Setting `load_by=EnumField.VALUE`. This will cause loading to use the value of the enum.\n-   Setting `dump_by=EnumField.VALUE`. This will cause dumping to use the value of the enum.\n\nIf either `load_by` or `dump_by` are unset, they will follow from `by_value`.\n\nAdditionally, there is `EnumField.NAME` to be explicit about the load and dump behavior, this\nis the same as leaving both `by_value` and either `load_by` and/or `dump_by` unset.\n\n### Custom Error Message\n\nA custom error message can be provided via the `error` keyword argument. It can accept three\nformat values:\n\n-   `{input}`: The value provided to the schema field\n-   `{names}`: The names of the individual enum members\n-   `{values}`: The values of the individual enum members\n\nPreviously, the following inputs were also available but are deprecated and will be removed in 1.6:\n\n-   `{name}`\n-   `{value}`\n-   `{choices}`\n\n\nVersion 1.5\n===========\n\nAllow unicode enum values (sergeyzsg)\nAdd support for Marshmallow 3 (svidela)\n\nVersion 1.4\n===========\n\n* Deprecate the `{choices}`, `{name}` and `{value}` format keys for custom error messages\n* Allow configure loading and dumping independently\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Enum field for Marshmallow",
    "version": "1.5.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "8d7f93cbd674015b878c199b82b96585",
                "sha256": "57161ab3dbfde4f57adeb12090f39592e992b9c86d206d02f6bd03ebec60f072"
            },
            "downloads": -1,
            "filename": "marshmallow_enum-1.5.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8d7f93cbd674015b878c199b82b96585",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 4186,
            "upload_time": "2019-08-21T01:07:44",
            "upload_time_iso_8601": "2019-08-21T01:07:44.814466Z",
            "url": "https://files.pythonhosted.org/packages/c6/59/ef3a3dc499be447098d4a89399beb869f813fee1b5a57d5d79dee2c1bf51/marshmallow_enum-1.5.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "7f4624f5b553c50895c33b5195108c10",
                "sha256": "38e697e11f45a8e64b4a1e664000897c659b60aa57bfa18d44e226a9920b6e58"
            },
            "downloads": -1,
            "filename": "marshmallow-enum-1.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7f4624f5b553c50895c33b5195108c10",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4013,
            "upload_time": "2019-08-21T01:07:46",
            "upload_time_iso_8601": "2019-08-21T01:07:46.254956Z",
            "url": "https://files.pythonhosted.org/packages/8e/8c/ceecdce57dfd37913143087fffd15f38562a94f0d22823e3c66eac0dca31/marshmallow-enum-1.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-08-21 01:07:46",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "marshmallow-enum"
}
        
Elapsed time: 0.01556s