How to extend Python timers with context managers

王林
Release: 2023-04-12 20:43:06
forward
1653 people have browsed it

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:

  • First, instantiate the class
  • Second, call .start()# before the code block to be timed
  • ##Finally, call .stop() after the code block

How to extend Python timers with context managers

A Python timer context manager

Python has a Unique construct for calling functions before and after blocks of code: context managers.

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
Copy after login

EXPRESSION 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:
Copy after login

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
Copy after login
True
Copy after login

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 .

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:

    Called .__enter__() when entering the context associated with the context manager.
  1. Called .__exit__() when exiting the context associated with the context manager.
In other words, to create your own context manager, you need to write a class that implements .__enter__() and .__exit__() . Try the 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}")
Copy after login

Studio is a context manager that implements the context manager protocol and is used as follows:

from studio import Studio
with Studio("云朵君"):
print("正在忙 ...")
Copy after login
你好 云朵君
正在忙 ...
一会儿见, 云朵君
Copy after login

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} 正在忙 ...")
Copy after login
你好 云朵君
云朵君 正在忙 ...
一会儿见, 云朵君
Copy after login

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 start with sys.exc_info () returns the return value. 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 will call .__exit__() with the exception type, exception instance, and traceback object (i.e. exc_type, exc_value, and exc_tb). 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")
Copy after login
你好 云朵君
一会儿见, 云朵君
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: 'Greeter' object has no attribute 'age'
Copy after login

As you can see, even if there is an error in the code, "See you later," is still printed. Mr. Yunduo".

Understanding and using contextlib

Now 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)
Copy after login

在被装饰函数里,必须是一个生成器(带有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)
Copy after login

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()
Copy after login

Timer 现在就是一个上下文管理器。实现的重要部分是在进入上下文时, .__enter__() 调用 .start() 启动 Python 计时器,而在代码离开上下文时, .__exit__() 使用 .stop() 停止 Python 计时器。

from timer import Timer
import time
with Timer():
time.sleep(0.7)
Copy after login
Elapsed time: 0.7012 seconds
Copy after login

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

  • .__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}")
Copy after login
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
Copy after login

注意 ,即使代码抛出异常,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()
Copy after login

我们正在对 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()
Copy after login

此代码实际上与上面的代码相同。主要区别在于没有定义无关变量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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!