创建自定义 sys.stdout 类?
我想做的只是将一些终端命令的输出打印到 wx.TextCtrl 小部件。我认为完成此操作的最简单方法是创建一个自定义标准输出类并将写入函数重载到小部件的写入函数。
stdout 类:
class StdOut(sys.stdout):
def __init__(self,txtctrl):
sys.stdout.__init__(self)
self.txtctrl = txtctrl
def write(self,string):
self.txtctrl.write(string)
然后我会做一些事情,例如:
sys.stdout = StdOut(createdTxtCtrl)
subprocess.Popen('echo "Hello World!"',stdout=sys.stdout,shell=True)
结果是以下错误:
Traceback (most recent call last):
File "mainwindow.py", line 12, in <module>
from systemconsole import SystemConsole
File "systemconsole.py", line 4, in <module>
class StdOut(sys.stdout):
TypeError: Error when calling the metaclass bases
file() argument 2 must be string, not tuple
任何修复此问题的想法将不胜感激。
What I'm trying to do is simply have the output of some terminal commands print out to a wx.TextCtrl widget. I figured the easiest way to accomplish this is to create a custom stdout class and overload the write function to that of the widget.
stdout class:
class StdOut(sys.stdout):
def __init__(self,txtctrl):
sys.stdout.__init__(self)
self.txtctrl = txtctrl
def write(self,string):
self.txtctrl.write(string)
And then I would do something such as:
sys.stdout = StdOut(createdTxtCtrl)
subprocess.Popen('echo "Hello World!"',stdout=sys.stdout,shell=True)
What results is the following error:
Traceback (most recent call last):
File "mainwindow.py", line 12, in <module>
from systemconsole import SystemConsole
File "systemconsole.py", line 4, in <module>
class StdOut(sys.stdout):
TypeError: Error when calling the metaclass bases
file() argument 2 must be string, not tuple
Any ideas to fix this would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要实现的只是编写,则根本不需要定义新类。只需使用
createdTxtCtrl
而不是StdOut(createdTxtCtrl)
,因为前者已经支持您需要的操作。如果您需要对 stdout 做的只是将某些程序的输出定向到那里,而不是直接将所有类型的内容定向到那里,则不要更改
sys.stdout
,只需实例化subprocess.Popen
code> 使用您自己的类文件对象 (createdTxtCtrl
) 而不是sys.stdout
。If all you need to implement is writing, there is no need to define a new class at all. Simply use
createdTxtCtrl
instead ofStdOut(createdTxtCtrl)
, because the former already supports the operation you need.If all you need to do with stdout is direct some programs' output there, not direct all kinds of stuff there, don't change
sys.stdout
, just instantiate thesubprocess.Popen
with your own file-like object (createdTxtCtrl
) instead ofsys.stdout
.我已经将一个名为 tiotrap 的包部署到 PyPi,其中包含
TextIOTrap
辅助类用于管理 TextIO 流。安装:
Trap 写入列表,您也可以使用此方法实现自己的处理程序,只需用函数/方法调用替换 lambda:
这是一种可以使用它来存储捕获的数据的方法:
作为跨平台 DEVNULL替代品:
I have deployed a package to PyPi called tiotrap, which contains
TextIOTrap
helper class for managing TextIO Streams.Installation:
Trap writes to a list, you could implement your own handler with this method as well, just substitute the lambda with a function/method call:
Here is a way you can use it to store the captured data:
As a cross platform DEVNULL replacement:
sys.stdout = StdOut(createdTxtCtrl)
不会创建循环依赖吗?尝试不要将sys.stdout
重新分配给从sys.stdout
派生的类。Wouldn't
sys.stdout = StdOut(createdTxtCtrl)
create cyclical dependency? Try not reassigningsys.stdout
to the class derived fromsys.stdout
.sys.stdout
不是一个类
,它是一个实例(类型为file
)。所以,就这样做:
不需要继承
file
,只需创建一个像这样的简单的类似文件的对象! 鸭子类型是你的朋友...(请注意,在 Python 中,与大多数其他 OO 语言一样,但与 Javascript 不同,你只能从类 AKA 类型继承,从不从 < em>类/类型的实例;-)。
sys.stdout
is not aclass
, it's an instance (of typefile
).So, just do:
No need to inherit from
file
, just make a simple file-like object like this! Duck typing is your friend...(Note that in Python, like most other OO languages but differently from Javascript, you only ever inherit from classes AKA types, never from instances of classes/types;-).