Skip to main content
Build Status

SpringField makes API data easy.

SpringField makes it simple to model structured data. Once the data is modeled, SpringField can parse API responses into easy to use Python objects and types. It can also generate the same structured data for making API request.

SpringField is ideal for:

  • Restful JSON API data structures

  • Parsing CSV data structures from csv.DictReader

  • Turning anything Python can parse into a dict or list into a structured object

There is also a helper library for using SpringField with Mongo: springfield- mongo

Quickstart

To define an springfield.Entity, subclass springfield.Entity. Define your attributes by specifying fields. This library provides the follow self-describing fields to start with:

  • IntField

  • FloatField

  • BooleanField

  • StringField

  • BytesField

  • DateTimeField

  • EmailField

  • UrlField

  • EntityField

  • CollectionField

A quick example:

#!/usr/bin/env python
from springfield import Entity, fields
from springfield.timeutil import utcnow


class Bookmark(Entity):
    uri = fields.UrlField(doc='The bookmark uri.')
    verified = fields.BooleanField(doc='Whether or not this bookmark URI has been verified to exist.')
    added = fields.DateTimeField()


class User(Entity):
    id = fields.IntField(doc='Auto-incremented database id.')
    email = fields.EmailField(doc='The user\'s email address.')
    bookmarks = fields.CollectionField(fields.EntityField(Bookmark))
    created = fields.DateTimeField()


if __name__ == '__main__':
    user = User()
    user.id = 5
    user.email = 'foobar@example.com'
    user.bookmarks = [
        {'uri': 'https://github.com'},
        {'uri': 'ftp://google.com', 'verified': True}
    ]
    user.created = utcnow()
    data = user.to_json()
    # `data` is suitable to return in something like a JSON API.
    print data

    # Similarly, `data` can be adapted from a JSON API request body.
    user = User.from_json(data)
    print user.email
    print user.created
    print user.bookmarks

Will print (the json was prettified to protect the innocent):

{
    "bookmarks":[
        {
            "uri":"https://github.com"
        },
        {
            "uri":"ftp://google.com",
            "verified":true
        }
    ],
    "created":"2017-01-25T20:25:54Z",
    "email":"foobar@example.com",
    "id":5
}
foobar@example.com
2017-01-25 20:47:37+00:00
[<Bookmark {uri: https://github.com}>, <Bookmark {verified: True, uri: ftp://google.com}>]

Notice a few things:

  • Not every field is required for an entity. This is useful for doing sparse updates on an API.

  • SpringField will adapt types in a non-destructive way.

  • You can also create entities by adapting JSON, which is really handy at API boundaries.

Field Validation

SpringField does field validation when constructing entities, according to the types defined by the fields on that entity. For example:

You can define more complex field adaptation behavior by subclassing springfield.fields.Field and implementing your own fields. See the documentation on springfield.fields.Field for more information.

Similar Projects

Changelog

0.9.1

  • Fixed Entity.get() for Python 3

0.9.0

  • Switched from future to six Python 2/3 compatibility libraries because future’s modified str does not play well with adapters.

0.8.0

  • Added support for Python 3.6+

  • Dropped support for Python <2.7

0.7.17

  • Fix packages for pytest plugin

0.7.16

  • Allow EntityFields to use dotted-name class strings. This was done to allow circular references in entities that may refer to one another.

  • Added BytesField

0.7.15

  • Allow empty values for URL

Metadata

Release files for springfield 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for springfield 0.9.1
File Size Uploaded Provenance
springfield-0.9.1.tar.gz 26.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for springfield 0.9.1
File Interpreter ABI Platform
springfield-0.9.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 42.5 kB

Release files / springfield-0.9.1.tar.gz

Download URL springfield-0.9.1.tar.gz
Size 26.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0537eee97ce9575d50bea213a58078f03ae349b9d048b5376626f2e062c33cde
BLAKE2b-256 checksum
How to use checksums
77c002788343ef9a1d7fbb49b92988273f58f992bda12469dafd937e98279ffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release files / springfield-0.9.1-py2.py3-none-any.whl

Download URL springfield-0.9.1-py2.py3-none-any.whl
Size 16.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
055a326300094f52e7f002c8e8a3c7202fd37dbb19e18768e6753a97332c788c
BLAKE2b-256 checksum
How to use checksums
4571738c41ed7fe01291868725cbec225c36d86349f5d62445600d0ba3562c10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Security for springfield 0.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page