跳转到主要内容

RFC 6570 URI模板的实现

项目描述

文档GitHubTravis-CI

一个简单的Python库,用于处理URI模板。API看起来像

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'}))

当需要此类的地方

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())

当加载包含此类的模块时,GitHubUser.url会被评估,因此模板只创建一次。在Python中,这往往不容易注意到,但对象的创建以及uritemplate所依赖的< span class="docutils literal">re模块都可能消耗大量的时间。构建对象一次可以减少你的代码运行所需的时间。

安装

pip install uritemplate

许可证

修改后的BSD许可协议

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

uritemplate-4.1.1.tar.gz (273.9 kB 查看哈希值)

上传时间 源代码

构建分发

uritemplate-4.1.1-py2.py3-none-any.whl (10.4 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面