Reference no: EM131054307
Database Assignment
Overview: We will create a basic database program.
Much of this assignment is conceptually similar to the earthquake example; look at that code (in the in-class examples) for guidance.
The earthquake example required a more complicated input, two splits were necessary. Only one split is needed for this assignment.
Description: Databases contain records. Each record contains information about an item. Each piece of information in a record is called a field.
You may choose the type of information you would like your database to store.
There must be at least three pieces of information stored for each record, you may store more.
Some possible databases are:
Phonebook:
|
Last name
|
First name
|
Phone number
|
Library:
|
Book title
|
Author
|
Subject/Category
|
Music:
|
Song title
|
Performer
|
Album
|
Other:
|
...
|
...
|
...
|
The description will be in terms of the Phonebook, make appropriate adjustments if you do something different.
Phonebook sample_data.
The program is comprised of 3 files (see below also):
1. database.py - The provided file. You may adjust this according to your database details if you want.
2. database_mod.py - The file that contains the functions you write. Put your functions in the file database_mod.py.
3. database.txt - The file that contains the phonebook data. Put your functions in the file database_mod.py.
The provided database program file:
• database.py
When run the program will provide the user with a menu from which to choose an operation, carry out the selected operation, and repeat. The operations are:
• Look up a record by the last name
• Look up a record by the first name
• Look up a record by the phone number
• Output all records formatted nicely
• Add a record.
Also:
• When run the program will automatically load the data from a file.
• When done the program will automatically save the data to a file.
Development: Details for each function is provided in the next section.
In software development it is a good idea to get a simple version working that has rudimentary functionality, and then add functionality incrementally until the program performs as required.
Develop your program using the following steps.
1. Copy the contents of the provided database start file. You may adjust it as appropriate to your database later.
2. Get the provided file to execute without error.
Do this by adding a stub for each function in the
database_mod.py file.
A stub is a function definition that does simply outputs a message and/or returns a value.
For example:
def open_db(file_name):
print('open_db called', file_name)
With the print for each stub also print the value of each parameter.
As you add stubs for other functions and run your program you can see the values sent from the call to the definition on each call. The first value for the remaining calls will be None bacause the open_db function returns None by default.
Once you have stubs for each function that is called in the provided main function (except show_menu, the definition for it is provided) you can run the program without error.
Run your program, if you have not done so already. The program should run at this point but of course it will not bahave correctly because the functions do not do what they need to yet.
If there is an error message about a missing function you missed one, write a stub for the missing function and run the program again.
3. Implement a preliminary version of the open_db that simply inputs lines and ouputs the line that was input.
Run your program. You should see lines from your input file outputted.
4. Remove the print's from open_db.
Store the the lines that were input in the list, and return the list.
Look at the File I/O in the earthquake in-class examples.
Run your program. Since there is no way to get output yet it is hard to tell if the program is inputting correctly.
5. Implement the output_all method.
Initially the function should simply output each item in the db list.
Run your program, observe the outputted list.
You now know that the program is inputting from the file and that the list has the lines of the file in it.
6. Have the open db split each line on whatever splitter character you are using (the sample data above used a colon to separate the fields).
Use the strip on each of the values from the split.
Put the result of the split in the list that is returned so that the returned list is a list of lists.
Run your program, observe the outputted list is now a list of lists.
Get this to work.
You now have a good start.
7. Get the output_all function to work correctly
8. Implement the remaining functions.
The Functions: The program will contain the following (or similar) functions:
1. open_db(file_name)
Summary: This function opens the database and loads the data from a file.
Parameters:
• file_name - The name of the file that will be opened.
Return: Return the result list.
Implementation Notes:
This function, when complete, should:
• Open the file whose name is given in the parameter variable
• Create an empty result list
• Input each line from the file (use a loop)
• Use split on the line
• Use the strip method on each field. For example:
record[0] = record[0].strip()
This will remove leading and trailing whitespace so the
• comparisons in the search functions will work correctly. Append the list you get from the split to the result list
• When done inputting the file (after the loop) close the file and return the result list.
2. output_all(db)
Summary: This function outputs all data of the database in a nicely formatted manner
Parameters:
• db - The "database", the list of lists.
Return: No return is needed.
Implementation Notes:
• Use a loop to go through the db list and call the
output_record function.
3. output_record(db, idx)
Summary: This function outputs a single database record. This function is used so that output is consistent. It is also called in the provided code.
Parameters:
• db - The "database", the list of lists.
• idx - The index of the record to be output.
Return: No return is needed.
Implementation Notes:
• Output the item from the database list db at the given index.
• Use string formatting (the % formatting operator).
4. lookup_last_name(db, name)
Summary: Search the database for the given name.
Parameters:
• db - The "database", the list of lists.
• name - The name to search for.
Return: The index where the item was found. Return None if not found.
Implementation Notes:
• Search the database list, db, for the name in the parameter name. (Use a loop.)
• The search must be case insensitive. If the user enters a string that differs only in case then a match should be found.
5. lookup_first_name(db, name)
Summary: Search the database for the given name.
Parameters:
• db - The "database", the list of lists.
• name - The name to search for.
Return: The index where the item was found. Return None if not found.
Implementation Notes:
• Search the database list, db, for the name in the parameter name. (Use a loop.)
• This function is similar to the first search function, the difference is it is comparing the parameter value to a different item in the sub-list.
• The search must be case insensitive. If the user enters a string that differs only in case then a match should be found.
6. lookup_number(db, number)
Summary: Search the database for the given number.
Parameters:
• db - The "database", the list of lists.
• number - The number to search for.
Return: The index where the item was found. Return None if not found.
Implementation Notes:
• Search the database list, db, for the number in the parameter number. (Use a loop.)
• This function is similar to the first search function, the difference is it is comparing the parameter value to a different item in the sub-list.
• Note that the number is a string so it may have dashes in it if desired.
• The search must be case insensitive. If the user enters a string that differs only in case then a match should be found.
7. add_record(db, first_name, last_name, number)
Summary: Add a record to the database
Parameters:
• db - The "database", the list of lists.
• first_name - The first name for the record.
• last_name - The last name for the record.
• number - The number for the record.
Return: No return is needed.
Implementation Notes:
• Create a list of the three paramter field values and append it to the db list.
8. close_db(db, file_name)
Summary: This function saves the contents to a file, the same file that the open_db opens and reads from when the program starts.
Parameters:
• db - The "database", the list of lists.
• file_name - The name of the file that will be opened.
Return: No return is needed.
Implementation Notes:
• Open the file specified by the parameter file_name.
• Use a loop to output each database item to the file.
• Output the field separator also.
• When done outputing to the file (after the loop) close the file.
Make a backup copy of your input file.
When a file is opened for writing/saving its previous contents are erased. If your program fails after this the orginal contents will be lost. If you have a backup of the data (phonebook) file you can copy it in and save some typing in recreating the original data file.