Composable 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 aTermobject. 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 aTerm, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file namespacecraft-0.3.2.tar.gz.
File metadata
- Download URL: namespacecraft-0.3.2.tar.gz
- Upload date:
- Size: 5.5 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eca6fad38774a614d2ef5b274ed53e59e61449a2c4eb5520230a2a99f7a379c3
|
|
| MD5 |
a995a40201d1c316260f1f03051eebc1
|
|
| BLAKE2b-256 |
2ef83ae987b1bc950b1fa7f52f8195c8e32d27e2cc0dce37825c128ba9f0f94e
|
File details
Details for the file namespacecraft-0.3.2-py3-none-any.whl.
File metadata
- Download URL: namespacecraft-0.3.2-py3-none-any.whl
- Upload date:
- Size: 5.0 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b43197f883781daaf20c9dec8938e5f3c4d7994ad8bea8facde6443a25b85aae
|
|
| MD5 |
1efb61e8916399dc74e6093f3f004f40
|
|
| BLAKE2b-256 |
0d5288ea9aecee9a7e5a6293292ad9497c14b858c8684afbbb4d75872c4ffe65
|