[PYTHON] Write standard input in code

For example, when you want to test standard input, it is troublesome to input every time, but reading an external file only increases garbage. If you want to test the original code as cleanly as possible, it seems better to override input ().

Write down the contents of the standard input you want to test as follows,

stdin_test = """1 2
3 4
5 6
"""

Override input () on the generator

def inp(t):
    from io import StringIO
    for i in StringIO(t):
        yield i
input = inp(stdin_test).__next__

Example of use

a = [list(map(int, input().split())) for i in range(3)]
print(a)

>>> [[1, 2], [3, 4], [5, 6]]

I don't think there is a better way end

Recommended Posts

Write standard input in code
Write Spigot in VS Code
Write selenium test code in python
Write python-like code
[Python] Standard input
Standard input summary
Write Python-like code (dictionary)
Write Pulumi in Go
Key input in Python
Write DCGAN in Keras
Write decorator in class
Write Python in MySQL
Key input in Python
[Python] About standard input
Standard input / output summary
I want to write in Python! (1) Code format check
Clearly write code that increases conditional branching in multiplication
Write Pandoc filters in Python
Standard input with time limit
Write beta distribution in Python
[Python3] Standard input [Cheat sheet]
Write python in Rstudio (reticulate)
Part 1 of receiving standard input
[For beginners] Summary of standard input in Python (with explanation)
Python3 standard input (competition pro)
Standard input / summary / python, ruby
Write data in HDF format
Receiving standard input tips @ python
Python Input Note in AtCoder
Transposed matrix in Python standard
Generate QR code in Python
Standard area code current value
Write Spider tests in Scrapy
[For AtCoder] Standard input memo
Character code learned in Python
Repeated @ app.callback in Dash How to write Input and State neatly
I wrote the code to write the code of Brainf * ck in python
Input / output method of values from standard input in competitive programming, etc.
Python standard input summary that can be used in competition pro
Use <input type = "date"> in Flask
Python3 standard input for competitive programming
Write a binary search in Python
Write a table-driven test in C
Write & compile & run code at codeanywhere.com
Matrix representation with Python standard input
Set placeholders in input fields in Django
[Python] Generate QR code in memory
Write JSON Schema in Python DSL
How to write soberly in pandas
Automatically format Python code in Vim
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Write standard output to a file
I can't enter standard in Subprocess ...
Write A * (A-star) algorithm in Python
[Maya] Write custom nodes in Open Maya 2.0
Make standard output non-blocking in Python
Write foreign key constraints in Django
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python