Skip to content
URI template parsing for Humans
Python
Branch: master
Clone or download
sigmavirus24 Merge pull request #43 from metatoaster/list_of_empty
Correct handling of empty/undef in list values
Latest commit df03b63 Jul 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs Add missing variables function Aug 23, 2016
old/uritemplate.py Drop support for EOL Python Sep 6, 2018
tests Correct handling of empty/undef in list values Jul 11, 2019
uritemplate Correct handling of empty/undef in list values Jul 11, 2019
.gitignore Update gitignore May 20, 2013
.mailmap Add .mailmap to map old name to new name Jul 8, 2017
.travis.yml
AUTHORS.rst Update project for new organization home Jul 26, 2017
HISTORY.rst Fix requirement on uritemplate Aug 31, 2016
LICENSE Relicense uritemplate.py Aug 18, 2016
LICENSE.APACHE Relicense uritemplate.py Aug 18, 2016
LICENSE.BSD Relicense uritemplate.py Aug 18, 2016
MANIFEST.in Include individaul license files Aug 19, 2016
README.rst Update project for new organization home Jul 26, 2017
setup.cfg
setup.py Drop support for EOL Python Sep 6, 2018
tox.ini Drop support for EOL Python Sep 6, 2018

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

gist_uri = 'https://api.github.com/users/sigmavirus24/gists{/gist_id}'
t = URITemplate(gist_uri)
print(t.expand(gist_id='123456'))
# => https://api.github.com/users/sigmavirus24/gists/123456

# or
print(expand(gist_uri, gist_id='123456'))

# also
t.expand({'gist_id': '123456'})
print(expand(gist_uri, {'gist_id': '123456'}))

Where it might be useful to have a class

import requests

class GitHubUser(object):
    url = URITemplate('https://api.github.com/user{/login}')
    def __init__(self, name):
        self.api_url = url.expand(login=name)
        response = requests.get(self.api_url)
        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

You can’t perform that action at this time.