Registry / serialization / uri-template

uri-template

JSON →
library1.3.0pypypi✓ verified 30d ago

The `uri-template` library provides an implementation of RFC 6570 URI Templates in Python. It supports template expansion in strict adherence to the RFC, while also introducing several extensions for handling non-string values, nested structures, and partial expansions. The current version is 1.3.0, released in June 2023. It appears to be maintained with a slower release cadence by a single maintainer.

pip install uri-template
INSTALL
IMPORT
SIG · URI-TEMPLATE
U
uri-template
serializationpythonv1.3.0
Install
1.6s avg
Import
13ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.014s · 17.9MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.012s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

expand
✓ from uri_template import expand
partial
✓ from uri_template import partial
URITemplate
✓ from uri_template import URITemplate

This quickstart demonstrates basic URI template expansion using both the standalone `expand` function and the `URITemplate` class. It also shows how to perform partial template expansions.

from uri_template import URITemplate, expand # Using the expand function for a simple, one-off expansion uri_str = "http://example.com/api/{resource}/{id}" expanded_uri_func = expand(uri_str, resource='users', id='123') print(f"Function expanded: {expanded_uri_func}") # Using the URITemplate class for repeated expansions or advanced features template = URITemplate("http://example.com/search{?query,limit}") expanded_uri_class = template.expand(query='python', limit=10) print(f"Class expanded: {expanded_uri_class}") # Example of partial expansion partial_template = URITemplate("http://example.com/items/{category}/{item_id}") partially_expanded = partial_template.partial(category='electronics') print(f"Partially expanded: {partially_expanded}") # Further expand the partially expanded template final_expanded = partially_expanded.expand(item_id='laptop-x') print(f"Further expanded: {final_expanded}")
Debug
Known issues
gotchaThere are two similarly named, but distinct, PyPI packages for URI templates: `uri-template` (this package, with a hyphen) and `uritemplate` (without a hyphen). They have different APIs, features, and maintenance statuses. Ensure you install and use the correct package according to your project's needs.
fix
Verify which package is required for your project. If you intend to use the `uritemplate` (no hyphen) package, install `pip install uritemplate` and import `from uritemplate import ...`.
affects: All versions
gotchaThe `uri-template` library extends RFC 6570 to handle non-string values and nested data structures (lists, dicts, booleans) by converting them or flattening them in specific ways. While convenient, this behaviour deviates from strict RFC 6570 and may lead to unexpected results if not understood.
fix
Refer to the library's documentation on 'RFC 6570 Extensions' to understand how different Python types are handled during expansion, especially for nested structures or non-standard primitive types.
affects: All versions
gotchaThe `URITemplate.expand()` and `uri_template.expand()` methods can raise an `ExpansionFailed` exception if a composite value (e.g., a list or dictionary) is provided for a variable that uses a prefix modifier (e.g., `{var:10}`).
fix
Avoid using prefix modifiers with composite values or handle the `ExpansionFailed` exception in your code. Ensure that the types of values passed to template variables are compatible with the specified modifiers.
affects: All versions
gotchaThis library only supports default *string* values for variables, as in `{foo=bar}`. It does not currently support default values for lists or associative arrays.
fix
If default values are needed for non-string types, you must provide them programmatically before calling the `expand` method, rather than relying on the template syntax.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'uri_template'
The `uri-template` Python package is not installed or not accessible in the current environment.
fix
pip install uri-template
SyntaxError: invalid syntax
Python import statements cannot contain hyphens; the correct import name for the `uri-template` package is `uri_template`.
fix
import uri_template
AttributeError: 'int' object has no attribute 'items'
The `variables` argument in `uri_template.expand()` must be a dictionary or a mapping-like object, not a scalar value.
fix
uri_template.expand("{name}", {"name": "value"})
ValueError: Expected '}', got end of string
The URI template string is malformed, specifically missing a closing brace `}` for a variable expression.
fix
uri_template.expand("{var_name}", {"var_name": "value"})
Upgrade
Version history
1.3.0latest on PyPI · released Jun 21, 2023
Audit
Dependencies
pythonrequiredRequires Python 3.7 or newer.
Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
1
Resources
uri-template — pip install uri-template · libregistry