欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?pluggy框架使用示例代碼

 更新時(shí)間:2022年05月13日 12:05:07   作者:授客  
這篇文章主要介紹了Python?pluggy框架基礎(chǔ)用法總結(jié),本文通過三個(gè)例子結(jié)合代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

代碼為例進(jìn)行說明

實(shí)踐環(huán)境

Python 3.6.5

pluggy 0.13.0

例1 注冊(cè)類函數(shù)為插件函數(shù)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pluggy
hookspec = pluggy.HookspecMarker("myproject")  # hook 標(biāo)簽 用于標(biāo)記hook
hookimpl = pluggy.HookimplMarker("myproject")  # hook 實(shí)現(xiàn)標(biāo)簽 用于標(biāo)記hook的一個(gè)或多個(gè)實(shí)現(xiàn)
class MySpec(object):
    """hook 集合"""
    @hookspec
    def myhook(self, arg1, arg2):
        pass
    @hookspec
    def my_hook_func1(self, arg1, arg2):
        pass
    @hookspec
    def my_hook_func2(self, arg1, arg2):
        pass
# 插件類
class Plugin_1(object):
    """hook實(shí)現(xiàn)類1"""
    @hookimpl
    def myhook(self, arg1, arg2):
        print("Plugin_1.myhook called")
        return arg1 + arg2
    @hookimpl
    def my_hook_func2(self, arg1, arg2):
        print("Plugin_1.my_hook_func2 called, args:", arg1, arg2)
    def my_hook_func3(self, arg1, arg2):
        print("Plugin_1.my_hook_func3 called, args:", arg1, arg2)
class Plugin_2(object):
    """hook實(shí)現(xiàn)類2"""
    @hookimpl
    def myhook(self, arg1, arg2):
        print("Plugin_2.myhook called")
        return arg1 - arg2
    @hookimpl
    def my_hook_func2(self, arg1, arg2):
        print("Plugin_2.my_hook_func2, args:", arg1, arg2)
# 初始化 PluginManager
pm = pluggy.PluginManager("myproject")
# 登記hook集合(hook函數(shù)聲明)
pm.add_hookspecs(MySpec)
# 注冊(cè)插件(hook函數(shù)實(shí)現(xiàn))
pm.register(Plugin_1())
pm.register(Plugin_2())
# 調(diào)用自定義hook
results = pm.hook.myhook(arg1=1, arg2=2) # 調(diào)用兩個(gè)插件類中的同名hook函數(shù) # 后注冊(cè)的插件中的函數(shù)會(huì)先被調(diào)用
print(results) # 輸出 [-1, 3]
results = pm.hook.my_hook_func1(arg1="name", arg2="shouke")
print(results)
pm.hook.my_hook_func2(arg1="addr", arg2="sz")

運(yùn)行結(jié)果

Plugin_2.myhook called
Plugin_1.myhook called
[-1, 3]
[]
Plugin_2.my_hook_func2, args: addr sz
Plugin_1.my_hook_func2 called, args: addr sz

例2 注冊(cè)模塊函數(shù)為插件函數(shù)

myhookspec.py, myhookimpl.py, other.py, example.py位于同一包目錄下

myhookspec.py

import pluggy
hookspec = pluggy.HookspecMarker("myproject")  # hook 標(biāo)簽 用于標(biāo)記hook
hookimpl = pluggy.HookimplMarker("myproject")  # hook 實(shí)現(xiàn)標(biāo)簽 用于標(biāo)記hook的一個(gè)或多個(gè)實(shí)現(xiàn)
@hookspec
def global_hook_func1(arg1, arg2):
    pass

myhookimpl.py

import pluggy
from myhookspec import hookimpl
@hookimpl
def global_hook_func1(arg1, arg2):
    print("global_hook_func1 in myhookimpl.py, args:", arg1, arg2)
    return "myhookimpl.py"

other.py

from myhookspec import hookimpl
@hookimpl
def global_hook_func1(arg1, arg2):
    print("global_hook_func1 in other.py, args:", arg1, arg2)
    return "other.py"

example.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import pluggy
import myhookspec
import myhookimpl
import other
# 初始化 PluginManager
pm = pluggy.PluginManager("myproject")
# 登記hook集合
pm.add_hookspecs(myhookspec)
# 登記hook的實(shí)現(xiàn)
pm.register(myhookimpl) # 插件也可以是模塊
pm.register(other)
print(pm.hook.global_hook_func1(arg1="name", arg2="shouke"))

example.py運(yùn)行結(jié)果如下

global_hook_func1 in other.py, args: name shouke
global_hook_func1 in myhookimpl.py, args: name shouke
['other.py', 'myhookimpl.py']

例3:自定義插件類實(shí)現(xiàn)hook函數(shù)免@hookimpl裝飾器

myhookspec.py

