Skip to main content
Build status Code health Latest version MIT license

Ordered Turtle Serializer for rdflib

An extension to the rdflib Turtle serializer that adds order (at the price of speed). Useful when you need to generate diffs between Turtle files, or just to make it easier for human beings to inspect the files.

$ pip install otsrdflib

Usage:

from rdflib import graph
from otsrdflib import OrderedTurtleSerializer

my_graph = Graph()

out = open('out.ttl', 'wb')
serializer = OrderedTurtleSerializer(my_graph)
serializer.serialize(out)

Class order

By default, classes are ordered alphabetically by their URIS.

A custom order can be imposed by adding classes to the class_order attribute. For a SKOS vocabulary, for instance, you might want to sort the concept scheme first, followed by the other elements of the vocabulary:

serializer.class_order = [
    SKOS.ConceptScheme,
    SKOS.Concept,
    ISOTHES.ThesaurusArray,
]

Any class not included in the class_order list will be sorted alphabetically at the end, after the classes included in the list.

Instance order

By default, instances of a class are ordered alphabetically by their URIS.

A custom order can be imposed by defining functions that generate sort keys from the URIs. For instance, you could define a function that returns the numeric last part of an URI to be sorted numerically:

serializer.sorters = [
    ('.*?/[^0-9]*([0-9.]+)$', lambda x: float(x[0])),
]

The first element of the tuple (‘.*?/[^0-9]*([0-9.]+)$’) is the regexp pattern to be matched against the URIs, while the second element (lambda x: float(x[0])) is the sort key generating function. In this case, it returns the first backreference as a float.

The patterns in sorters will be attempted matched against instances of any class. You can also define patterns that will only be matched against instances of a specific class. Let’s say you only wanted to sort instances of SKOS.Concept this way:

from rdflib.namespace import SKOS

serializer.sorters_by_class = {
    SKOS.Concept: [
        ('.*?/[^0-9]*([0-9.]+)$', lambda x: float(x[0])),
    ]
}

For a slightly more complicated example, let’s look at Dewey. Classes in the main schedules are describes by URIs like http://dewey.info/class/001.433/e23/, and we will use the class number (001.433) for sorting. But there’s also table classes like http://dewey.info/class/1–0901/e23/. We want to sort these at the end, after the main schedules. To achieve this, we define two sorters, one that matches the table classes and one that matches the main schedule classes:

serializer.sorters = [
    ('/([0-9A-Z\-]+)\-\-([0-9.\-;:]+)/e', lambda x: 'T{}--{}'.format(x[0], x[1])),  # table numbers
    ('/([0-9.\-;:]+)/e', lambda x: 'A' + x[0]),  # main schedule numbers
]

By prefixing the table numbers with ‘T’ and the main schedule numbers with ‘A’, we ensure the table numbers are sorted after the main schedule numbers.

Changes in version 0.5

  • The topClasses attribute was renamed to class_order to better reflect its content and comply with PEP8. It was also changed to be empty by default, since the previous default list was rather random.

  • A sorters_by_class attribute was added to allow sorters to be defined per class.

Metadata

Release files for otsrdflib 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for otsrdflib 0.5.0
File Size Uploaded Provenance
otsrdflib-0.5.0.tar.gz 4.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for otsrdflib 0.5.0
File Interpreter ABI Platform
otsrdflib-0.5.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 11.6 kB

Release files / otsrdflib-0.5.0.tar.gz

Download URL otsrdflib-0.5.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
937f02cd655487c0632aa1dcab6f7aabd956fa9c3415ec30bb90726a8011d626
BLAKE2b-256 checksum
How to use checksums
151fc8b55fcb049714e4fe443c9c6a280b5915ff63bc347a142e304bf641b714
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / otsrdflib-0.5.0-py2.py3-none-any.whl

Download URL otsrdflib-0.5.0-py2.py3-none-any.whl
Size 6.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c05605d093b0263c5500e852f81d185ce14dabbcbc323a600c6ca24bd7b7bfca
BLAKE2b-256 checksum
How to use checksums
23d3c9e172abeb9a043154f44cffaadc079bea37fb604cf59beee536e543a61e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for otsrdflib 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page