与语句相关的黑客的构建块
withhacks的Python项目详细描述
WithHacks:与语句相关的Hackry的构建块
这个模块是一个有用的构建块的集合,用于破解python “with”语句。它结合了几个简洁的想法和语句技巧 我在网上找到了一套可重复使用的组件:
通过从这个模块子类化适当的上下文管理器,您可以 轻松完成以下操作:
- skip execution of the code inside the with-statement
- set local variables in the frame executing the with-statement
- capture the bytecode from inside the with-statement
- capture local variables defined inside the with-statement
在这些基本工具的基础上,本模块还提供了一些有用的预构建 黑客:
xargs: call a function with additional arguments defined in the body of the with-statement xkwargs: call a function with additional keyword arguments defined in the body of the with-statement namespace: direct all variable accesses and assignments to the attributes of a given object (like “with” in JavaScript or VB) keyspace: direct all variable accesses and assignments to the keys of of a given object (like namespace() but for dicts)
WithHacks广泛使用了Noam Raphael的神奇“Byteplay”模块; 因为官方的byteplay发行版不支持Python2.6,所以 此模块中包含带有适当修补程序的版本。