IT tutorials
 
Database
 

MySQL for Python : Simple Insertion - A command-line insertion utility (part 2) - Coding the other functions

6/14/2013 7:38:19 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2.2 Coding the other functions

We are not done yet. With main() finished, we now have to fill out the program with the auxiliary functions that were called in the course of main().

  • valid_digit()

  • valid_string()

  • valid_table()

  • query()

The other functions must be inserted before the main() function is called, otherwise Python will throw a NameError.


valid_digit() and valid_string()

The first three functions should validate input as: digital, alpha character string, or as a valid number on the table menu presented to the user. If the input does not check out, then each should call query() to ask the user for new input, providing the type of valid input required as an argument with the value input by the user. The first two require only the value to be validated. They therefore look like this:

def valid_digit(value):
if value.isdigit() is not True:
value = query(value, "digit")
else:
value = value
return value
def valid_string(value):
if value.isalpha() is not True:
value = query(name, "alpha")
else:
value = value
return value

valid_table()

To validate the table selection, you will remember that we passed the number of the user's selection and the table names to valid_table(). This function compares the user's selection to the number of tables available and calls query() if there is a problem. The function is therefore coded as follows:

def valid_table(choice, tables):
valid_choice = valid_digit(choice) # Check whether the choice is a valid number
valid_choice = int(valid_choice)
while 0 <= valid_choice <= len(tables) : # Ensure the choice is among the valid options
print "Your selection is outside the bounds of possible choices."
valid_choice = query(valid_choice, "digit")
return valid_choice


					  

query()

The function query() uses an if...elif...else structure to alert the user to the malformed data, asks for new input, and validates it. It therefore calls valid_digit() and valid_string() as necessary for the last task. Querying and testing is of two kinds: digit and alpha. Within the present program, only these two are called, but there is a chance that this function could be called wrongly from another Python program. Therefore, we try to fail softly by returning 1 if the wrong argument is passed for type.

def query(value, type):
if type == "alpha":
print "The value you entered ('%s') is not correct. Please enter a valid value." %(value)
new_value = raw_input("New value: ")
valid_string(new_value)
return new_value
elif type == "digit":
print "The value you entered ('%s') is not correct. Please enter a valid value." %(value)
new_value = raw_input("New value: ")
valid_digit(new_value)
return new_value
else:
## if type is neither "alpha" nor "digit"
return 1					  
 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us