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
Metadata
Release files for namespacecraft 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| namespacecraft-0.3.2.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| namespacecraft-0.3.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.5 kB
Release files / namespacecraft-0.3.2.tar.gz
| Download URL | namespacecraft-0.3.2.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eca6fad38774a614d2ef5b274ed53e59e61449a2c4eb5520230a2a99f7a379c3
|
|
BLAKE2b-256 checksum How to use checksums |
2ef83ae987b1bc950b1fa7f52f8195c8e32d27e2cc0dce37825c128ba9f0f94e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / namespacecraft-0.3.2-py3-none-any.whl
| Download URL | namespacecraft-0.3.2-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b43197f883781daaf20c9dec8938e5f3c4d7994ad8bea8facde6443a25b85aae
|
|
BLAKE2b-256 checksum How to use checksums |
0d5288ea9aecee9a7e5a6293292ad9497c14b858c8684afbbb4d75872c4ffe65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|