Skip to main content

A simple input while loop utility

Project description

inputwhile

A simple but helpful package that allows you to create input while loops with different conditions.

Explanation nobody cares about

Once, I was tired of writing:

value = input()
while not condition(value):
        value = input()

every time I wanted to check if a user entered the right value in a program.

So, I decided to make this package (my first one). It has a bunch of functions with pre-defined condition functions, but you can make custom inputwhiles too.

Installation

# Linux/macOS
python3 -m pip install inputwhile

# Windows
py -3 -m pip install inputwhile

Quick examples

Custom error messages

import inputwhile

# If input value is an empty string it will print "Enter your username:" again until input value is not empty
string = inputwhile.stringNotEmpty(
        "Enter your username:"
)

# If input value is an empty string it will print "You can't leave it empty. Please enter your username:" until input value is not empty.
string = inputwhile.stringNotEmpty(
        "Enter your username:",
        error_prompt="You can't leave it empty. Please enter your username:"
)

# error_prompt is optional, but is also in all package functions.

Number inputwhiles

import inputwhile

integer = inputwhile.integer(
        "Enter a number:",
        error_prompt="Invalid number. Please enter a valid number:"
)

floatNumber = inputwhile.floatInput(
        "Enter a float:",
        min=0.0,
        max=1.0 # You can add ranges too.
)

Custom types

import inputwhile
from myproject import BigInt

# Will request a value until it is parsable like: BigInt(value) and will return it with that type.
value = inputwhile.customTypeClass(
        "Enter the nuclear code:",
        typeClass=BigInt
)

Boolean inputwhiles

import inputwhile

# Only matches "true" or "false"
boolean = inputwhile.boolean(
        "It is illegal to keep a guinea pig in Switzerland. True or false?:",
        "Prompt not recognized. Please enter 'true' or 'false':"
)

# Matches "true", "t", "yes", "y", "1", "si", "sí", "s" or "false", "f", "no", "n", "0", "nope" by default.
flexibleBoolean = inputwhile.booleanFlexible(
        "Do you want to continue? (y/n):",
        trueStrings={"continue"},
        falseStrings={"exit"},
        doUseCustomNDefaults=True # Makes trueStrings and falseStrings being added to the default ones instead of replacing them.
)

regexBoolean = inputwhile.booleanFlexibleRegex(
        "Do you like tacos?:",
        trueRegex=f"^(yes|true|you don't have an idea|definitely|yes,[\s]*[\w]+)$"
        falseRegex=f"^(no|definitely no|never|no,[\s]*[\w]+)$"
        error_prompt="Prompt not recognized. Try again:"
)

Lists

import inputwhile

# List of strings
stringsList = inputwhile.listInput(
        "Enter the countries you have ever visited:",
        separator=",",
        error_prompt="Please, don't leave empty the input:",
        doStriptElements=True # Makes a element.strip() to every element in the list
)

# Can filter the list
from pathlib import Path
filteredList = inputwhile.listInput(
        "Enter the directories you want to scan:",
        separator=" ",
        error_prompt="Invalid directories. Please enter at least one directory:"
        filter_func=lambda x: Path(x).is_dir()
)

# Can map too
mappedList = inputwhile.listInput(
        "Enter the values of the Mexican peso per day in the last month:"
        error_prompt="Enter at least one value:"
        map_func=lambda x: int(x),
        filter_func=lambda x: inputwhile.utils.isParsable(x, int)
)

# Also sets available for unique values
setsToo = setInput(
        "Enter your social media urls:",
        separator=" ",
        error_prompt="Enter at least one url:"
        filter_func=lambda x: x.startswith("http")
)

# And of course regex
regexList = inputwhile.listRegex(
        "Enter your hobbies separated by commas:",
        regex_separator=r"[,]+",
        doStripElements=True
)

regexSet = inputwhile.setRegex(
        "Enter the file extensions you like the most:"
        regex_separator=r"[.,\s]+"
)

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

inputwhile-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inputwhile-1.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file inputwhile-1.0.0.tar.gz.

File metadata

  • Download URL: inputwhile-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for inputwhile-1.0.0.tar.gz
Algorithm Hash digest
SHA256 747e79b36975713f080bda6b13ade4b5d954429768f5f7caf929ad0d5910a3dd
MD5 2e6cf40732611311938f9ea4c9455927
BLAKE2b-256 c68a3fcc076df392a4f7515c70775547899e3de4d1fa7b7498d23b2364904cde

See more details on using hashes here.

File details

Details for the file inputwhile-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: inputwhile-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for inputwhile-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13942b726d1b32359d63a268bd59b226610098ac2a082e1517a93b4998545f9c
MD5 582605f53c956ea59a1ea8ff77acf9e8
BLAKE2b-256 896c02d987179c66d9a24f8fbe5f817708000795f9397ef1c51c0d6b3db8103e

See more details on using hashes here.

Supported by

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