跳转到主要内容

calamus 是一个基于 marshmallow 构建的库,允许对 Python 类进行 (反) 序列化为 JSON-LD。

项目描述

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

calamus:Python 的 JSON-LD 序列化库

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

calamus 是一个基于 marshmallow 构建的库,允许对 Python 类进行 (反) 序列化为 JSON-LD

安装

calamus 的发布版本和开发版本可在 PyPI 上找到。您可以使用任何知道如何处理 PyPI 软件包的工具来安装它。

使用 pip

$ pip install calamus

用法

假设您有一个类似以下类

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

声明方案

您可以声明一个用于序列化的方案,如下所示

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

fields.Namespace 类表示一个本体命名空间。

请确保将 rdf_type 设置为您想要的 RDF 三元组类型,并将 model 设置为应用此方案的目标 Python 类。

对象序列化(“导出”)

现在您可以将 Python 类轻松序列化为 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"}')

对象反序列化(“加载”)

您也可以轻松地将 JSON-LD 反序列化为 Python 对象。

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

使用 OWL 本体验证命名空间中的属性

您可以在序列化过程中使用 OWL 本体来验证 Python 类中的属性。以下示例中的本体没有将 publishedYear 定义为属性。

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

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

    class Meta:
       rdf_type = schema.Book
       model = Book

book = Book(id="http://example.com/books/2", name="Outliers", author="Malcolm Gladwell", publishedYear=2008)

data = {
    "@id": "http://example.com/books/3",
    "@type": "http://schema.org/Book",
    "http://schema.org/name" : "Harry Potter & The Prisoner of Azkaban",
    "http://schema.org/author" : "J. K. Rowling",
    "http://schema.org/publishedYear" : 1999
}

valid_invalid_dict = BookSchema().validate_properties(
    data,
    "tests/fixtures/book_ontology.owl"
)
# The ontology doesn't have a publishedYear property
# {'valid': {'http://schema.org/author', 'http://schema.org/name'}, 'invalid': {'http://schema.org/publishedYear'}}

validated_json = BookSchema().validate_properties(book, "tests/fixtures/book_ontology.owl", return_valid_data=True)
#{'@id': 'http://example.com/books/2', '@type': ['http://schema.org/Book'], 'http://schema.org/name': 'Outliers', 'http://schema.org/author': 'Malcolm Gladwell'}

您也可以在反序列化过程中使用它。

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

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

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

    class Meta:
        rdf_type = schema.Book
        model = Book

data = {
    "@id": "http://example.com/books/1",
    "@type": "http://schema.org/Book",
    "http://schema.org/name": "Harry Potter & The Chamber of Secrets",
    "http://schema.org/author": "J. K. Rowling",
    "http://schema.org/publishedYear": 1998,
}

verified_data = BookSchema().validate_properties(data, "tests/fixtures/book_ontology.owl", return_valid_data=True)

book_verified = BookSchema().load(verified_data)
#<Book(_id="http://example.com/books/1", name="Harry Potter & The Chamber of Secrets", author="J. K. Rowling")>

函数 validate_properties 有三个参数:dataontologyreturn_valid_data

data 可以是 Json-LD,是方案模型类的 Python 对象,或它们的列表。

ontology 是指向 OWL 本体位置的字符串(路径或 URI)。

return_valid_data 是一个可选参数,默认值为 False。默认行为是返回包含有效和无效属性的字典。将其设置为 True 将返回仅包含已验证属性的 JSON-LD。

注释

类也可以直接用方案信息注释,从而无需单独的方案。这可以通过将模型的元类设置为 JsonLDAnnotation 来实现。

import datetime.datetime as dt

from calamus.schema import JsonLDAnnotation
import calamus.fields as fields

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

class User(metaclass=JsonLDAnnotation):
    _id = fields.Id()
    birth_date = fields.Date(schema.birthDate, default=dt.now)
    name = fields.String(schema.name, default=lambda: "John")

    class Meta:
        rdf_type = schema.Person

user = User()

# dumping
User.schema().dump(user)
# or
user.dump()

# loading
u = User.schema().load({"_id": "http://example.com/user/1", "name": "Bill", "birth_date": "1970-01-01 00:00"})

支持

您可以通过我们的 Gitter 频道 联系我们。

项目详情


下载文件

下载适合您平台的应用程序文件。如果您不确定该选择哪个,请了解更多关于 安装包 的信息。

源代码分发

calamus-0.4.2.tar.gz (21.3 kB 查看哈希值

上传时间 源代码

构建分发

calamus-0.4.2-py3-none-any.whl (22.8 kB 查看哈希值

上传时间 Python 3

由以下支持