使用当前作用域中的名称格式化字符串。
项目描述
ScopeFormatter 允许Python的字符串格式化与当前作用域中的名称一起使用,类似于Ruby和Perl等语言中发现的变量插值。
示例
>>> 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!'
需求
堆栈检查需要提供 sys._getframe() 的Python VM,例如CPython。
限制
除非它们在局部作用域中引用,否则将找不到封装作用域中的非全局名称。
>>> 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'
历史
1.0.3 – 2009年10月22日
已将历史添加到项目页面。
添加了额外的文档文件。
1.0.2 – 2009年10月22日
重新组织了元数据。
删除了setuptools和nose的依赖项。
1.0.1 – 2009年10月3日
对元数据进行了小的补充。
1.0 – 2009年9月25日
初始发布。