跳转到主要内容

JSON-LD API的Python实现

项目描述

Build Status

简介

此库是JSON-LD规范在Python中的实现。

JSON,如RFC7159中所述,是一种简单的语言,用于在Web上表示对象。链接数据是一种在不同的文档或网站之间描述内容的方法。Web资源使用IRI进行描述,通常是可引用的实体,可用于查找更多信息,创建“知识网络”。JSON-LD旨在成为在JSON中表达链接数据的一种简单发布方法,同时为现有的JSON添加语义。

JSON-LD设计为一种轻量级语法,可用于表达链接数据。它主要旨在作为一种在JavaScript和其他基于Web的编程环境中表达链接数据的方法。当构建互操作Web服务以及将链接数据存储在基于JSON的文档存储引擎中时,它也非常有用。它是实用的,旨在尽可能简单,利用今天使用的大量JSON解析器和现有代码。它旨在能够表达键值对、RDF数据、RDFa数据、Microformats数据以及Microdata数据。也就是说,它支持今天使用的每个主要的基于Web的结构化数据模型。

语法不需要许多应用程序更改它们的JSON,但可以通过在带内或带外的方式添加上下文来轻松添加意义。该语法旨在不干扰已经在JSON上运行的现有系统,而是提供一个从JSON到具有附加语义的JSON的平滑迁移路径。最后,该格式旨在快速解析、快速生成、基于流和文档处理兼容,并且为了运行需要非常小的内存占用。

一致性

此库旨在符合以下规范

测试运行器 通常会更新 以记录或跳过尚未支持的更新测试。

需求

安装

PyLD可以使用pip 安装。

pip install PyLD

在pyld上定义依赖项不会引入Requestsaiohttp。如果您需要一个文档加载器,则可以直接依赖所需的第三方库,或者将需求定义为PyLD[requests]PyLD[aiohttp]

快速示例

from pyld import jsonld
import json

doc = {
    "http://schema.org/name": "Manu Sporny",
    "http://schema.org/url": {"@id": "http://manu.sporny.org/"},
    "http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
}

context = {
    "name": "http://schema.org/name",
    "homepage": {"@id": "http://schema.org/url", "@type": "@id"},
    "image": {"@id": "http://schema.org/image", "@type": "@id"}
}

# compact a document according to a particular context
# see: https://json-ld.org/spec/latest/json-ld/#compacted-document-form
compacted = jsonld.compact(doc, context)

print(json.dumps(compacted, indent=2))
# Output:
# {
#   "@context": {...},
#   "image": "http://manu.sporny.org/images/manu.png",
#   "homepage": "http://manu.sporny.org/",
#   "name": "Manu Sporny"
# }

# compact using URLs
jsonld.compact('http://example.org/doc', 'http://example.org/context')

# expand a document, removing its context
# see: https://json-ld.org/spec/latest/json-ld/#expanded-document-form
expanded = jsonld.expand(compacted)

print(json.dumps(expanded, indent=2))
# Output:
# [{
#   "http://schema.org/image": [{"@id": "http://manu.sporny.org/images/manu.png"}],
#   "http://schema.org/name": [{"@value": "Manu Sporny"}],
#   "http://schema.org/url": [{"@id": "http://manu.sporny.org/"}]
# }]

# expand using URLs
jsonld.expand('http://example.org/doc')

# flatten a document
# see: https://json-ld.org/spec/latest/json-ld/#flattened-document-form
flattened = jsonld.flatten(doc)
# all deep-level trees flattened to the top-level

# frame a document
# see: https://json-ld.org/spec/latest/json-ld-framing/#introduction
framed = jsonld.frame(doc, frame)
# document transformed into a particular tree structure per the given frame

# normalize a document using the RDF Dataset Normalization Algorithm
# (URDNA2015), see: https://json-ld.github.io/normalization/spec/
normalized = jsonld.normalize(
    doc, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})
# normalized is a string that is a canonical representation of the document
# that can be used for hashing, comparison, etc.

文档加载器

PyLD的默认文档加载器使用Requests。在生产环境中,您可能需要设置一个自定义加载器,该加载器至少设置超时值。您还可以强制请求使用https、设置客户端证书、禁用验证或设置其他Requests参数。

jsonld.set_document_loader(jsonld.requests_document_loader(timeout=...))

使用aiohttp的异步文档加载器也可用。请注意,此文档加载器将异步性限制为仅获取文档。处理循环保持同步。

jsonld.set_document_loader(jsonld.aiohttp_document_loader(timeout=...))

如果没有指定文档加载器,则默认加载器设置为Requests。如果Requests不可用,则加载器设置为aiohttp。后备文档加载器是一个在每次调用时都会抛出异常的虚拟文档加载器。

商业支持

根据要求,Digital Bazaar提供此库的商业支持:Digital Bazaarsupport@digitalbazaar.com

源代码

JSON-LD API的Python实现源代码可在以下位置获取

https://github.com/digitalbazaar/pyld

测试

此库包括一个示例测试实用工具,可用于验证对处理器的更改是否保持了正确的输出。

要运行示例测试,您需要通过克隆GitHub上托管的json-ld-apijson-ld-framingnormalization存储库来获取测试套件文件

如果套件仓库作为PyLD源目录的兄弟目录可用,则可以使用以下命令运行所有测试:

python tests/runtests.py

如果您想测试单个的.jsonld文件或包含manifest.jsonld的目录,则可以将这些文件或目录作为参数提供

python tests/runtests.py TEST_PATH [TEST_PATH...]

测试运行器通过设置-l requests-l aiohttp支持不同的文档加载器。默认文档加载器设置为Requests

可以使用-e--earl选项生成EARL报告。

项目详情


下载文件

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

源分发

PyLDmod-2.0.5.tar.gz (70.7 kB 查看哈希值)

上传时间:

由支持