Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return prefix + properties_str + suffix
default_format = TensorFormat()
class NumpyFormat:
def __call__(self, x):
return 'ndarray<{}, {}>'.format(x.shape, x.dtype.name)
default_numpy_format = NumpyFormat()
class TorchSnooper(pysnooper.tracer.Tracer):
def __init__(self, *args, tensor_format=default_format, numpy_format=default_numpy_format, **kwargs):
self.orig_custom_repr = kwargs['custom_repr'] if 'custom_repr' in kwargs else ()
custom_repr = (lambda x: True, self.compute_repr)
kwargs['custom_repr'] = (custom_repr,)
super(TorchSnooper, self).__init__(*args, **kwargs)
self.tensor_format = tensor_format
self.numpy_format = numpy_format
@staticmethod
def is_return_types(x):
return type(x).__module__ == 'torch.return_types'
def return_types_repr(self, x):
if type(x).__name__ in {'max', 'min', 'median', 'mode', 'sort', 'topk', 'kthvalue'}:
return type(x).__name__ + '(values=' + self.tensor_format(x.values) + ', indices=' + self.tensor_format(x.indices) + ')'
def decorate(function):
target_code_object = function.__code__
tracer = Tracer(
target_code_object=target_code_object, write=write,
truncate=truncate, watch=watch, watch_explode=watch_explode,
depth=depth, prefix=prefix, overwrite=overwrite
)
def inner(function_, *args, **kwargs):
with tracer:
return function(*args, **kwargs)
return decorator.decorate(function, inner)
2、将代码运行轨迹修改成可以点击的,点击对应行号可以跳转到代码位置。
3、提供一个猴子补丁,使用猴子补丁修改三方包的模块级全局变量MAX_VARIABLE_LENGTH ,最大变量默认是100,但对调试对接方json时候,往往很大,可以加大到最大显示10万个字母。
"""
import datetime
import os
from functools import wraps
import decorator
import pysnooper # 需要安装 pip install pysnooper==0.0.11
from pysnooper.pysnooper import get_write_function
from pysnooper.tracer import Tracer, get_local_reprs, get_source_from_frame
os_name = os.name
class TracerCanClick(Tracer):
"""
使代码运行轨迹可点击。
"""
def trace(self, frame, event, arg):
if frame.f_code is not self.target_code_object:
if self.depth == 1:
return self.trace
else:
_frame_candidate = frame
for i in range(1, self.depth):
_frame_candidate = _frame_candidate.f_back
if _frame_candidate is None:
return self.trace
elif _frame_candidate.f_code is self.target_code_object: