easyinput module in Python
Last Updated :
29 Jul, 2021
easyinput module in Python offers an easy input interface analogous to cin stream in C++. It supports multiple data types including files and provides functionalities such as input till different data types, multi-line input, etc.
Installation
To install this module type the below command in the terminal.
pip install easyinput
Function Used
- read(*type, amount=1, file, as_list): Returns tokens from input stream.
Parameters:
- type : List of data types, for each type corresponding token is returned.
- amount : For repetition of given type for specific number of times.
- as_list : If true, stream of tokens is returned as list, otherwise, generator is returned.
- file : File entity stream.
- read_many(*types, amount=1, file=_StdIn): Reads token from input stream unless the element of other type is entered.
Example 1: Working of read() and read_many()
Python3
from easyinput import read_many, read
a = read(int)
b = read(str)
print("The elements using read : ")
print(a, b)
print("Integer inputs using read many : ")
for num in read_many(int):
print(num)
# reading the string after integers
print(read())
Output :
Demonstrating read() and read_many()
Example 2: Using amount() and as_list()
Usually, read() returns a list when working with multiple arguments in read(). If we need a generator to be used, not a list, as_list() can be set to false. The advantage it can have is that it may avoid iteration of the list to access and populate to different data types.
Python3
from easyinput import read
# input int, str, int chain 3 times
multi_input = read(int, str, amount=2)
# printing type and input
print(type(multi_input))
print(multi_input)
# putting as_list = False
print("Using as_list false : ")
multi_input = read(int, str, amount=2, as_list=False)
print(type(multi_input))
print(multi_input)
Output :
Using amount() and as_list()
Example 3: File input using read_many()
The read() and read_many() functions provide the functionality to get files as an input stream to get data from files and render on console, or to any file using file parameter.
Code :
Python3
from easyinput import read_many
print("Getting integer inputs from files : ")
with open('gfg_file_input') as inp_file:
for ele in read_many(int, file=inp_file):
print(ele)
Output :
Output numbers
Example 4: Using Custom data type with read()
Apart from primitive data types, read() can accept class instances accepting strings as input which can be used to translate to custom types for working. The example below gets lowercase words.
Python3
from easyinput import read
class ToLower:
def __init__(self, ele):
self.ele = ele.lower()
def print_ele(self):
print(self.ele)
# Gets object of ToLower class
ele = read(ToLower)
# printing the word
print("Lowercase string : ")
ele.print_ele()
Output :
Getting Input converting to lowercaseWorking with read_many_lines()
Similar to read_many(), the difference being it reads whole lines at once rather than moving to newline at spaces. Reads the whole line.
Syntax:
read_many_lines(rstrip=True, skip_empty=False)
Parameters:
rstrip : Skips all the trailing newline characters that are entered. By default value is True.
skip_empty : Defaults to false, when set to True, skips the lines which are just empty characters.
Code :
Python3
from easyinput import read_many_lines
print("Reading lines using read many lines : ")
count = 1
for line in read_many_lines():
print(line)
count = count + 1
if count > 5:
break
print("Reading lines using read many lines by skipping empty : ")
count = 1
for sline in read_many_lines(skip_empty=True):
print(sline)
count = count + 1
if count > 5:
break
Output :
Examples of read_many_lines
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read