Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据配置指定要使用的接口子类_Python_Interface_Overriding - Fatal编程技术网

Python 如何根据配置指定要使用的接口子类

Python 如何根据配置指定要使用的接口子类,python,interface,overriding,Python,Interface,Overriding,我想创建一个“接口”类(在interface.py中),通过它我可以访问基础类的功能,其中所访问的类依赖于xml配置文件设置。 以ssh或ftp连接为例 我会在配置文件中设置一个变量,比如 “接口”:“ssh” 然后我希望有这样的代码: # file = interface.py class Interface(?) def connect(self, *args, **kwargs): return self # file = ssh.py class SSH(?)

我想创建一个“接口”类(在interface.py中),通过它我可以访问基础类的功能,其中所访问的类依赖于xml配置文件设置。 以ssh或ftp连接为例

我会在配置文件中设置一个变量,比如
“接口”:“ssh”

然后我希望有这样的代码:

# file = interface.py
class Interface(?)
    def connect(self, *args, **kwargs):
        return self

# file = ssh.py
class SSH(?)
    def connect(self, *args, **kwargs):
        # Setup a connection
        connection = paramiko.SSHClient()
        return connection

#file = ftp.py
class FTP(?)
    def connect(self, *args, **kwargs):
        # Setup a connection
        return connection

# And in my calling code I would just like a generic call e.g.
from path.interface import Interface

foo = Interface()
foo.connect(bar)    # Where "INTERFACE" : "ssh"
from abc import abstractmethod, ABCMeta

class Interface(metaclass=ABCMeta):
    @abstractmethod
    def upload(self):
        raise NotImplementedError('Must implement upload in subclasses')

class SSH(Interface):

    def upload(self):
        # ssh implementation

class FTP(Interface):

    def upload(self):
        # ftp implementation

def InterfaceForConfigurationValue(interface_value):
    if interface_value == 'ssh':
        return SSH()

    if interface_value == 'ftp':
        return FTP()

    raise NotImplementedError('Interface not available for value %s' % (
        interface_value,))
然后
SSH
类将(重写?)执行其
connect()
版本中定义的代码。 如果我随后将config设置更改为config.INTERFACE=“ftp”,相同的调用将“找到它的方式”到ftp类以建立ftp连接

理想情况下,我可以使用我的代码在不同版本的
connect()
之间切换,只需设置:

config.INTERFACE = "ssh"
config.INTERFACE = "ftp"
我想这不是什么不可能的事吧?我甚至不知道用谷歌搜索怎么做!这是压倒一切的吗? 任何建议都将不胜感激。
对谷歌来说,一个话题就是一个起点

这样做的标准模式如下:

# file = interface.py
class Interface(?)
    def connect(self, *args, **kwargs):
        return self

# file = ssh.py
class SSH(?)
    def connect(self, *args, **kwargs):
        # Setup a connection
        connection = paramiko.SSHClient()
        return connection

#file = ftp.py
class FTP(?)
    def connect(self, *args, **kwargs):
        # Setup a connection
        return connection

# And in my calling code I would just like a generic call e.g.
from path.interface import Interface

foo = Interface()
foo.connect(bar)    # Where "INTERFACE" : "ssh"
from abc import abstractmethod, ABCMeta

class Interface(metaclass=ABCMeta):
    @abstractmethod
    def upload(self):
        raise NotImplementedError('Must implement upload in subclasses')

class SSH(Interface):

    def upload(self):
        # ssh implementation

class FTP(Interface):

    def upload(self):
        # ftp implementation

def InterfaceForConfigurationValue(interface_value):
    if interface_value == 'ssh':
        return SSH()

    if interface_value == 'ftp':
        return FTP()

    raise NotImplementedError('Interface not available for value %s' % (
        interface_value,))
  • Interface
    是定义要使用的接口的抽象基类。它本身是无用的,需要具体的子类来实现它

    请注意,为了使其成为一个有用的抽象并值得付出努力,您希望使其更适合您的应用程序,并提供更高级别的API,例如
    upload()
    get()
    ,否则它就有点毫无意义,您可能会发现很难推广到不同的协议

  • SSH
    FTP
    都覆盖了它们的超类(
    Interface
    )的
    upload()
    方法,因此您可以在
    Interface
    实例上调用
    upload()
    ,而不必担心它是哪个特定的子类
  • InterfaceForConfigurationValue
    是一个出厂函数,它根据配置选项为您提供正确的接口子类
一般来说,最重要的是确保
接口
SSH
FTP
中的任何内容都不知道或关心配置文件中的内容,因为您可能希望在其他配置系统中使用它们。他们只知道他们需要做什么。您添加了一个工厂函数,该函数很小,包含如何将配置转换为子类的知识


注意:工厂函数不必是全局函数。通常,您会发现在配置系统和代码之间需要进行许多类似的链接,因此您可能希望使用一个类,并将工厂作为类上的一个方法。的子类通常是一个不错的选择。

ssh和ftp只是示例,而不是我试图编写的代码。这就是我想要学习的结构。重写还是继承?我想了解这里的实用程序。。。执行
config.INTERFACE=“ssh”
config=ssh()
之间有什么显著区别?这个接口似乎对您没有多大帮助。“接口”:“ssh”可能是XML配置文件中许多行中的一行。其内容将以config结尾。所以访问接口的值应该是config.INTERFACEOk。。。似乎您仍然应该使用
config.INTERFACE
的值来选择合适的类(
SSH
FTP
)。我的问题是如何做到这一点?很抱歉没有回到这里。我做这件事的首要任务是从悬崖上掉下来,然后慢慢地爬起来!我还没有机会尝试上述方法,但它看起来很有说服力。:)谢谢你抽出时间/帮助大家。