Created
December 27, 2015 12:22
-
-
Save Bogdanp/47711e30301f9134f231 to your computer and use it in GitHub Desktop.
lol Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
import inspect | |
class NameVisitor(ast.NodeVisitor): | |
def __init__(self, default_value=None): | |
self.default_value = default_value | |
def visit_Name(self, node): | |
if isinstance(node.ctx, ast.Load): | |
if node.id not in globals(): | |
globals()[node.id] = self.default_value | |
class PHPTon(object): | |
def show(self, x): | |
print "VALUE: {!r}".format(x) | |
def __getattribute__(self, name): | |
frame = inspect.stack()[1] | |
try: | |
call = ast.parse(frame[-2][0]) | |
visitor = NameVisitor() | |
visitor.visit(call) | |
finally: | |
del frame | |
return super(PHPTon, self).__getattribute__(name) | |
p = PHPTon() | |
p.show(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment