pytoolbox.console module

pytoolbox.console.confirm(question=None, default=False, stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Return True if user confirm the action, else False. default if user only press ENTER.

Example usage

>> confirm('Do it now', default=True)
Do it now ? [Y/n]:
True
>> confirm('Are you sure', default=False)
Are you sure ? [y/N]:
False
>> confirm('Really, I am sure that is false', default=False)
Really, I am sure that is false ? [y/N]: y
True
pytoolbox.console.choice(question='', choices=(), stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Prompt the user for a choice and return his/her answer.

Example of usage

>> choice('What is your favorite color?', ['blue', 'orange', 'red'])
What is your favourite color? [blue, orange, red]: orange
orange
>> choice(['male', 'female'])
[male, female]? female
female
pytoolbox.console.print_error(message, exit_code=1, stream=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)[source]

Print an error message and exit if exit_code is not None.

Example usage

In following example stream is set to sys.stdout and exit is disabled (for doctest):

>>> print_error("It's not a bug - it's an undoc. feature.", exit_code=None, stream=sys.stdout)
[ERROR] It's not a bug - it's an undoc. feature.
pytoolbox.console.progress_bar(start_time, current, total, size=50, done='=', todo=' ', template='\r[{done}{todo}]', stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Show a progress bar. Default template string starts with a carriage return to update progress on same line.

Example usage

>>> import functools, time
>>> progress = functools.partial(progress_bar, template='[{done}{todo}]', stream=sys.stdout)
>>> progress(time.time(), 10, 15, size=30)
[====================          ]
>>> progress(time.time(), 1, 6, size=10)
[=         ]
>>> progress(time.time(), 3, 5, size=5, done='+', todo='-')
[+++--]
pytoolbox.console.shell(banner=None, history_filename='~/.python_history', history_length=1000, imported=None)[source]

Execute an interactive shell with auto-completion and history (if available).