PyLD


NamePyLD JSON
Version 2.0.4 PyPI version JSON
download
home_pagehttps://github.com/digitalbazaar/pyld
SummaryPython implementation of the JSON-LD API
upload_time2024-02-16 17:35:51
maintainer
docs_urlNone
authorDigital Bazaar
requires_python
licenseBSD 3-Clause license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            PyLD
====

.. image:: https://travis-ci.org/digitalbazaar/pyld.png?branch=master
   :target: https://travis-ci.org/digitalbazaar/pyld
   :alt: Build Status

Introduction
------------

This library is an implementation of the JSON-LD_ specification in Python_.

JSON, as specified in RFC7159_, is a simple language for representing
objects on the Web. Linked Data is a way of describing content across
different documents or Web sites. Web resources are described using
IRIs, and typically are dereferencable entities that may be used to find
more information, creating a "Web of Knowledge". JSON-LD_ is intended
to be a simple publishing method for expressing not only Linked Data in
JSON, but for adding semantics to existing JSON.

JSON-LD is designed as a light-weight syntax that can be used to express
Linked Data. It is primarily intended to be a way to express Linked Data
in JavaScript and other Web-based programming environments. It is also
useful when building interoperable Web Services and when storing Linked
Data in JSON-based document storage engines. It is practical and
designed to be as simple as possible, utilizing the large number of JSON
parsers and existing code that is in use today. It is designed to be
able to express key-value pairs, RDF data, RDFa_ data,
Microformats_ data, and Microdata_. That is, it supports every
major Web-based structured data model in use today.

The syntax does not require many applications to change their JSON, but
easily add meaning by adding context in a way that is either in-band or
out-of-band. The syntax is designed to not disturb already deployed
systems running on JSON, but provide a smooth migration path from JSON
to JSON with added semantics. Finally, the format is intended to be fast
to parse, fast to generate, stream-based and document-based processing
compatible, and require a very small memory footprint in order to operate.

Conformance
-----------

This library aims to conform with the following:

- `JSON-LD 1.1 <JSON-LD WG 1.1_>`_,
  W3C Candidate Recommendation,
  2019-12-12 or `newer <JSON-LD WG latest_>`_
- `JSON-LD 1.1 Processing Algorithms and API <JSON-LD WG 1.1 API_>`_,
  W3C Candidate Recommendation,
  2019-12-12 or `newer <JSON-LD WG API latest_>`_
- `JSON-LD 1.1 Framing <JSON-LD WG 1.1 Framing_>`_,
  W3C Candidate Recommendation,
  2019-12-12 or `newer <JSON-LD WG Framing latest_>`_
- Working Group `test suite <WG test suite_>`_

The `test runner`_ is often updated to note or skip newer tests that are not
yet supported.

Requirements
------------

- Python_ (3.6 or later)
- Requests_ (optional)
- aiohttp_ (optional, Python 3.5 or later)

Installation
------------

PyLD can be installed with a pip_ `package <https://pypi.org/project/PyLD/>`_

.. code-block:: bash

    pip install PyLD

Defining a dependency on pyld will not pull in Requests_ or aiohttp_.  If you
need one of these for a `Document Loader`_ then either depend on the desired
external library directly or define the requirement as ``PyLD[requests]`` or
``PyLD[aiohttp]``.

Quick Examples
--------------

.. code-block:: Python

    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://www.w3.org/TR/rdf-canon/
    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.

Document Loader
---------------

The default document loader for PyLD uses Requests_. In a production
environment you may want to setup a custom loader that, at a minimum, sets a
timeout value. You can also force requests to use https, set client certs,
disable verification, or set other Requests_ parameters.

.. code-block:: Python

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

An asynchronous document loader using aiohttp_ is also available. Please note
that this document loader limits asynchronicity to fetching documents only.
The processing loops remain synchronous.

.. code-block:: Python

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

When no document loader is specified, the default loader is set to Requests_.
If Requests_ is not available, the loader is set to aiohttp_. The fallback
document loader is a dummy document loader that raises an exception on every
invocation.

Commercial Support
------------------

Commercial support for this library is available upon request from
`Digital Bazaar`_: support@digitalbazaar.com.

Source
------

The source code for the Python implementation of the JSON-LD API
is available at:

https://github.com/digitalbazaar/pyld

Tests
-----

This library includes a sample testing utility which may be used to verify
that changes to the processor maintain the correct output.

To run the sample tests you will need to get the test suite files by cloning
the ``json-ld-api``, ``json-ld-framing``, and ``normalization`` repositories
hosted on GitHub:

- https://github.com/w3c/json-ld-api
- https://github.com/w3c/json-ld-framing
- https://github.com/json-ld/normalization

If the suites repositories are available as sibling directories of the PyLD
source directory, then all the tests can be run with the following:

