Dưới đây là một trang trí sẽ xem tất cả các chức năng của bạn, sử dụng colorama, để có được một đầu ra tốt đẹp.
thử: nhập khẩu coloramac trừ ImportError: lớp stdClass: pass def passer (* args, ** kwargs): pass colorama = stdClass() colorama.init = passer colorama.Fore = stdClass () colorama.Fore.RED = colorama.Fore.GREEN = ''
def check_for_use(show=False):
if show:
try:
check_for_use.functions
except AttributeError:
return
no_error = True
for function in check_for_use.functions.keys():
if check_for_use.functions[function][0] is False:
print(colorama.Fore.RED + 'The function {!r} hasn\'t been called. Defined in "{}" '.format(function, check_for_use.functions[function][1].__code__.co_filename))
no_error = False
if no_error:
print(colorama.Fore.GREEN + 'Great! All your checked function are being called!')
return check_for_use.functions
try:
check_for_use.functions
except AttributeError:
check_for_use.functions = {}
if colorama:
colorama.init(autoreset=True)
def add(function):
check_for_use.functions[function.__name__] = [False, function]
def func(*args, **kwargs):
check_for_use.functions[function.__name__] = [True, function]
function(*args, **kwargs)
return func
return add
@check_for_use()
def hello():
print('Hello world!')
@check_for_use()
def bonjour(nb):
print('Bonjour tout le monde!')
# hello(); bonjour(0)
hello()
check_for_use(True) # outputs the following
Output:
Hello world!
The function 'bonjour' hasn't been called. Defined in "path_to_file.py"
tôi đã viết một [đếm trang trí] (http: //code.activestate.com/recipes/577534-counting-decorator/?in=user-4173873) khi áp dụng sẽ cho bạn biết số lần một hàm được gọi. Bạn có thể điều chỉnh điều này theo nhu cầu của bạn nếu bạn muốn. –
Bạn hy vọng sẽ làm gì với thông tin này? –