=====================
Marshmallow-Polyfield
=====================
.. image:: https://travis-ci.com/Bachmann1234/marshmallow-polyfield.svg?branch=master
:target: https://travis-ci.com/Bachmann1234/marshmallow-polyfield
:alt: Build Status
.. image:: https://coveralls.io/repos/Bachmann1234/marshmallow-polyfield/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/Bachmann1234/marshmallow-polyfield?branch=master
:alt: Coverage Status
This branch supports Marshmallow 3.0 and above. For 2.0 support see `The 2.0 branch <https://github.com/Bachmann1234/marshmallow-polyfield/tree/polyfield-2support>`_
An unofficial extension to Marshmallow to allow for polymorphic fields.
Marshmallow is a fantastic library for serialization and deserialization of data.
For more on that project see its `GitHub <https://github.com/marshmallow-code/marshmallow>`_ page or its `Documentation <http://marshmallow.readthedocs.org/en/latest/>`_.
This project adds a custom field designed for polymorphic types. This allows you to define a schema that says "This field accepts anything of type X"
The secret to this field is that you need to define two functions. One to be used when serializing, and another for deserializing. These functions
take in the raw value and return the schema to use.
This field should support the same properties as other Marshmallow fields. I have worked with *required* *allow_none* and *many*.
Last version support v2 is tagged FINAL_V2_VERSION
Installing
----------
::
$ pip install marshmallow-polyfield
Importing
---------
Here is how to import the necessary field class
::
from marshmallow_polyfield import PolyField
Example
-------
The code below demonstrates how to setup a schema with a PolyField. For the full context check out the tests.
Once setup the schema should act like any other schema. If it does not then please file an Issue.
.. code:: python
def shape_schema_serialization_disambiguation(base_object, parent_obj):
class_to_schema = {
Rectangle.__name__: RectangleSchema,
Triangle.__name__: TriangleSchema
}
try:
return class_to_schema[base_object.__class__.__name__]()
except KeyError:
pass
raise TypeError("Could not detect type. "
"Did not have a base or a length. "
"Are you sure this is a shape?")
def shape_schema_deserialization_disambiguation(object_dict, parent_object_dict):
if object_dict.get("base"):
return TriangleSchema()
elif object_dict.get("length"):
return RectangleSchema()
raise TypeError("Could not detect type. "
"Did not have a base or a length. "
"Are you sure this is a shape?")
class ContrivedShapeClass(object):
def __init__(self, main, others):
self.main = main
self.others = others
def __eq__(self, other):
return self.__dict__ == other.__dict__
class ContrivedShapeClassSchema(Schema):
main = PolyField(
serialization_schema_selector=shape_schema_serialization_disambiguation,
deserialization_schema_selector=shape_schema_deserialization_disambiguation,
required=True
)
others = PolyField(
serialization_schema_selector=shape_schema_serialization_disambiguation,
deserialization_schema_selector=shape_schema_deserialization_disambiguation,
allow_none=True,
many=True
)
@post_load
def make_object(self, data):
return TestPolyField.ContrivedShapeClass(
data.get('main'),
data.get('others')
)
Raw data
{
"_id": null,
"home_page": "https://github.com/Bachmann1234/marshmallow-polyfield",
"name": "marshmallow-polyfield",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": "",
"keywords": "serialization,rest,json,api,marshal,marshalling,deserialization,validation,schema",
"author": "Matt Bachmann",
"author_email": "bachmann.matt@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0e/55/b752d034a09afe34d680c65cf8634c0c55f9b57e7a673774bc13ca44b53e/marshmallow-polyfield-5.11.tar.gz",
"platform": null,
"description": "=====================\nMarshmallow-Polyfield\n=====================\n\n.. image:: https://travis-ci.com/Bachmann1234/marshmallow-polyfield.svg?branch=master\n :target: https://travis-ci.com/Bachmann1234/marshmallow-polyfield\n :alt: Build Status\n.. image:: https://coveralls.io/repos/Bachmann1234/marshmallow-polyfield/badge.svg?branch=master&service=github\n :target: https://coveralls.io/github/Bachmann1234/marshmallow-polyfield?branch=master\n :alt: Coverage Status\n\nThis branch supports Marshmallow 3.0 and above. For 2.0 support see `The 2.0 branch <https://github.com/Bachmann1234/marshmallow-polyfield/tree/polyfield-2support>`_ \n\nAn unofficial extension to Marshmallow to allow for polymorphic fields.\n\nMarshmallow is a fantastic library for serialization and deserialization of data.\nFor more on that project see its `GitHub <https://github.com/marshmallow-code/marshmallow>`_ page or its `Documentation <http://marshmallow.readthedocs.org/en/latest/>`_.\n\nThis project adds a custom field designed for polymorphic types. This allows you to define a schema that says \"This field accepts anything of type X\"\n\nThe secret to this field is that you need to define two functions. One to be used when serializing, and another for deserializing. These functions\ntake in the raw value and return the schema to use.\n\nThis field should support the same properties as other Marshmallow fields. I have worked with *required* *allow_none* and *many*.\n\nLast version support v2 is tagged FINAL_V2_VERSION\n\nInstalling\n----------\n::\n\n $ pip install marshmallow-polyfield\n\nImporting\n---------\nHere is how to import the necessary field class\n::\n\n from marshmallow_polyfield import PolyField\n\nExample\n-------\n\nThe code below demonstrates how to setup a schema with a PolyField. For the full context check out the tests.\nOnce setup the schema should act like any other schema. If it does not then please file an Issue.\n\n.. code:: python\n\n def shape_schema_serialization_disambiguation(base_object, parent_obj):\n class_to_schema = {\n Rectangle.__name__: RectangleSchema,\n Triangle.__name__: TriangleSchema\n }\n try:\n return class_to_schema[base_object.__class__.__name__]()\n except KeyError:\n pass\n\n raise TypeError(\"Could not detect type. \"\n \"Did not have a base or a length. \"\n \"Are you sure this is a shape?\")\n\n\n def shape_schema_deserialization_disambiguation(object_dict, parent_object_dict):\n if object_dict.get(\"base\"):\n return TriangleSchema()\n elif object_dict.get(\"length\"):\n return RectangleSchema()\n\n raise TypeError(\"Could not detect type. \"\n \"Did not have a base or a length. \"\n \"Are you sure this is a shape?\")\n\n\n class ContrivedShapeClass(object):\n def __init__(self, main, others):\n self.main = main\n self.others = others\n\n def __eq__(self, other):\n return self.__dict__ == other.__dict__\n\n\n class ContrivedShapeClassSchema(Schema):\n main = PolyField(\n serialization_schema_selector=shape_schema_serialization_disambiguation,\n deserialization_schema_selector=shape_schema_deserialization_disambiguation,\n required=True\n )\n others = PolyField(\n serialization_schema_selector=shape_schema_serialization_disambiguation,\n deserialization_schema_selector=shape_schema_deserialization_disambiguation,\n allow_none=True,\n many=True\n )\n\n @post_load\n def make_object(self, data):\n return TestPolyField.ContrivedShapeClass(\n data.get('main'),\n data.get('others')\n )",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "An unofficial extension to Marshmallow to allow for polymorphic fields",
"version": "5.11",
"split_keywords": [
"serialization",
"rest",
"json",
"api",
"marshal",
"marshalling",
"deserialization",
"validation",
"schema"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0e55b752d034a09afe34d680c65cf8634c0c55f9b57e7a673774bc13ca44b53e",
"md5": "3a86a529ce3f8f1cc96bb52527030e9c",
"sha256": "8075a9cc490da4af58b902b4a40a99882dd031adb7aaa96abd147a4fcd53415f"
},
"downloads": -1,
"filename": "marshmallow-polyfield-5.11.tar.gz",
"has_sig": false,
"md5_digest": "3a86a529ce3f8f1cc96bb52527030e9c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 8596,
"upload_time": "2022-10-26T03:10:40",
"upload_time_iso_8601": "2022-10-26T03:10:40.661368Z",
"url": "https://files.pythonhosted.org/packages/0e/55/b752d034a09afe34d680c65cf8634c0c55f9b57e7a673774bc13ca44b53e/marshmallow-polyfield-5.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-10-26 03:10:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Bachmann1234",
"github_project": "marshmallow-polyfield",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "coverage",
"specs": [
[
">=",
"3.7.1"
]
]
},
{
"name": "coveralls",
"specs": [
[
">=",
"0.5"
]
]
},
{
"name": "flake8",
"specs": [
[
">=",
"2.4.1"
]
]
},
{
"name": "marshmallow",
"specs": [
[
">=",
"3.0.0b10"
]
]
},
{
"name": "pytest",
"specs": [
[
">=",
"2.7.2"
]
]
},
{
"name": "pytest-cov",
"specs": [
[
">=",
"2.1.0"
]
]
},
{
"name": "tox",
"specs": [
[
">=",
"2.1.1"
]
]
}
],
"tox": true,
"lcname": "marshmallow-polyfield"
}