Surrerstry.pl

Entries Projects About

 Smart Input - Python library to filtering out user input

30 June 2018, (Update: 25 July)

Recently I created library that facilitate parsing correct input from user.

Examples of usage on Python3, You will get more if you use help(smart_input).
# works the same like standard function.
smart_input('')

# Function will be repeated until passed data become convertible into specified type, maxium 3 times.
smart_input('', type_restriction=int, tries=3)

# we will get as result our definied type_restriction
smart_input('', type_restriction=int, tries=3, return_converted=True)

# In this case we specify function to call on each incorrect input
smart_input('', type_restriction=int, tries=3, action_when_fail=lambda: print('Please pass integer.'))

# We can define also action on correct input
smart_input('', type_restriction=int, tries=3, action_when_success=lambda: print('Great your input is correct'))

# Both actions together
smart_input('', type_restriction=int, tries=3, action_when_fail=lambda: print('Please pass integer.'), action_when_success=lambda: print('Great your input is correct'))

# In parameter action_on_the_end we set function that will be called always on the end :)
smart_input('', type_restriction=int, tries=3, action_on_the_end=lambda: print("I'm called always on the end"))

# It will be asking forver until input is correct
smart_input('', repeat_until_success=True, type_restriction=int)

# It this way we can use regex as condition
smart_input('', repeat_until_success=True, regex=r"\d")

# We can mix type_restriction and regex
smart_input('', repeat_until_success=True, regex=r"\d\.\d", type_restriction=float)

# We can base success or fail on results from our function
smart_input('', repeat_until_success=True, conditioning_call=lambda: randint(0,1))

# We can also pass to this function input passed by user
# For that we have to name one of our parameters as 'user_input'
smart_input('', repeat_until_success=True, conditioning_call=lambda user_input: 1 if user_input == 'a' else 0)

# When we have more conditions conditioning_call is called on the end
smart_input('', repeat_until_success=True, type_restriction=float, conditioning_call=lambda user_input: 1 if user_input == '1.5' else 0)

In general you can mix parameters together like you want until it's reasonable.
For example you cannot mix repeat_until_success and tries...

You can install that library from pip by command: pip install smart_input.
And import it like this: from smart_input import smart_input

Or if you are using python2:
from smart_input import smart_input or from smart_input import raw_smart_input.
On Python 2 I implemented two versions to imitate input and row_input.

--------------------------------
Update 1, (25 July 2018)
--------------------------------

I added method that in combination with smart_input lets you create simple menu quickly.
Example of usage together with smart_input:

from smart_input import smart_input, menu

menu_structure = [
{1:('1. Menu description', lambda: print('A'))},
{2:('2. Menu description', lambda: print('B'))},
{3:('3. Menu description', lambda: print('C'))}
]

my_menu = menu(menu_structure)

# print out menu only
my_menu()

# take action based on user input (without printing menu)
my_menu( smart_input('', repeat_until_success=True, type_restriction=int, return_converted=True) )

Source code:
Github Smart Input
Onsite smart_input.zip


Posts



Miscellaneous


Surrerstry.pl © 2014-2017-2018