Skip to main content

calamus is a library built on top of marshmallow to allow (de-)Serialization of Python classes to JSON-LD.

Project description

https://github.com/SwissDataScienceCenter/calamus/blob/master/docs/reed.png?raw=true

calamus: JSON-LD Serialization Library for Python

Documentation Status https://github.com/SwissDataScienceCenter/calamus/workflows/Test,%20Integration%20Tests%20and%20Deploy/badge.svg https://badges.gitter.im/SwissDataScienceCenter/calamus.svg

calamus is a library built on top of marshmallow to allow (de-)Serialization of Python classes to JSON-LD

Installation

calamus releases and development versions are available from PyPI. You can install it using any tool that knows how to handle PyPI packages.

With pip:

$ pip install calamus

Usage

Assuming you have a class like

class Book:
    def __init__(self, _id, name):
        self._id = _id
        self.name = name

Declare schemes

You can declare a schema for serialization like

from calamus import fields
from calamus.schema import JsonLDSchema

schema = fields.Namespace("http://schema.org/")

class BookSchema(JsonLDSchema):
    _id = fields.Id()
    name = fields.String(schema.name)

    class Meta:
        rdf_type = schema.Book
        model = Book

The fields.Namespace class represents an ontology namespace.

Make sure to set rdf_type to the RDF triple type you want get and model to the python class this schema applies to.

Serializing objects (“Dumping”)

You can now easily serialize python classes to JSON-LD

book = Book(_id="http://example.com/books/1", name="Ilias")
jsonld_dict = BookSchema().dump(book)
#{
#    "@id": "http://example.com/books/1",
#    "@type": "http://schema.org/Book",
#    "http://schema.org/name": "Ilias",
#}

jsonld_string = BookSchema().dumps(book)
#'{"@id": "http://example.com/books/1", "http://schema.org/name": "Ilias", "@type": "http://schema.org/Book"}')

Deserializing objects (“Loading”)

You can also easily deserialize JSON-LD to python objects

data = {
    "@id": "http://example.com/books/1",
    "@type": "http://schema.org/Book",
    "http://schema.org/name": "Ilias",
}
book = BookSchema().load(data)
#<Book(_id="http://example.com/books/1", name="Ilias")>

Support

You can reach us on our Gitter Channel.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

calamus-0.3.4.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

calamus-0.3.4-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file calamus-0.3.4.tar.gz.

File metadata

  • Download URL: calamus-0.3.4.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.8.10-arch1-1

File hashes

Hashes for calamus-0.3.4.tar.gz
Algorithm Hash digest
SHA256 61b69ffa3ac41445b7e9deec598dea0e4b463ef00c044f4f001c14c85f06d4b7
MD5 5939fe6d70b110bb799da570298e8db2
BLAKE2b-256 aa35aa467310a18934abceb1d6be24dc9cb3b793d5d875aa22d7067cbed64859

See more details on using hashes here.

File details

Details for the file calamus-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: calamus-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.8.10-arch1-1

File hashes

Hashes for calamus-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 46959139e4a4a7c545c1b5d6a9c0a99891f87d1ceccd6832ff61c32542458adc
MD5 282863a27fe459c405a2a4f278a7f038
BLAKE2b-256 61e0337970554a98bf67ae111d7982e78cc9081517f0aea98ee363efc93e37f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page