Last active
February 27, 2020 01:03
-
-
Save littlebenlittle/9acf693e171201284a9875a9ac1cb7ff to your computer and use it in GitHub Desktop.
Oveload the input() builtin with a generator of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import builtins | |
from unittest.mock import patch | |
class InputSeq(): | |
'''Emit elements of a sequence by invoking __call__''' | |
def __init__(self, input_seq): | |
self._input_seq = input_seq.copy() | |
self._index = -1 | |
def __call__(self, _=None): | |
self._index += 1 | |
return self._input_seq[self._index] | |
def input_seq(seq): | |
def wf(f): | |
with patch.object(builtins, 'input', InputSeq(seq)): | |
f() | |
return wf | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment