Subpy是一个库,用于定义Python语言的子集,并查询AST的语言级属性,这些属性被指定为一组功能。
许多项目旨在与Python的特定子集合作,这些项目适合静态分析和类型推理,Subpy只是一个静态分析库,用于检查子集的子集,目的是为最终用户提供更有信息的错误报告。
checker的输入可以是字符串的模块,函数代码或源代码。它返回以feature枚举代码键入的列表的词典以及带有检测到功能的行号的值。
> >> from subpy import checker
> >> import io
> >> print checker ( io )
{ 3 : [ 98 ],
5 : [ 78 , 81 , 84 , 87 ],
9 : [ 78 , 81 , 84 , 87 ],
10 : [ 81 , 84 , 87 ],
32 : [ 92 , 96 ],
34 : [ 79 ]}将功能代码与字典中的键匹配,我们看到了这一信息告诉我们,在标准库中的io模块中:
使用功能级检查器的示例:
from subpy import checker
from subpy . features import ListComp
def example1 ():
return [ x ** 2 for x in range ( 25 )]
def example2 ():
return 'hello'
features = checker ( example1 )
if ListComp in features :
print 'You used a list comprehension on lines %r' % ( features [ ListComp ])
features = checker ( example2 )
if ListComp not in features :
print 'You did not use any list comprehensions!' 例如,如果我们要排除列表综合的使用和设置综合的使用,我们可以定义一个排除这些功能的Python的子集。
MyPythonSubset = FullPython - { ListComp , SetComp }当在给定源中检测到不支持的功能时, validator命令可用于升级。例如,我们将支持Python功能集,不包括列表综合和设置综合。
from subpy import validator , FullPython , FeatureNotSupported
from subpy . features import ListComp , SetComp
def example ():
return [ x ** 2 for x in range ( 25 )]
my_features = FullPython - { ListComp , SetComp }
validator ( example , features = my_features ) File "<stdin>" , line 2
return [ x ** 2 for x in range ( 25 )]
^
subpy . validate . FeatureNotSupported : ListCompSubpy当前能够解析整个标准库,可用于查询一些有趣的琐事事实。
from subpy import detect
from subpy . stdlib import libraries
from subpy . features import Metaclasses , MInheritance , Exec
import importlib
print ( 'Libraries with Multiple Inheritance and Metaclasses:' )
for lib in libraries :
mod = importlib . import_module ( lib )
features = detect ( mod )
if Metaclasses in features and MInheritance in features :
print ( lib ) Libraries with Multiple Inheritance and Metaclasses:
io
或查询潜在的不安全代码执行:
print ( 'Libraries with Exec' )
for lib in libraries :
mod = importlib . import_module ( lib )
features = detect ( mod )
if Exec in features :
print ( lib ) Libraries with Exec
ihooks
site
cgi
rexec
Bastion
imputil
trace
timeit
cProfile
doctest
code
bdb
runpy
profile
collections
当前支持的功能是枚举,其值下面给出:
进行测试运行:
$ python -m unittest discover subpy/tests核心逻辑是在features.py中独立的validate.py除标准库外,没有其他依赖关系。
版权(C)2013,Continuum Analytics,Inc。保留所有权利。
如果满足以下条件:
源代码的再分配必须保留上述版权通知,此条件列表和以下免责声明。
二进制形式的重新分配必须复制上述版权通知,此条件列表以及文档和/或分发提供的其他材料中的以下免责声明。该软件由版权所有者和贡献者“按原样”提供,任何明示或暗示的保证,包括但不限于对适销性和特定目的适合性的隐含保证。在任何情况下,版权持有人或贡献者均不得对任何直接,间接,偶然,特殊,特殊,示例性或结果损害均承担任何责任(包括但不限于替代商品或服务的采购,损失,数据或利润损失,数据或利润;还是造成的,无论是在合同中造成的,或者在任何责任中造成的责任,或者是否有任何责任,或者在任何责任中,是否有任何责任,或者是否有任何责任,或者是否有责任,是否有责任,是否有责任。告知可能造成这种损害的可能性。