|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## <u> PyInputPlus Module </u> :\n", |
| 8 | + " - PyInputPlus contains functions similar to input() for several kinds of data: numbers, dates, email addresses, and more.\n", |
| 9 | + " - PyInputPlus will reprompt them for input if it didnt match the condition. \n", |
| 10 | + " - PyInputPlus also has other useful features like a limit for the number of times it reprompts users and a timeout if users are required to respond within a time limit." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "## No Python Standard Library is not included with PyInputPlus.You need to manually install using pip command." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## PyInputPlus commonly imported with import pyinputplus as pyip because shortername becomes easy to call it's functions" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## Difference between inputInt() and inputFloat() is that whatever user will enter inputInt() will convert the value in int and for floating values it will convert inputfloat()" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 1, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [ |
| 39 | + { |
| 40 | + "name": "stdout", |
| 41 | + "output_type": "stream", |
| 42 | + "text": [ |
| 43 | + "Enter a number: Akash\n", |
| 44 | + "'Akash' is not an integer.\n", |
| 45 | + "Enter a number: 2\n", |
| 46 | + "2\n" |
| 47 | + ] |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "## In below example, you see that unlike input(), these functions return an int or float value: 2 or 2.0 instead of the strings '42' and '3.14'.\n", |
| 52 | + "import pyinputplus as pyip\n", |
| 53 | + "result = pyip.inputInt(prompt='Enter a number: ')\n", |
| 54 | + "print(result)" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 19, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "name": "stdout", |
| 64 | + "output_type": "stream", |
| 65 | + "text": [ |
| 66 | + "Enter a number: 5.0\n", |
| 67 | + "Converted to int: 5\n" |
| 68 | + ] |
| 69 | + } |
| 70 | + ], |
| 71 | + "source": [ |
| 72 | + "## Using inputInt()\n", |
| 73 | + "result = pyip.inputInt(prompt='Enter a number: ')\n", |
| 74 | + "print(\"Converted to int: \",result)" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 17, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [ |
| 82 | + { |
| 83 | + "name": "stdout", |
| 84 | + "output_type": "stream", |
| 85 | + "text": [ |
| 86 | + "Enter a number: 34\n", |
| 87 | + "Converted to Float: 34.0\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "## Using inputFloat()\n", |
| 93 | + "g = pyip.inputFloat(prompt='Enter a number: ')\n", |
| 94 | + "print(\"Converted to Float: \",g)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 3, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "name": "stdout", |
| 104 | + "output_type": "stream", |
| 105 | + "text": [ |
| 106 | + "Enter wholeNumber from 0 to 99 : 2.5\n", |
| 107 | + "'2.5' is not an integer.\n", |
| 108 | + "Enter wholeNumber from 0 to 99 : 2\n" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "source": [ |
| 113 | + "## WholeNumber Validation\n", |
| 114 | + "response = pyip.inputInt('Enter wholeNumber from 0 to 99 : ', min=0,max=99)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "## The allowRegexes and blockRegexes keyword arguments take a list of regular expression strings to determine what the PyInputPlus function will accept or reject as valid input." |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 13, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "name": "stdout", |
| 131 | + "output_type": "stream", |
| 132 | + "text": [ |
| 133 | + "\n", |
| 134 | + "Blank values are not allowed.\n", |
| 135 | + "\n", |
| 136 | + "Blank values are not allowed.\n", |
| 137 | + "\n", |
| 138 | + "Blank values are not allowed.\n" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "ename": "RetryLimitException", |
| 143 | + "evalue": "", |
| 144 | + "output_type": "error", |
| 145 | + "traceback": [ |
| 146 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 147 | + "\u001b[1;31mValidationException\u001b[0m Traceback (most recent call last)", |
| 148 | + "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\pyinputplus\\__init__.py\u001b[0m in \u001b[0;36m_genericInput\u001b[1;34m(prompt, default, timeout, limit, applyFunc, validationFunc, postValidateApplyFunc, passwordMask)\u001b[0m\n\u001b[0;32m 166\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 167\u001b[1;33m possibleNewUserInput = validationFunc(\n\u001b[0m\u001b[0;32m 168\u001b[0m \u001b[0muserInput\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 149 | + "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\pyinputplus\\__init__.py\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(value)\u001b[0m\n\u001b[0;32m 242\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 243\u001b[1;33m validationFunc = lambda value: pysv._prevalidationCheck(\n\u001b[0m\u001b[0;32m 244\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mblank\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mblank\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstrip\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mstrip\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mallowRegexes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mallowRegexes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mblockRegexes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mblockRegexes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexcMsg\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 150 | + "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\pysimplevalidate\\__init__.py\u001b[0m in \u001b[0;36m_prevalidationCheck\u001b[1;34m(value, blank, strip, allowRegexes, blockRegexes, excMsg)\u001b[0m\n\u001b[0;32m 249\u001b[0m \u001b[1;31m# value is blank but blanks aren't allowed.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 250\u001b[1;33m \u001b[0m_raiseValidationException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Blank values are not allowed.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexcMsg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 251\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mblank\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mvalue\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 151 | + "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\pysimplevalidate\\__init__.py\u001b[0m in \u001b[0;36m_raiseValidationException\u001b[1;34m(standardExcMsg, customExcMsg)\u001b[0m\n\u001b[0;32m 221\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcustomExcMsg\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 222\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValidationException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstandardExcMsg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 223\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 152 | + "\u001b[1;31mValidationException\u001b[0m: Blank values are not allowed.", |
| 153 | + "\nDuring handling of the above exception, another exception occurred:\n", |
| 154 | + "\u001b[1;31mRetryLimitException\u001b[0m Traceback (most recent call last)", |
| 155 | + "\u001b[1;32m<ipython-input-13-7223a15bd62c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m## It gives to give ValidationException and later RetryLimitException\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpyip\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minputStr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlimit\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 156 | + "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\pyinputplus\\__init__.py\u001b[0m in \u001b[0;36minputStr\u001b[1;34m(prompt, default, blank, timeout, limit, strip, allowRegexes, blockRegexes, applyFunc, postValidateApplyFunc)\u001b[0m\n\u001b[0;32m 245\u001b[0m )[1]\n\u001b[0;32m 246\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 247\u001b[1;33m return _genericInput(\n\u001b[0m\u001b[0;32m 248\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 249\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdefault\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 157 | + "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\pyinputplus\\__init__.py\u001b[0m in \u001b[0;36m_genericInput\u001b[1;34m(prompt, default, timeout, limit, applyFunc, validationFunc, postValidateApplyFunc, passwordMask)\u001b[0m\n\u001b[0;32m 186\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 187\u001b[0m \u001b[1;31m# If there is no default, then raise the timeout/limit exception.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 188\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mlimitOrTimeoutException\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 189\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 190\u001b[0m \u001b[1;31m# If there was no timeout/limit exceeded, let the user enter input again.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 158 | + "\u001b[1;31mRetryLimitException\u001b[0m: " |
| 159 | + ] |
| 160 | + } |
| 161 | + ], |
| 162 | + "source": [ |
| 163 | + "## It gives ValidationException and later RetryLimitException \n", |
| 164 | + "response = pyip.inputStr(limit=3)" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": 11, |
| 170 | + "metadata": {}, |
| 171 | + "outputs": [ |
| 172 | + { |
| 173 | + "name": "stdout", |
| 174 | + "output_type": "stream", |
| 175 | + "text": [ |
| 176 | + "\n", |
| 177 | + "Blank values are not allowed.\n", |
| 178 | + "\n", |
| 179 | + "Blank values are not allowed.\n", |
| 180 | + "\n", |
| 181 | + "Blank values are not allowed.\n", |
| 182 | + "The default value is printed: hello\n" |
| 183 | + ] |
| 184 | + } |
| 185 | + ], |
| 186 | + "source": [ |
| 187 | + "## Here after reaching maximum reach of limit it will try to print the default value\n", |
| 188 | + "res = pyip.inputStr(limit=3, default='hello')\n", |
| 189 | + "print(\"The default value is printed: \",res)" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "metadata": { |
| 194 | + "kernelspec": { |
| 195 | + "display_name": "Python 3", |
| 196 | + "language": "python", |
| 197 | + "name": "python3" |
| 198 | + }, |
| 199 | + "language_info": { |
| 200 | + "codemirror_mode": { |
| 201 | + "name": "ipython", |
| 202 | + "version": 3 |
| 203 | + }, |
| 204 | + "file_extension": ".py", |
| 205 | + "mimetype": "text/x-python", |
| 206 | + "name": "python", |
| 207 | + "nbconvert_exporter": "python", |
| 208 | + "pygments_lexer": "ipython3", |
| 209 | + "version": "3.8.5" |
| 210 | + } |
| 211 | + }, |
| 212 | + "nbformat": 4, |
| 213 | + "nbformat_minor": 4 |
| 214 | +} |
0 commit comments