Reprint: http://www.thinksaas.cn/topics/0/594/594368.html
Marshmallow-a lightweight library for turning complex objects into simple Python data types. Or from a simple Python data type into a complex object.
From datetime import datefrom Marshmallow Import schema, fields, Pprintclass Artistschema (Schema): name = fields. STR () class Albumschema (Schema): title = fields. STR () release_date = fields. Date () artist = fields. Nested (artistschema) Bowie = dict (name= ' David Bowie ') album = Dict (Artist=bowie, title= ' hunky Dory ', release_date=date ( 1971, () schema = Albumschema () result = Schema.dump (album) Pprint (Result.data, indent=2) # {' Artist ': {' name ': ' David Bowie '},# ' release_date ': ' 1971-12-17 ', # ' title ': ' Hunky Dory '}
In short, marshmallow schemas can is used to:
- Validate input data.
- Deserialize input data to app-level objects.
- Serialize App-level objects to primitive Python types. The serialized objects can then is rendered to standard formats such as JSON for use in an HTTP API.
Get It Now
$ pip install-u Marshmallow
Documentation
Full documentation are available at http://marshmallow.readthedocs.org/.
Requirements
Marshmallow have no external dependencies outside of the Python standard library, although Python-dateutil is recommended F or robust datetime deserialization.
Ecosystem
A List of marshmallow-related libraries can be found on the GitHub wiki here:
Https://github.com/marshmallow-code/marshmallow/wiki/Ecosystem
Project Links
- docs:http://marshmallow.readthedocs.org/
- Changelog:http://marshmallow.readthedocs.org/en/latest/changelog.html
- Pypi:https://pypi.python.org/pypi/marshmallow
- Issues:https://github.com/marshmallow-code/marshmallow/issues
License
MIT licensed. See the bundled LICENSE file for more details.
Project Address: Https://github.com/marshmallow-code/marshmallow
Marshmallow: Simplifying Python object serialization