Home > Article > Backend Development > How to extend Python timers with context managers
In the above we created the first Python timer class, and then gradually expanded our Timer class, and its code is also relatively rich and powerful. We can't be satisfied with this, we still need to template some code to use the Timer:
with EXPRESSION as VARIABLE: BLOCKEXPRESSION are Python expressions that return context managers. First the context manager is bound to the variable name VARIABLE, BLOCK can be any regular Python code block. The context manager ensures that the program calls some code before BLOCK and some other code after BLOCK is executed. In this way, even if BLOCK throws an exception, the latter will still be executed. The most common use of context managers is to handle different resources, such as files, locks, and database connections. Context managers are used to release and clean up resources after they have been used. The following example demonstrates the basic structure of timer.py by printing only the lines containing colons. Additionally, it shows the common idiom for opening files in Python:
with open("timer.py") as fp: print("".join(ln for ln in fp if ":" in ln)) class TimerError(Exception): class Timer: timers: ClassVar[Dict[str, float]] = {} name: Optional[str] = None text: str = "Elapsed time: {:0.4f} seconds" logger: Optional[Callable[[str], None]] = print _start_time: Optional[float] = field(default=None, init=False, repr=False) def __post_init__(self) -> None: if self.name is not None: def start(self) -> None: if self._start_time is not None: def stop(self) -> float: if self._start_time is None: if self.logger: if self.name:Note that using open() as the context manager, the file pointer fp will not be closed explicitly, you can confirm that fp has been closed automatically:
fp.closed
TrueIn this example, open("timer.py") is an expression that returns the context manager. This context manager is bound to the name fp. The context manager is valid during print() execution. This single-line block of code is executed in the context of fp . What does fp mean as context manager? Technically speaking, fp implements the context manager protocol. There are many different protocols underlying the Python language. Think of a protocol as a contract that states what specific methods our code must implement. The context manager protocol consists of two methods:
# studio.py class Studio: def __init__(self, name): self.name = name def __enter__(self): print(f"你好 {self.name}") return self def __exit__(self, exc_type, exc_value, exc_tb): print(f"一会儿见, {self.name}")Studio is a context manager that implements the context manager protocol and is used as follows:
from studio import Studio with Studio("云朵君"): print("正在忙 ...")
你好 云朵君 正在忙 ... 一会儿见, 云朵君First, pay attention to .__enter__() How it is called before doing something, and .__exit__() is called after doing something. In this example, the context manager is not referenced, so there is no need to use as to name the context manager. Next, note that the return value of self.__enter__() is subject to as constraints. When creating a context manager, you usually want to return self from .__enter__() . The return value can be used as follows:
from greeter import Greeter with Greeter("云朵君") as grt: print(f"{grt.name} 正在忙 ...")
你好 云朵君 云朵君 正在忙 ... 一会儿见, 云朵君When writing the __exit__ function, you need to pay attention to it. It must have these three parameters:
from greeter import Greeter with Greeter("云朵君") as grt: print(f"{grt.age} does not exist")
你好 云朵君 一会儿见, 云朵君 Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: 'Greeter' object has no attribute 'age'As you can see, even if there is an error in the code, "See you later," is still printed. Mr. Yunduo". Understanding and using contextlibNow we have a preliminary understanding of what a context manager is and how to create your own. In the above example, we wrote a class just to build a context manager. If you just want to implement a simple function, writing a class is a bit too complicated. At this time, we thought, it would be great if we could implement the context manager by writing only one function. Python has already thought of this. It provides us with a decorator. You can turn this function object into a context manager as long as you implement the function content according to its code protocol.
我们按照 contextlib 的协议来自己实现一个上下文管理器,为了更加直观我们换个用例,创建一个我们常用且熟悉的打开文件(with open)的上下文管理器。
import contextlib @contextlib.contextmanager def open_func(file_name): # __enter__方法 print('open file:', file_name, 'in __enter__') file_handler = open(file_name, 'r') # 【重点】:yield yield file_handler # __exit__方法 print('close file:', file_name, 'in __exit__') file_handler.close() return with open_func('test.txt') as file_in: for line in file_in: print(line)
在被装饰函数里,必须是一个生成器(带有yield),而 yield 之前的代码,就相当于__enter__里的内容。yield 之后的代码,就相当于__exit__ 里的内容。
上面这段代码只能实现上下文管理器的第一个目的(管理资源),并不能实现第二个目的(处理异常)。
如果要处理异常,可以改成下面这个样子。
import contextlib @contextlib.contextmanager def open_func(file_name): # __enter__方法 print('open file:', file_name, 'in __enter__') file_handler = open(file_name, 'r') try: yield file_handler except Exception as exc: # deal with exception print('the exception was thrown') finally: print('close file:', file_name, 'in __exit__') file_handler.close() return with open_func('test.txt') as file_in: for line in file_in: 1/0 print(line)
Python 标准库中的 contextlib包括定义新上下文管理器的便捷方法,以及可用于关闭对象、抑制错误甚至什么都不做的现成上下文管理器!
了解了上下文管理器的一般工作方式后,要想知道它们是如何帮助处理时序代码呢?假设如果可以在代码块之前和之后运行某些函数,那么就可以简化 Python 计时器的工作方式。其实,上下文管理器可以自动为计时时显式调用 .start() 和.stop()。
同样,要让 Timer 作为上下文管理器工作,它需要遵守上下文管理器协议,换句话说,它必须实现 .__enter__() 和 .__exit__() 方法来启动和停止 Python 计时器。从目前的代码中可以看出,所有必要的功能其实都已经可用,因此只需将以下方法添加到之前编写的的 Timer 类中即可:
# timer.py @dataclass class Timer: # 其他代码保持不变 def __enter__(self): """Start a new timer as a context manager""" self.start() return self def __exit__(self, *exc_info): """Stop the context manager timer""" self.stop()
Timer 现在就是一个上下文管理器。实现的重要部分是在进入上下文时, .__enter__() 调用 .start() 启动 Python 计时器,而在代码离开上下文时, .__exit__() 使用 .stop() 停止 Python 计时器。
from timer import Timer import time with Timer(): time.sleep(0.7)
Elapsed time: 0.7012 seconds
此处注意两个更微妙的细节:
在这种情况下不会处理任何异常。上下文管理器的一大特点是,无论上下文如何退出,都会确保调用.__exit__()。在以下示例中,创建除零公式模拟异常查看代码功能:
from timer import Timer with Timer(): for num in range(-3, 3): print(f"1 / {num} = {1 / num:.3f}")
1 / -3 = -0.333 1 / -2 = -0.500 1 / -1 = -1.000 Elapsed time: 0.0001 seconds Traceback (most recent call last): File "<stdin>", line 3, in <module> ZeroDivisionError: division by zero
注意 ,即使代码抛出异常,Timer 也会打印出经过的时间。
现在我们将一起学习如何使用 Timer 上下文管理器来计时 "下载数据" 程序。回想一下之前是如何使用 Timer 的:
# download_data.py import requests from timer import Timer def main(): t = Timer() t.start() source_url = 'https://cloud.tsinghua.edu.cn/d/e1ccfff39ad541908bae/files/?p=%2Fall_six_datasets.zip&dl=1' headers = {'User-Agent': 'Mozilla/5.0'} res = requests.get(source_url, headers=headers) t.stop() with open('dataset/datasets.zip', 'wb') as f: f.write(res.content) if __name__ == "__main__": main()
我们正在对 requests.get() 的调用进行记时监控。使用上下文管理器可以使代码更短、更简单、更易读:
# download_data.py import requests from timer import Timer def main(): source_url = 'https://cloud.tsinghua.edu.cn/d/e1ccfff39ad541908bae/files/?p=%2Fall_six_datasets.zip&dl=1' headers = {'User-Agent': 'Mozilla/5.0'} with Timer(): res = requests.get(source_url, headers=headers) with open('dataset/datasets.zip', 'wb') as f: f.write(res.content) if __name__ == "__main__": main()
此代码实际上与上面的代码相同。主要区别在于没有定义无关变量t,在命名空间上无多余的东西。
将上下文管理器功能添加到 Python 计时器类有几个优点:
使用 Timer 作为上下文管理器几乎与直接使用 .start() 和 .stop() 一样灵活,同时它的样板代码更少。在该系列下一篇文章中,云朵君将和大家一起学习如何将 Timer 也用作装饰器,并用于代码中,从而更加容易地监控代码完整运行过程,我们一起期待吧!
The above is the detailed content of How to extend Python timers with context managers. For more information, please follow other related articles on the PHP Chinese website!