Home >Backend Development >Python Tutorial >How to extend Python's timers using context managers?

How to extend Python's timers using context managers?

WBOY
WBOYforward
2023-05-10 08:06:281030browse

A Python timer context manager

Python has a unique construct for calling functions before and after a block of code: Context Manager.

Understanding context managers in Python

Context managers have been an important part of Python for a long time. Introduced in 2005 by PEP 343 and first implemented in Python 2.5. You can identify context managers in your code using the with keyword:

with EXPRESSION as VARIABLE:
    BLOCK

EXPRESSION are Python expressions that return context managers. First the context manager is bound to the variable name VARIABLE, where 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 manager is used to release and clean up resources after using them. 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 Explicitly close, you can confirm that fp has automatically closed:

fp.closed

True

In 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.

fp What does it mean to be a 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:

  • Called when entering the context associated with the context manager .__enter__().

  • Called when exiting the context associated with the context manager .__exit__().

In other words, to create your own context manager, you need to write a class that implements .__enter__() and .__exit__() . Try Hello, World!Context manager example:

# 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("正在忙 ...")

Hello Yun Duojun
Busy...
See you soon, Yun Duojun

First of all, please note .__enter__() How it is called before doing things, and .__exit__() is called after doing things. In this example, there is no reference to the context manager, 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} 正在忙 ...")

Hello Yun Duojun
Yunduo Jun is busy...
See you soon, Yunduo Jun

When writing the __exit__ function, you need to pay attention to it. It must have these three parameters:

  • exc_type: exception type

  • exc_val:Exception value

  • exc_tb:Exception error stack information

These three parameters are used for error handling in the context manager, and they are returned as the return value of sys.exc_info(). When the main logic code does not report an exception, these three parameters will all be None.

If an exception occurs while executing the block, the code uses the exception type, exception instance, and traceback object (i.e., exc_type, exc_value, and exc_tb) Call .__exit__(). Normally, these are ignored in the context manager and .__exit__() is called before raising the exception:

from greeter import Greeter
with Greeter("云朵君") as grt:
    print(f"{grt.age} does not exist")

Hello Yun Duojun
See you soon , Yun Duojun
Traceback (most recent call last):
File "c2f32e7e9231c3bf5bf9f218b5147824", line 2, in 4225fa317875f3e92281a7b1a5733569
AttributeError: 'Greeter' object has no attribute 'age'

You can see that even if there is an error in the code, it still prints "See you later, Mr. Yun Duo".

理解并使用 contextlib

现在我们初步了解了上下文管理器是什么以及如何创建自己的上下文管理器。在上面的例子中,我们只是为了构建一个上下文管理器,却写了一个类。如果只是要实现一个简单的功能,写一个类未免有点过于繁杂。这时候,我们就想,如果只写一个函数就可以实现上下文管理器就好了。

这个点Python早就想到了。它给我们提供了一个装饰器,你只要按照它的代码协议来实现函数内容,就可以将这个函数对象变成一个上下文管理器。

我们按照 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 计时器上下文管理器

了解了上下文管理器的一般工作方式后,要想知道它们是如何帮助处理时序代码呢?假设如果可以在代码块之前和之后运行某些函数,那么就可以简化 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

此处注意两个更微妙的细节:

  • .__enter__() 返回 self,Timer 实例,它允许用户使用 as 将 Timer 实例绑定到变量。例如,使用 with Timer() as t: 将创建指向 Timer 对象的变量 t

  • .__exit__() 需要三个参数,其中包含有关上下文执行期间发生的任何异常的信息。代码中,这些参数被打包到一个名为 exc_info 的元组中,然后被忽略,此时 Timer 不会尝试任何异常处理。

在这种情况下不会处理任何异常。上下文管理器的一大特点是,无论上下文如何退出,都会确保调用.__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 "c2f32e7e9231c3bf5bf9f218b5147824", line 3, in 4225fa317875f3e92281a7b1a5733569
ZeroDivisionError: division by zero

注意 ,即使代码抛出异常,Timer 也会打印出经过的时间。

使用 Python 定时器上下文管理器

现在我们将一起学习如何使用 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()

The above is the detailed content of How to extend Python's timers using context managers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete