Skip to content

Instantly share code, notes, and snippets.

@littlebenlittle
Last active February 27, 2020 01:03
Show Gist options
  • Save littlebenlittle/9acf693e171201284a9875a9ac1cb7ff to your computer and use it in GitHub Desktop.
Save littlebenlittle/9acf693e171201284a9875a9ac1cb7ff to your computer and use it in GitHub Desktop.
Oveload the input() builtin with a generator of strings
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