跳转到主要内容

URI模板

项目描述

uritemplate

文档GitHubBitBucketTravis-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所依赖的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.py以支持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 查看哈希)

上传时间

构建分发

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

上传时间 Python 2 Python 3

由以下支持