.. code-block:: bash

    python tests/runtests.py

If you want to test individual manifest ``.jsonld`` files or directories
containing a ``manifest.jsonld``, then you can supply these files or
directories as arguments:

.. code-block:: bash

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

The test runner supports different document loaders by setting ``-l requests``
or ``-l aiohttp``. The default document loader is set to Requests_.

An EARL report can be generated using the ``-e`` or ``--earl`` option.


.. _Digital Bazaar: https://digitalbazaar.com/

.. _JSON-LD WG 1.1 API: https://www.w3.org/TR/json-ld11-api/
.. _JSON-LD WG 1.1 Framing: https://www.w3.org/TR/json-ld11-framing/
.. _JSON-LD WG 1.1: https://www.w3.org/TR/json-ld11/

.. _JSON-LD WG API latest: https://w3c.github.io/json-ld-api/
.. _JSON-LD WG Framing latest: https://w3c.github.io/json-ld-framing/
.. _JSON-LD WG latest: https://w3c.github.io/json-ld-syntax/

.. _JSON-LD Benchmarks: https://json-ld.org/benchmarks/
.. _JSON-LD WG: https://www.w3.org/2018/json-ld-wg/
.. _JSON-LD: https://json-ld.org/
.. _Microdata: http://www.w3.org/TR/microdata/
.. _Microformats: http://microformats.org/
.. _Python: https://www.python.org/
.. _Requests: http://docs.python-requests.org/
.. _aiohttp: https://aiohttp.readthedocs.io/
.. _RDFa: http://www.w3.org/TR/rdfa-core/
.. _RFC7159: http://tools.ietf.org/html/rfc7159
.. _WG test suite: https://github.com/w3c/json-ld-api/tree/master/tests
.. _errata: http://www.w3.org/2014/json-ld-errata
.. _pip: http://www.pip-installer.org/
.. _test runner: https://github.com/digitalbazaar/pyld/blob/master/tests/runtests.py
.. _test suite: https://github.com/json-ld/json-ld.org/tree/master/test-suite

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/digitalbazaar/pyld",
    "name": "PyLD",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Digital Bazaar",
    "author_email": "support@digitalbazaar.com",
    "download_url": "https://files.pythonhosted.org/packages/50/0b/d97dddcc079d4961aa38bec1ad444b8a3e39ea0fd5627682cac25d452c82/PyLD-2.0.4.tar.gz",
    "platform": null,
    "description": "PyLD\n====\n\n.. image:: https://travis-ci.org/digitalbazaar/pyld.png?branch=master\n   :target: https://travis-ci.org/digitalbazaar/pyld\n   :alt: Build Status\n\nIntroduction\n------------\n\nThis library is an implementation of the JSON-LD_ specification in Python_.\n\nJSON, as specified in RFC7159_, is a simple language for representing\nobjects on the Web. Linked Data is a way of describing content across\ndifferent documents or Web sites. Web resources are described using\nIRIs, and typically are dereferencable entities that may be used to find\nmore information, creating a \"Web of Knowledge\". JSON-LD_ is intended\nto be a simple publishing method for expressing not only Linked Data in\nJSON, but for adding semantics to existing JSON.\n\nJSON-LD is designed as a light-weight syntax that can be used to express\nLinked Data. It is primarily intended to be a way to express Linked Data\nin JavaScript and other Web-based programming environments. It is also\nuseful when building interoperable Web Services and when storing Linked\nData in JSON-based document storage engines. It is practical and\ndesigned to be as simple as possible, utilizing the large number of JSON\nparsers and existing code that is in use today. It is designed to be\nable to express key-value pairs, RDF data, RDFa_ data,\nMicroformats_ data, and Microdata_. That is, it supports every\nmajor Web-based structured data model in use today.\n\nThe syntax does not require many applications to change their JSON, but\neasily add meaning by adding context in a way that is either in-band or\nout-of-band. The syntax is designed to not disturb already deployed\nsystems running on JSON, but provide a smooth migration path from JSON\nto JSON with added semantics. Finally, the format is intended to be fast\nto parse, fast to generate, stream-based and document-based processing\ncompatible, and require a very small memory footprint in order to operate.\n\nConformance\n-----------\n\nThis library aims to conform with the following:\n\n- `JSON-LD 1.1 <JSON-LD WG 1.1_>`_,\n  W3C Candidate Recommendation,\n  2019-12-12 or `newer <JSON-LD WG latest_>`_\n- `JSON-LD 1.1 Processing Algorithms and API <JSON-LD WG 1.1 API_>`_,\n  W3C Candidate Recommendation,\n  2019-12-12 or `newer <JSON-LD WG API latest_>`_\n- `JSON-LD 1.1 Framing <JSON-LD WG 1.1 Framing_>`_,\n  W3C Candidate Recommendation,\n  2019-12-12 or `newer <JSON-LD WG Framing latest_>`_\n- Working Group `test suite <WG test suite_>`_\n\nThe `test runner`_ is often updated to note or skip newer tests that are not\nyet supported.\n\nRequirements\n------------\n\n- Python_ (3.6 or later)\n- Requests_ (optional)\n- aiohttp_ (optional, Python 3.5 or later)\n\nInstallation\n------------\n\nPyLD can be installed with a pip_ `package <https://pypi.org/project/PyLD/>`_\n\n.. code-block:: bash\n\n    pip install PyLD\n\nDefining a dependency on pyld will not pull in Requests_ or aiohttp_.  If you\nneed one of these for a `Document Loader`_ then either depend on the desired\nexternal library directly or define the requirement as ``PyLD[requests]`` or\n``PyLD[aiohttp]``.\n\nQuick Examples\n--------------\n\n.. code-block:: Python\n\n    from pyld import jsonld\n    import json\n\n    doc = {\n        \"http://schema.org/name\": \"Manu Sporny\",\n        \"http://schema.org/url\": {\"@id\": \"http://manu.sporny.org/\"},\n        \"http://schema.org/image\": {\"@id\": \"http://manu.sporny.org/images/manu.png\"}\n    }\n\n    context = {\n        \"name\": \"http://schema.org/name\",\n        \"homepage\": {\"@id\": \"http://schema.org/url\", \"@type\": \"@id\"},\n        \"image\": {\"@id\": \"http://schema.org/image\", \"@type\": \"@id\"}\n    }\n\n    # compact a document according to a particular context\n    # see: https://json-ld.org/spec/latest/json-ld/#compacted-document-form\n    compacted = jsonld.compact(doc, context)\n\n    print(json.dumps(compacted, indent=2))\n    # Output:\n    # {\n    #   \"@context\": {...},\n    #   \"image\": \"http://manu.sporny.org/images/manu.png\",\n    #   \"homepage\": \"http://manu.sporny.org/\",\n    #   \"name\": \"Manu Sporny\"\n    # }\n\n    # compact using URLs\n    jsonld.compact('http://example.org/doc', 'http://example.org/context')\n\n    # expand a document, removing its context\n    # see: https://json-ld.org/spec/latest/json-ld/#expanded-document-form\n    expanded = jsonld.expand(compacted)\n\n    print(json.dumps(expanded, indent=2))\n    # Output:\n    # [{\n    #   \"http://schema.org/image\": [{\"@id\": \"http://manu.sporny.org/images/manu.png\"}],\n    #   \"http://schema.org/name\": [{\"@value\": \"Manu Sporny\"}],\n    #   \"http://schema.org/url\": [{\"@id\": \"http://manu.sporny.org/\"}]\n    # }]\n\n    # expand using URLs\n    jsonld.expand('http://example.org/doc')\n\n    # flatten a document\n    # see: https://json-ld.org/spec/latest/json-ld/#flattened-document-form\n    flattened = jsonld.flatten(doc)\n    # all deep-level trees flattened to the top-level\n\n    # frame a document\n    # see: https://json-ld.org/spec/latest/json-ld-framing/#introduction\n    framed = jsonld.frame(doc, frame)\n    # document transformed into a particular tree structure per the given frame\n\n    # normalize a document using the RDF Dataset Normalization Algorithm\n    # (URDNA2015), see: https://www.w3.org/TR/rdf-canon/\n    normalized = jsonld.normalize(\n        doc, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})\n    # normalized is a string that is a canonical representation of the document\n    # that can be used for hashing, comparison, etc.\n\nDocument Loader\n---------------\n\nThe default document loader for PyLD uses Requests_. In a production\nenvironment you may want to setup a custom loader that, at a minimum, sets a\ntimeout value. You can also force requests to use https, set client certs,\ndisable verification, or set other Requests_ parameters.\n\n.. code-block:: Python\n\n    jsonld.set_document_loader(jsonld.requests_document_loader(timeout=...))\n\nAn asynchronous document loader using aiohttp_ is also available. Please note\nthat this document loader limits asynchronicity to fetching documents only.\nThe processing loops remain synchronous.\n\n.. code-block:: Python\n\n    jsonld.set_document_loader(jsonld.aiohttp_document_loader(timeout=...))\n\nWhen no document loader is specified, the default loader is set to Requests_.\nIf Requests_ is not available, the loader is set to aiohttp_. The fallback\ndocument loader is a dummy document loader that raises an exception on every\ninvocation.\n\nCommercial Support\n------------------\n\nCommercial support for this library is available upon request from\n`Digital Bazaar`_: support@digitalbazaar.com.\n\nSource\n------\n\nThe source code for the Python implementation of the JSON-LD API\nis available at:\n\nhttps://github.com/digitalbazaar/pyld\n\nTests\n-----\n\nThis library includes a sample testing utility which may be used to verify\nthat changes to the processor maintain the correct output.\n\nTo run the sample tests you will need to get the test suite files by cloning\nthe ``json-ld-api``, ``json-ld-framing``, and ``normalization`` repositories\nhosted on GitHub:\n\n- https://github.com/w3c/json-ld-api\n- https://github.com/w3c/json-ld-framing\n- https://github.com/json-ld/normalization\n\nIf the suites repositories are available as sibling directories of the PyLD\nsource directory, then all the tests can be run with the following:\n\n.. code-block:: bash\n\n    python tests/runtests.py\n\nIf you want to test individual manifest ``.jsonld`` files or directories\ncontaining a ``manifest.jsonld``, then you can supply these files or\ndirectories as arguments:\n\n.. code-block:: bash\n\n    python tests/runtests.py TEST_PATH [TEST_PATH...]\n\nThe test runner supports different document loaders by setting ``-l requests``\nor ``-l aiohttp``. The default document loader is set to Requests_.\n\nAn EARL report can be generated using the ``-e`` or ``--earl`` option.\n\n\n.. _Digital Bazaar: https://digitalbazaar.com/\n\n.. _JSON-LD WG 1.1 API: https://www.w3.org/TR/json-ld11-api/\n.. _JSON-LD WG 1.1 Framing: https://www.w3.org/TR/json-ld11-framing/\n.. _JSON-LD WG 1.1: https://www.w3.org/TR/json-ld11/\n\n.. _JSON-LD WG API latest: https://w3c.github.io/json-ld-api/\n.. _JSON-LD WG Framing latest: https://w3c.github.io/json-ld-framing/\n.. _JSON-LD WG latest: https://w3c.github.io/json-ld-syntax/\n\n.. _JSON-LD Benchmarks: https://json-ld.org/benchmarks/\n.. _JSON-LD WG: https://www.w3.org/2018/json-ld-wg/\n.. _JSON-LD: https://json-ld.org/\n.. _Microdata: http://www.w3.org/TR/microdata/\n.. _Microformats: http://microformats.org/\n.. _Python: https://www.python.org/\n.. _Requests: http://docs.python-requests.org/\n.. _aiohttp: https://aiohttp.readthedocs.io/\n.. _RDFa: http://www.w3.org/TR/rdfa-core/\n.. _RFC7159: http://tools.ietf.org/html/rfc7159\n.. _WG test suite: https://github.com/w3c/json-ld-api/tree/master/tests\n.. _errata: http://www.w3.org/2014/json-ld-errata\n.. _pip: http://www.pip-installer.org/\n.. _test runner: https://github.com/digitalbazaar/pyld/blob/master/tests/runtests.py\n.. _test suite: https://github.com/json-ld/json-ld.org/tree/master/test-suite\n",
    "bugtrack_url": null,
    "license": "BSD 3-Clause license",
    "summary": "Python implementation of the JSON-LD API",
    "version": "2.0.4",
    "project_urls": {
        "Homepage": "https://github.com/digitalbazaar/pyld"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "44cd80760be197a4bd08e7c136ef4bcb4a2c63fc799d8d91f4c177b21183135e",
                "md5": "5f4ad80779d874193f6ff6b6ca04c5a9",
                "sha256": "6dab9905644616df33f8755489fc9b354ed7d832d387b7d1974b4fbd3b8d2a89"
            },
            "downloads": -1,
            "filename": "PyLD-2.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5f4ad80779d874193f6ff6b6ca04c5a9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 70868,
            "upload_time": "2024-02-16T17:35:49",
            "upload_time_iso_8601": "2024-02-16T17:35:49.000979Z",
            "url": "https://files.pythonhosted.org/packages/44/cd/80760be197a4bd08e7c136ef4bcb4a2c63fc799d8d91f4c177b21183135e/PyLD-2.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "500bd97dddcc079d4961aa38bec1ad444b8a3e39ea0fd5627682cac25d452c82",
                "md5": "e23569a9ed133cd574e2f55d33b38e15",
                "sha256": "311e350f0dbc964311c79c28e86f84e195a81d06fef5a6f6ac2a4f6391ceeacc"
            },
            "downloads": -1,
            "filename": "PyLD-2.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "e23569a9ed133cd574e2f55d33b38e15",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 70976,
            "upload_time": "2024-02-16T17:35:51",
            "upload_time_iso_8601": "2024-02-16T17:35:51.481528Z",
            "url": "https://files.pythonhosted.org/packages/50/0b/d97dddcc079d4961aa38bec1ad444b8a3e39ea0fd5627682cac25d452c82/PyLD-2.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-16 17:35:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "digitalbazaar",
    "github_project": "pyld",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "pyld"
}
        
Elapsed time: 0.20547s