专栏名称: 机器学习算法与Python学习
作为沟通学习的平台,发布机器学习与数据挖掘、深度学习、Python实战的前沿与动态,欢迎机器学习爱好者的加入,希望帮助你在AI领域更好的发展,期待与你相遇!
目录
相关文章推荐
中核集团  ·  “钇”举成功!我国成功实现商用堆生产钇-90 ·  9 小时前  
中核集团  ·  主播说中核 | 科技圈“顶流局”来了! ·  9 小时前  
中核集团  ·  走进“龙之谷”,直击核仪控智造跃迁! ·  21 小时前  
中核集团  ·  安全生产月 | ... ·  21 小时前  
中核集团  ·  数智强核 ... ·  昨天  
51好读  ›  专栏  ›  机器学习算法与Python学习

揭秘Pluggy,快速实现插件化系统!

机器学习算法与Python学习  · 公众号  ·  · 2024-11-07 10:05

正文

请到「今天看啥」查看全文


]
return spam


@eggsample.hookimpl
def eggsample_prep_condiments(condiments):
"""Here the caller passes a mutable object, so we mess with it directly."""
try:
del condiments[ "steak sauce" ]
except KeyError:
pass
condiments[ "spam sauce" ] = 42
return "Now this is what I call a condiments tray!"

并在打包信息中标注相同的命名空间:

from setuptools import setup

setup(
    name="eggsample-spam",
    install_requires="eggsample",
    entry_points={"eggsample": ["spam = eggsample_spam"]},
    py_modules=["eggsample_spam"],
)

其原理也是通过 Python 的 from importlib.metadata import entry_points 找到注册到 Python 解释器 entry_points 中的包,并 根据命名空间获取需要的内容。

stevedore

stevedore 是 Openstack 开发和维护的一个插件工具。该 插件为 Openstack 的 ceilometer 提供插件功能。

stevedore 则是推荐使用继承的方式规范插件接口。

首先创建一个插件基类:

# stevedore/example/base.py
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import abc


class FormatterBase(metaclass=abc.ABCMeta):
    """Base class for example plugin used in the tutorial.
    """


    def __init__(self, max_width=60):
        self.max_width = max_width

    @abc.abstractmethod
    def format(self, data):
        """Format the data and return unicode text.

        :param data: A dictionary with string keys and simple types as
                     values.
        :type data: dict(str:?)
        :returns: Iterable producing the formatted text.
        """

然后实现一个简单的插件:

# stevedore/example/simple.py
# Copyright (C) 2020 Red Hat, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.
from stevedore.example import base


class Simple(base.FormatterBase):
    """A very basic formatter."""

    def format(self, data):
        """Format the data and return unicode text.

        :param data: A dictionary with string keys and simple types as
                     values.
        :type data: dict(str:?)
        """

        for name, value in sorted(data.items()):
            line = '{name} = {value}\n'.format(
                name=name,
                value=value,
            )
            yield line

最后打包。打包的时候,将插件注册到 entry_points 中。

# stevedore/example/setup.py
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or






请到「今天看啥」查看全文