[英]How do I suppress the output from log step while running jupyter notebook?
On this page, there is this log_step function which is being used to record what each step in a pandas pipeline is doing.在这个页面上,有这个 log_step function 用于记录 pandas 管道中的每个步骤正在做什么。 The exact function is:确切的 function 是:
from functools import wraps
import datetime as dt
def log_step(func):
@wraps(func)
def wrapper(*args, **kwargs):
tic = dt.datetime.now()
result = func(*args, **kwargs)
time_taken = str(dt.datetime.now() - tic)
print(f"just ran step {func.__name__} shape={result.shape} took {time_taken}s")
return result
return wrapper
and it is used in the following fashion:它以下列方式使用:
import pandas as pd
df = pd.read_csv('https://calmcode.io/datasets/bigmac.csv')
@log_step
def start_pipeline(dataf):
return dataf.copy()
@log_step
def set_dtypes(dataf):
return (dataf
.assign(date=lambda d: pd.to_datetime(d['date']))
.sort_values(['currency_code', 'date']))
My question is: how do I keep the @log_step in front of my functions and be able to use them at will, while setting the results of @log_step by default, to not be outputed when I run my Jupyter notebook?我的问题是:如何将@log_step 保留在我的函数前面并能够随意使用它们,同时将@log_step 的结果默认设置为在我运行Jupyter notebook 时不输出? I suspect the answer comes down to something more general about using decorators but I don't really know what to look for.我怀疑答案归结为关于使用装饰器的更一般的东西,但我真的不知道要寻找什么。 Thanks!谢谢!
You can indeed remove the print
statement or, if you want to not alter the decorating function, you can as well redirect the sys
to avoid seeing the prints, as explained here .您确实可以删除print
语句,或者,如果您不想更改装饰 function,您也可以重定向sys
以避免看到打印,如此处所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.