forked from Mortal/complexity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metafunc.py
34 lines (27 loc) · 957 Bytes
/
metafunc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
#coding: utf-8
from meta.decompiler import decompile_func, compile_func
from meta import dump_python_source
import ast
class Mf:
def __init__(self, func):
self.ast = decompile_func(func)
if isinstance(self.ast, ast.Lambda):
self.ast = ast.FunctionDef(
name="f",
args=self.ast.args,
body=[ast.Return(value=self.ast.body, lineno=1, col_offset=0)],
lineno=self.ast.lineno,
col_offset=self.ast.col_offset,
decorator_list=[])
def dump(self):
return ast.dump(self.ast)
def __call__(self, *args, **kwargs):
self.ast = ast.fix_missing_locations(self.ast)
return compile_func(self.ast, __name__, globals())(*args, **kwargs)
def __hash__(self):
return hash(ast.dump(self.ast))
def __eq__(self,other):
return ast.dump(self.ast) == ast.dump(other.ast)
def __repr__(self):
return "#%s%s"%(hash(self),dump_python_source(self.ast))