Python的StringIO的 - 有选择地将数据放入标准输入(Python StringIO -

2019-09-27 17:46发布

我们使用一点点编译Python代码,我们没有源。 代码提示用户输入的,我们正在努力的部分自动化。

基本上,询问用户名,密码,则根据某些情况下,一些各种各样的问题。 我不知道编译功能是否正在使用的raw_input,输入或别的东西。

我已经能够使用StringIO的更换标准输入W /用户名和密码,我可以用我自己的类替换标准输出,并找出哪些提示快到了,但我很为难,当谈到选择性地将基于数据为标准输入在我从标准输出读取。

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.stdout.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self):
        pass

    def write(self, msg):
        if re.search("The file exists, overwrite", msg):
            # put data into stdin 
        if re.search("The file is marked for notification", msg):
            # put data into stdin

sys.stdout = Catcher()
test()

我不能只预加载StringIO对象,因为这些问题可能会根据情况有所不同,但我需要自动送入标准输入,因为他们正试图把这个变成一个自动生成系统,所以他们将提供的默认值通过命令行来回答出现的任何问题。

不知道如何使它等待输入 - 如果我调用编译功能,那么它只是错误了与EOF之前设置标准输入到一个空StringIO对象。

事情是这样的:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite", msg):
            self.stdin.write('yes\n')
        if re.search("The file is marked for notification", msg):
            self.stdin.write('no\n')

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()

生产:

Traceback (most recent call last):
  File "./teststdin.py", line 25, in <module>
    test()
  File "./teststdin.py", line 8, in test
    overwrite = raw_input("The file exists, overwrite? ")
EOFError: EOF when reading a line

有任何想法吗?

Answer 1:

如果你想从阅读StringIO你刚刚写,你必须先退给你已经开始写的位置。
此外,对于错误的字符串你的第二个搜索测试。

这应该工作:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite?", msg):
            self.stdin.truncate(0)
            self.stdin.write('yes\n')
            self.stdin.seek(0)
        if re.search("This file is marked for notifies.  Notify?", msg):
            self.stdin.truncate(0)
            self.stdin.write('no\n')
            self.stdin.seek(0)

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()


Answer 2:

下面是避免解决方案StringIO全部:

import sys
import re

class Catcher(object):
    def __init__(self, handler):
        self.handler = handler
        self.inputs = []

    def __enter__(self):
        self.__stdin  = sys.stdin
        self.__stdout = sys.stdout
        sys.stdin = self
        sys.stdout = self

    def __exit__(self, type, value, traceback):
        sys.stdin  = self.__stdin
        sys.stdout = self.__stdout

    def write(self, value):
        result = self.handler(value)
        if result:
            self.inputs = [result] + self.inputs

    def readline(self):
        return self.inputs.pop()

使用:

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))


@Catcher
def exist_notify(msg):
    if re.search("The file exists, overwrite", msg):
        return 'yes'
    if re.search("This file is marked for notifies", msg):
        return 'no'

with exist_notify:
    test()


文章来源: Python StringIO - selectively place data into stdin