Skip to main content

Tiny toolkit for composing URI namespaces

Project description

Namespacecraft

Namespacecraft is a tiny toolkit for composing URI namespaces.

Use

>>> from namespacecraft import Namespace
>>> EX = Namespace('http://example.org/')
>>> EX.a
Term('http://example.org/a')
>>> Namespace('http://example.org').terminates_with('#') / 'a' / 'b' / 'c' + 'x'
Term('http://example.org/a/b/c#x')

Paths

Build paths using the / operator.

>>> Namespace('http://example.org/') / 'a' / 'b' / 'c'
Namespace('http://example.org/a/b/c')

You can pass a list or tuple to / to append multiple path components at once.

>>> Namespace('http://example.org') / [1, 2, 3]
Namespace('http://example.org/1/2/3')

Use terminates_with() to set the final delimiter.

>>> Namespace('http://example.org').terminates_with('#') / 'a' / 'b'
Namespace('http://example.org/a/b#')

Terms

Create terms by accessing a Namespace attribute.

>>> EX = Namespace('http://example.org/')
>>> EX.a
Term('http://example.org/a')

Or by getting an attribute by name.

>>> EX['b']
Term('http://example.org/b')

Or with the + operator.

>>> EX + 1
Term('http://example.org/1')

Namespace will create terms in any class that initialises from str. For example create terms as instances of rdflib.URIRef.

from namespacecraft import Namespace
from rdflib import Graph, URIRef


EX = Namespace('http://example.org/', term_cls=URIRef).terminates_with('/') / 'a/b/c'
graph = Graph()
graph.add((EX.s, EX.p, EX.o))
print(graph.serialize(format='turtle'))
@prefix ns1: <http://example.org/a/b/c/> .

ns1:s ns1:p ns1:o .

Install

pip install namespacecraft

Gotchas

  • The + operator returns a Term object. Any further + operations are just string concatenations.

    >>> Namespace('http://example.org/') + 'a'
    Term('http://example.org/a')
    >>> Namespace('http://example.org/') + 'a' + 'b'
    Term('http://example.org/ab')
    
  • Namespaces ending with # are always terminal. Any path added via / immediately returns a Term, and further / operations are not allowed.

    >>> BASE = Namespace('http://example.org#')
    >>> BASE / 'section'
    Term('http://example.org#section')
    >>> (BASE / 'section') / 'subsection'
    TypeError: unsupported operand type(s) for /: 'Term' and 'str'
    
  • You cannot set a trailing delimiter on a hash namespace. Attempting to do so will raise a ValueError.

    >>> BASE = Namespace('http://example.org#')
    >>> BASE.terminates_with('/')
    ValueError: Cannot set a trailing delimiter on a hash namespace
    

Test

pytest

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

namespacecraft-0.3.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

namespacecraft-0.3.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file namespacecraft-0.3.1.tar.gz.

File metadata

  • Download URL: namespacecraft-0.3.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for namespacecraft-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a8a1d0bb30586893b805f38c6d0e405ba15f96ef5507529227672611a2d201ae
MD5 eb0905e416729c8f2bc5d1694c66198c
BLAKE2b-256 2b7a4e4dab78e9225ab71c6b948c80b95a39e5bb10a34f8d51672b347c12d8b7

See more details on using hashes here.

File details

Details for the file namespacecraft-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: namespacecraft-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for namespacecraft-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 454182955e6f8e3c63a61d81f826f9df205854cfe88616fc34f789444e74341f
MD5 e380812621d07dd1fd357bde7ed6dee5
BLAKE2b-256 825c10a8aee168ccadfe2089e6beac3a6772dcccbffdc000d81e1c842330713a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page