相关疑难解决方法(0)

如何在Python中显示重定向的stdin?

我正在开始一个Python项目,其中stdin重定向是必要的,使用类似于下面的代码:

import sys
import StringIO

s = StringIO.StringIO("Hello")
sys.stdin = s
a = raw_input("Type something: ")
sys.stdin = sys.__stdin__
print("You typed in: "+a)
Run Code Online (Sandbox Code Playgroud)

问题是,代码运行后,将显示以下内容:

输入内容:输入:Hello

有没有办法修改我的代码,以便显示以下内容?

输入内容:你好

你输入了:你好

我一直在寻找高低,但尚未找到答案.如果有人有想法我真的很感激.谢谢!

python redirect stdin

7
推荐指数
1
解决办法
3636
查看次数

Python单元测试覆盖模块级功能

Python单元测试代码相关,它调用OS/Module级python函数.在我的单元测试期间,我重载一些python系统调用以使我的测试驱动模块的不同路径.这种技术称为Monkey Patch(在相关问题中),用于隔离测试.

我有点担心当我并行运行Python测试时会发生什么,比如"Nose".当两个测试并行运行并且都想模拟os.path.exists方法时会发生什么?

有没有办法在我的测试环境中有选择地覆盖系统或模块功能?

以下面的例子为例

fixture.py (say that is the module under test)

def my_func():
    some_stuff

test_fixture.py (say this is my test case)


class MyTest(unittest.TestCase):

    def test_mine(self):
         fixture.my_func = my_new_func
         fixture.execute_some_func_that_calls_my_func()
         #What happens if another test is executing at the same time and accesses
         #my_func I don't want it to start executing my_new_func?
Run Code Online (Sandbox Code Playgroud)

python python-2.7

6
推荐指数
1
解决办法
1510
查看次数

标签 统计

python ×2

python-2.7 ×1

redirect ×1

stdin ×1