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

..
Copyright 2017-2020 - Swiss Data Science Center (SDSC)
A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
Eidgenössische Technische Hochschule Zürich (ETHZ).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

.. image:: https://github.com/SwissDataScienceCenter/calamus/blob/master/docs/reed.png?raw=true
:scale: 50
:align: center

==================================================
calamus: Json-LD Serialization Libary for Python
==================================================

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
<https://pypi-hypernode.com/project/calamus/>`_. 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 schemas
---------------
You can declare a schema for serialization like
::

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")>

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.1.2.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

calamus-0.1.2-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: calamus-0.1.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.6.10-arch1-1

File hashes

Hashes for calamus-0.1.2.tar.gz
Algorithm Hash digest
SHA256 00ff6c7c08fa5a964dccb398b027980ee76191ff467f01983cba4ad5f0187fd5
MD5 57b4292f8a9be90bc0825b3ba43aec98
BLAKE2b-256 d77370757999a829504d48fb244e685c7daca29027ba2981f709b945aa637378

See more details on using hashes here.

File details

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

File metadata

  • Download URL: calamus-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.6.10-arch1-1

File hashes

Hashes for calamus-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2b8c2a46276ef10ca99a8aceb2b1a0c96868163a738c0069b9b8ddecbb343d7
MD5 a5fc115a0fc666d0a242e0c6ef165c85
BLAKE2b-256 9600aed16549dec095a6cc2a73f31210dfc1abbd714e1ae95f8b3e1f43e8b326

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