Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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_Python Decorators - Fatal编程技术网

Python编码技术:使用另一个方法作为接口进行系统调用的类方法

Python编码技术:使用另一个方法作为接口进行系统调用的类方法,python,python-decorators,Python,Python Decorators,考虑到我有一些方法都包装了一个特定的方法,并在其中添加了一些预/后处理,有没有一种方法可以更好地利用python编码技术来实现我的代码 希望利用python编码技术(认为python装饰程序可能有助于清理这一点)来实现下面的类 我有一个类,它有一个与外部世界接口的方法,类中的其他方法用于执行操作和对某些数据进行预/后处理 import subprocess as sp class MyClass(): def _system_interface(self, command):

考虑到我有一些方法都包装了一个特定的方法,并在其中添加了一些预/后处理,有没有一种方法可以更好地利用python编码技术来实现我的代码

希望利用python编码技术(认为python装饰程序可能有助于清理这一点)来实现下面的类

我有一个类,它有一个与外部世界接口的方法,类中的其他方法用于执行操作和对某些数据进行预/后处理

import subprocess as sp


class MyClass():

    def _system_interface(self, command):
        hello = ["echo", "'hello'"]
        start = ["echo", "'start'"]
        stop = ["echo", "'reset'"]
        reset = ["echo", "'reset'"]

        cmd = sp.Popen(locals()[command], stdout=sp.PIPE)
        output = cmd.stdout.readlines()
        print(output)
        return cmd.stdout.readlines()

    def call_one(self):
        # Do some processing
        self._system_interface("hello")

    def call_two(self):
        # Do some processing
        self._system_interface("start")

    def call_three(self):
        # Do some processing
        self._system_interface("stop")

    if __name__ == "__main__":
        c = MyClass()
        c.call_one()
        c.call_two()
        c.call_three()

您可以使用一个类,该类在实例化时接受一个命令,并且在调用时返回一个decorator函数,该函数使用从实例化期间给定的命令派生的命令调用
Popen

import subprocess as sp

class system_interface:
    def __init__(self, command):
        self.command = command

    def __call__(self, func):
        def wrapper(*args, **kwargs):
            func(*args, **kwargs)
            cmd = sp.Popen(['echo', self.command], stdout=sp.PIPE)
            output = cmd.stdout.readlines()
            print(output)
            return cmd.stdout.readlines()

        return wrapper

class MyClass():
    @system_interface('hello')
    def call_one(self):
        print('one: do some processing')

    @system_interface('start')
    def call_two(self):
        print('two: do some processing')

    @system_interface('stop')
    def call_three(self):
        print('three: do some processing')

if __name__ == "__main__":
    c = MyClass()
    c.call_one()
    c.call_two()
    c.call_three()
这将产生:

one: do some processing
[b'hello\n']
two: do some processing
[b'start\n']
three: do some processing
[b'stop\n']

问题是什么?这个问题更适合CodeReview.SE。但是请注意,您不应该像那样使用
locals()
;相反,将您的命令存储为dict。@Vlad考虑到我有一些方法都包装了一个特定的方法并添加了一个命令,但添加了一点预/后处理,有没有更好的方法利用python编码技术来实现我的代码?@DanielRoseman,谢谢。你能详细说明为什么不应该使用
locals()
?因为就像我说的,这正是dict的用途。当您可以在适当的数据结构中定义一个名称时,用额外的名称污染名称空间,然后使用hack查找它们是没有意义的。太棒了!我想这就是我要找的。似乎我必须对系统调用进行预处理或后处理。是否有一种方法可以同时完成这两项工作,或者可能只需要将其与另一个功能结合起来?很高兴能为您提供帮助。如果您需要同时执行预处理和后处理,那么您最初使用公共辅助方法的方法将是最好的,因为decorator的目的是围绕已修饰的函数,而不是相反。