URI跟URI模板
uritemplate的Python项目详细描述
uritemplate
Documentation–GitHub–BitBucket–Travis-CI
处理URI Templates的简单python库。api看起来像
fromuritemplateimportURITemplate,expand# NOTE: URI params must be strings not integersgist_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# orprint(expand(gist_uri,gist_id='123456'))# alsot.expand({'gist_id':'123456'})print(expand(gist_uri,{'gist_id':'123456'}))
如果有一个类可能有用
importrequestsclassGitHubUser(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)ifresponse.status_code==200:self.__dict__.update(response.json())
加载包含该类的模块时,GitHubUser.url是 已求值,因此模板只创建一次。通常很难注意到 python,但是对象创建会耗费大量时间,而且 reuritemplate所依赖的模块。构造对象一次应该 减少代码运行所需的时间。
安装
pip install uritemplate.py
许可证
改良bsd license
更改日志-uritemplate
2.0.0-2016-08-29
- 将uritemplate.py合并到uritemplate中
changelog-uritemplate.py
2.0.0-2016年8月20日
- 将uritemplate.py重新授权为apache 2和bsd(请参见 https://github.com/sigmavirus24/uritemplate/pull/23)
1.0.1-2016年8月18日
- 解决一些小包装问题。
1.0.0-2016年8月17日
- 修复了在python 2.6和2.7上对urllib.quote的unicode值的处理。
- 通过版本号确认公共稳定API。
0.3.0-2013年10月22日
- 添加#partial以部分展开模板并返回 URITemplate。
0.2.0-2013年7月26日
- 对库进行一些重构并添加更多测试。
- 如果直接使用URIVariable,则向后与0.1.x不兼容 uritemplate.template
0.1.1-2013年5月19日
- 添加在当前uri中获取变量名集的功能
- 如果没有给定值或默认值,只需返回一个空字符串
- 修复sdist
0.1.0-2013年5月14日
- 初始版本