You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
![]() |
1 week ago | |
---|---|---|
.github/workflows | 10 months ago | |
docs/source | 10 months ago | |
tests | 7 months ago | |
uritemplate | 10 months ago | |
.gitignore | 10 months ago | |
.mailmap | 5 years ago | |
.pre-commit-config.yaml | 1 week ago | |
.tool-versions | 10 months ago | |
.travis.yml | 3 years ago | |
AUTHORS.rst | 5 years ago | |
HISTORY.rst | 10 months ago | |
LICENSE | 3 years ago | |
LICENSE.APACHE | 6 years ago | |
LICENSE.BSD | 10 months ago | |
MANIFEST.in | 6 years ago | |
README.rst | 10 months ago | |
pyproject.toml | 10 months ago | |
setup.cfg | 1 week ago | |
setup.py | 10 months ago | |
tox.ini | 10 months ago |
README.rst
uritemplate
Documentation -- GitHub -- Travis-CI
Simple python library to deal with URI Templates. The API looks like
from uritemplate import URITemplate, expand
# NOTE: URI params must be strings not integers
= 'https://api.github.com/users/sigmavirus24/gists{/gist_id}'
gist_uri = URITemplate(gist_uri)
t print(t.expand(gist_id='123456'))
# => https://api.github.com/users/sigmavirus24/gists/123456
# or
print(expand(gist_uri, gist_id='123456'))
# also
'gist_id': '123456'})
t.expand({print(expand(gist_uri, {'gist_id': '123456'}))
Where it might be useful to have a class
import requests
class GitHubUser(object):
= URITemplate('https://api.github.com/user{/login}')
url def __init__(self, name):
self.api_url = url.expand(login=name)
= requests.get(self.api_url)
response if response.status_code == 200:
self.__dict__.update(response.json())
When the module containing this class is loaded, GitHubUser.url
is evaluated and so the template is created once. It's often hard to notice in Python, but object creation can consume a great deal of time and so can the re
module which uritemplate relies on. Constructing the object once should reduce the amount of time your code takes to run.
Installing
pip install uritemplate
License
Modified BSD license