Skip to main content

Lion

Lion is a very flexible yet fast library for mapping objects to dictionaries. It uses a declarative API and supports a query language similar to GraphQL. Unlike other serialization libraries it also allows to skip entire fields instead of having a null value in the dictionary.

It is inspired by libraries like serpy, marshmallow, Django REST Framework and Kim.

Example

import lion

class GroupMapper(lion.Mapper):
    id = lion.UUIDField()
    name = lion.StrField()

class UserMapper(lion.Mapper):
    id = lion.UUIDField()
    email = lion.StrField(condition=lion.skip_empty)
    first_name = lion.StrField()
    last_name = lion.StrField()
    groups = lion.ListField(GroupMapper)

user = User(
    id=UUID('ad94d0e8-2526-4d9b-ad76-0fbffcf41033'),
    email='john.doe@example.com',
    first_name='John',
    last_name='Doe',
    groups=[
        Group(
            id=UUID('95a326fc-32e5-4d9b-a385-1ea1257d98da'),
            name='Awesome people'
        )
    ]
)

# Dump all fields to a dictionary
assert UserMapper().dump(user) == {
    'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
    'email': 'john.doe@example.com',
    'first_name': 'John',
    'last_name': 'Doe',
    'groups': [
        {
            'id': '95a326fc-32e5-4d9b-a385-1ea1257d98da',
            'name': 'Awesome people'
        }
    ]
}

# Load user object from a dictionary
assert user == UserMapper().load({
    'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
    'email': 'john.doe@example.com',
    'first_name': 'John',
    'last_name': 'Doe',
    'groups': [
        {
            'id': '95a326fc-32e5-4d9b-a385-1ea1257d98da',
            'name': 'Awesome people'
        }
    ]
})

Query language

By using the GraphQL-like query language it is possible to dump and load only parts of a given structure:

# Dump a subset of fields
assert UserMapper('{id,email}').dump(user) == {
    'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
    'email': 'john.doe@example.com'
}

# Dump subset of a nested mapper
assert UserMapper('{id,email,groups{id}}').dump(user) == {
    'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
    'email': 'john.doe@example.com',
    'groups': [
        'id': '95a326fc-32e5-4d9b-a385-1ea1257d98da'
    ]
}

Performance

The performance is somewhat slower than serpy but still far ahead of marshmallow and Django REST Framework.

Simple Benchmark

Complex Benchmark

Caveats

Lion also supports loading (serialization/marshalling) of data but currently does not perform any kind of validation. This is not a big deal if using Lion as part of a project which uses something like connexion which already performs validation using the provided OpenAPI specification file. Just be warned that loading an unvalidated data structure using Lion might result in somewhat weird looking data.

Metadata

Release files for lion 0.5.0

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

Source distribution (sdist)

Source distribution for lion 0.5.0
File Size Uploaded Provenance Actions
lion-0.5.0.tar.gz 7.3 kB Without provenance Details for lion-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lion 0.5.0
File Interpreter ABI Platform Actions
lion-0.5.0-py3-none-any.whl Python 3 none any Details for lion-0.5.0-py3-none-any.whl

Total release size: 15.3 kB

Release files / lion-0.5.0.tar.gz

Download URL lion-0.5.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f5e8fc19653ce702702ce8ba03723abb030505cf2949935532003b12fd1f87ff
BLAKE2b-256 checksum
How to use checksums
399913d5473c5b586f6f3b4d92d69f6ee8d2dac9b4e426d1c9a38bfc7f45ee8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-92-generic

Release files / lion-0.5.0-py3-none-any.whl

Download URL lion-0.5.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce567e6904e49bdde44ac75a892a289d9d655f26fb414be35b50344f0f69c476
BLAKE2b-256 checksum
How to use checksums
165f02a5031ab2e375749eb916724d1da3eda51ce944c3607b00b2cdf6800ea1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-92-generic

Security for lion 0.5.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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