❤️Practical application examples of Python decorators⚡

This chapter combines the previous decorator articles to do an example operation, brainstorm, and deepen the understanding of decorators.

Example 1 - Automation - Open Browser Action

# -*- coding: utf-8 -*-
# https://blog.csdn.net/weixin_52040868
# 公众号:测个der
# 微信:qing_an_an

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep

s = Service('F://geckodriver.exe')

driver = webdriver.Firefox(service=s)
driver.get('https://blog.csdn.net/weixin_52040868?spm=1000.2115.3001.5343')
sleep(1)
driver.quit()

decorator version

def driver(fun):
    def func(args):
        driver = webdriver.Firefox(service=args)
        return fun(driver)
    return func

@driver
def run(driver):    # run = driver(run)
    driver.get('https://blog.csdn.net/weixin_52040868')
    sleep(1)
    driver.quit()

if __name__ == '__main__':
    s = Service('F://geckodriver.exe')
    run(s)

Example 2 - Make buns and eat buns

Supongo que te gusta

Origin blog.csdn.net/weixin_52040868/article/details/129388613
Recomendado
Clasificación