import pluggy
hookspec = pluggy.HookspecMarker("myproject")
@hookspec
def mytest_hook_func1(arg1, arg2):
    pass

other.py

def mytest_hook_func1(arg1, arg2):
    print("global_hook_func1 in other.py, args:", arg1, arg2)
    return "other.py"

example.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import inspect
import pluggy
import myhookspec
import other
class PytestPluginManager(pluggy.PluginManager):
    """
    插件類,實(shí)現(xiàn)不用@HookimplMarkerInstance裝飾的函數(shù)也可以當(dāng)做函數(shù)體
    """
    def parse_hookimpl_opts(self, plugin, name):
        # 規(guī)定免@hookimpl裝飾的 hooks 函數(shù)總是以 mytest_打頭,這樣以避免訪問非可讀屬性
        if not name.startswith("mytest_"):
            return
        method = getattr(plugin, name)
        opts = super().parse_hookimpl_opts(plugin, name)
        # 考慮hook只能為函數(shù)(consider only actual functions for hooks)
        if not inspect.isroutine(method):
            return
        # 收集未被標(biāo)記的,以mytest打頭的hook函數(shù),(collect unmarked hooks as long as they have the `pytest_' prefix)
        if opts is None and name.startswith("mytest_"):
            opts = {}
        return opts
# 初始化 PluginManager
pm = PytestPluginManager("myproject")
# 登記hook集合
pm.add_hookspecs(myhookspec)
# 登記hook的實(shí)現(xiàn)
pm.register(other)
pm.hook.mytest_hook_func1(arg1="addr", arg2="sz")

參考連接

https://pypi.org/project/pluggy/

到此這篇關(guān)于Python pluggy框架基礎(chǔ)用法總結(jié)的文章就介紹到這了,更多相關(guān)Python pluggy用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python?import模塊時(shí)有錯(cuò)誤紅線的原因

    python?import模塊時(shí)有錯(cuò)誤紅線的原因

    這篇文章主要介紹了python?import模塊時(shí)有錯(cuò)誤紅線的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼

    python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼

    今天翻電腦時(shí)突然發(fā)現(xiàn)有個(gè)存了很多照片和視頻的文件夾,想起來是去年換手機(jī)(流行的小5)時(shí)拷出來的??戳藥讖堈掌掠忠荒荒坏母‖F(xiàn)在腦海,好吧,我是個(gè)感性的人
    2012-08-08
  • Python竟然能剪輯視頻

    Python竟然能剪輯視頻

    平時(shí)我們?cè)谒⒍桃曨l或者看到一些視頻的時(shí)候,希望可以把視頻里面的音頻提取出來當(dāng)背景音樂究竟是怎么操作的呢,下面小編教大家python如何去提取一個(gè)視頻里面的音頻,感興趣的朋友一起看看吧
    2021-05-05
  • python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用

    python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用

    本文主要介紹了python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條

    WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條

    這篇文章主要介紹了WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python實(shí)現(xiàn)檢測(cè)文件的MD5值來查找重復(fù)文件案例

    Python實(shí)現(xiàn)檢測(cè)文件的MD5值來查找重復(fù)文件案例

    這篇文章主要介紹了Python實(shí)現(xiàn)檢測(cè)文件的MD5值來查找重復(fù)文件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 詳解如何比較Python中的兩個(gè)迭代器

    詳解如何比較Python中的兩個(gè)迭代器

    Python迭代器是高效遍歷元素序列的強(qiáng)大工具,有時(shí)可能需要比較兩個(gè)迭代器以確定它們的相等性或找到它們的差異,本文將比較Python中兩個(gè)迭代器的不同方法,
    2024-11-11
  • 解決ImportError:DLL load failed while importing win32api:找不到指定的模塊

    解決ImportError:DLL load failed while impo

    在安裝pywin32后,可能會(huì)出現(xiàn)無法導(dǎo)入win32api的錯(cuò)誤,一個(gè)有效的解決方案是運(yùn)行pywin32_postinstall.py腳本,首先,打開cmd并切換到環(huán)境的Scripts文件夾,確保存在pywin32_postinstall.py文件
    2024-09-09
  • Ubuntu手動(dòng)編譯源碼安裝Python的詳細(xì)過程

    Ubuntu手動(dòng)編譯源碼安裝Python的詳細(xì)過程

    這篇文章主要介紹了Ubuntu手動(dòng)編譯源碼安裝Python的詳細(xì)過程,在python官網(wǎng)找到所需版本的python安裝包,下載到Ubuntu系統(tǒng)中,需要的朋友可以參考下
    2006-08-08
  • Python中單元測(cè)試的快速入門指南

    Python中單元測(cè)試的快速入門指南

    在這篇文章中,我們會(huì)深入探討Python單元測(cè)試的各個(gè)方面,包括它的基本概念、基礎(chǔ)知識(shí)、實(shí)踐方法、高級(jí)話題,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-07-07

最新評(píng)論