使用当前作用域中的名称格式化字符串。
ScopeFormatter的Python项目详细描述
scopeformatter允许对名称使用python的字符串格式 从当前范围绘制,类似于变量插值 在ruby和perl等语言中发现。
内容
Examples
>>> from scopeformatter import F >>> greeting = 'Hello' >>> def greet(name): ... return F('{greeting}, {name}!') >>> greet('world') 'Hello, world!'
接受位置参数和关键字参数:
>>> F('{greeting} {0} times, {name}!', len(greeting), name='world') 'Hello 5 times, world!'
Requirements
堆栈检查需要一个python vm,它提供 sys._getframe(),例如cpython。
Limitations
将找不到封闭作用域中的非全局名称,除非 它们在本地范围内被引用。
>>> def outer(): ... non_local = 'non-local' ... def inner(): ... return F('{non_local} is not referenced locally') ... return inner() >>> outer() Traceback (most recent call last): ... KeyError: 'non_local'
>>> def outer(): ... non_local = 'non-local' ... def inner(): ... non_local ... return F('{non_local} is referenced locally') ... return inner() >>> outer() 'non-local is referenced locally'
History
1.0.3 – 2009 Oct 22
- 将历史记录添加到项目页。
- 添加了其他文档文件。
1.0.2 – 2009 Oct 22
- 重新组织元数据。
- 删除了对setuptools和nose的依赖关系。
1.0.1 – 2009 Oct 3
- 对元数据进行了少量添加。
1.0 – 2009 Sept 25
- 初次发布。