Skip to main content

Python user input prompt toolkit

Project description

ASKpy is a module which makes collecting Raw Input for your Python program more robust. It supports creating prompts, conditional questions, validation, and transforming responses.

Installation

Installing ASKpy is as simple as:

pip install askpy

Usage

ASKpy at its core is a way to build up questions and create a prompt for users in your terminal applications.

from askpy import Prompt

prompt = Prompt()

my_question = prompt.make_question('name', 'What is your name?')

prompt.add(my_question)

prompt.collect()

name = prompt.get_response('name')

While the above may hide raw input, it does not give you much more power than you could already use in the standard library. ASKpy allows you to validate and transform your responses easily:

from askpy import Prompt, Validator

prompt = Prompt()

question = prompt.make_question('age', 'How old are you?') \
                 .must(Validator.num_gt(20)) \
                 .transform(int)

prompt.add(question)

prompt.collect()

question = prompt.get_response('question')

The above example will ensure the user enters an age greater 20 otherwise it will reprompt them. Once it has a valid age it will cast it to an integer for you to use later.

There’s more too.

Sometimes questions depend on others and in order to handle these cases you would usually rely on conditional statements and parsing. With ASKpy you can do this by chaining questions.

from askpy import Prompt, ValidationError, Validator

prompt = Prompt()


def validate_even(num):
    Validator.num(num)
    if int(num) % 2 != 0:
        raise ValidationError('Please choose an even number')


def validate_odd(num):
    Validator.num(num)
    if int(num) % 2 == 0:
        raise ValidationError('Please choose an odd number')


even_or_odd = prompt.make_question('even_or_odd', 'Do you prefer even or odd numbers?') \
                    .must(Validator.one_of(['even', 'odd']))

even_question = prompt.make_question('even', 'What is your favorite even number?') \
                      .must(validate_even) \
                      .transform(int)

odd_question = prompt.make_question('odd', 'What is your favorite odd number?') \
                     .must(validate_odd) \
                     .transform(int)

prompt.add(even_or_odd) \
      .then(lambda resp: resp == 'even', even_question, odd_question)

prompt.collect()

As you can see, you get explicit prompts with the ability to keep your code sane.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

askpy-0.0.4.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

askpy-0.0.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file askpy-0.0.4.tar.gz.

File metadata

  • Download URL: askpy-0.0.4.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for askpy-0.0.4.tar.gz
Algorithm Hash digest
SHA256 413e29642c7a637f40650fe08412e1f97dbcbb3d7138a93842720a34046ddc4e
MD5 edf37ec0967ab57d5240f158c943de4d
BLAKE2b-256 58bc5e600cb7bc346b35044417e62b294075a1e1083cfa20843d41ca133999f3

See more details on using hashes here.

File details

Details for the file askpy-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for askpy-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 989e6e9688ec7fac7dd3de2fb086b7eea4c3d26b4df3ef48d546203eb2e69772
MD5 2b4907e88929ce2ad74ff769e632dcf8
BLAKE2b-256 ecfe30e009d83544cfc246d2bdc9f9781a0423663c80a27eac73c834d3768ead

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page