Skip to main content

URI 模板

项目描述

uri模板

文档GitHubBitBucketTravis-CI

处理URI 模板的简单 python 库。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 所依赖的re模块也会如此。构造一次对象应该可以减少代码运行的时间。

安装

pip install uritemplate.py

执照

修改后的 BSD许可证

变更日志 - uritemplate

2.0.0 - 2016-08-29

  • 将 uritemplate.py 合并到 uritemplate

变更日志 - uritemplate.py

3.0.2 - 2015-08-30

  • 修复元包要求。

3.0.1 - 2015-08-29

  • 弃用 uritemplate。这个包现在是一个依赖于 uritemplate 的元包。

2.0.0 - 2016-08-20

1.0.1 - 2016-08-18

  • 修复一些小的包装问题。

1.0.0 - 2016-08-17

  • 修复了 Python 2.6 和 2.7 上 urllib.quote 的 Unicode 值处理。

  • 通过版本号确认公共稳定 API。

0.3.0 - 2013-10-22

  • 添加#partial以部分扩展模板并返回 URITemplate的新实例。

0.2.0 - 2013-07-26

  • 稍微重构一下库并添加更多测试。

  • 如果直接从 uritemplate.template使用URIVariable ,则向后与 0.1.x 不兼容

0.1.1 - 2013-05-19

  • 添加在当前 URI 中获取变量名称集的功能

  • 如果没有给出值或默认值,只需返回一个空字符串

  • 修复 sdist

0.1.0 - 2013-05-14

  • 初始发行

项目详情


下载文件

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

源分布

uritemplate.py-3.0.2.tar.gz (30.6 kB 查看哈希

已上传 source

内置分布

uritemplate.py-3.0.2-py2.py3-none-any.whl (4.9 kB 查看哈希

已上传 py2 py3