以下を参照する関数デバッグ デコレータを作成しようとしています。
def foo(baz):
bar = 1
bar = 2
return bar
そしてそれを次のようにラップします:
def foo(baz):
bar = 1
print 'bar: {}'.format(bar)
bar = 2
print 'bar: {}'.format(bar)
return bar
「\w+(?=\s*[=])」を取得するために、関数をテキストとして操作する必要がありますが、それにアクセスする方法がわかりません。動作するブログから変更したデコレータがありますが、次のように変更してみました:
class decorator_string_check(object):
def __init__(self, func):
self.func = func
wraps(func)(self)
def __call__(self, *args, **kwargs):
print dir(self.func)
print dir(self.func.__code__)
print self.func.__code__.__str__()
ret = self.func(*args, **kwargs)
return ret
@decorator_string_check
def fake(x):
y = 6
y = x
return y
y = fake(9)
つまり、価値のあるものは何も得ていません。
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
<code object fake at 0x7f98b8b1d030, file "./logging.py", line 48>
実際の "func" テキストを操作して正規表現を実行し、デコレータ クラス オブジェクト内で必要なものを見つけるにはどうすればよいですか? ありがとうございました