- Details
- Parent Category: Programming Assignments' Solutions
We Helped With This Python Programming Assignment: Have A Similar One?
Assignment Description
ASSIGNMENT 2
Contents
Purpose
In this assignment, you will be demonstrating your understanding and use of python constructs, as covered in module 2.
This assignment is design to help you practice your skills at working with files. You will read data from a CSV file and, following input from the user, save view of the data to a CSV file.
Requirements:
NOTE: You are required to use the given dataset found in the attached file named:
“2010_Census_Populations_by_Zip_Code.csv”.
1. Let’s read the data into memory.
a. Using what you have learned in the past 2 modules, create a load_census_data function.
i. The function must use with to open the file
ii. Using the csv module (https://docs.python.org/3.6/library/csv.html#csv.DictReader), you must read the contents of the file
iii. Verify
1. that each row has 7 keys (the number of columns)
2. that each of the column names (keys) is found in each row
3. that all values are ints
iv. Place each validated row (from iii) into a list.
v. Any row that does not have valid data must be discarded (not added to the list)
vi. The function must return a tuple by converting the list into a tuple
2. Invoke load_census_data to get the data stored in the CSV file by passing the path to the file to the load_census_data function. It is OK to hard code the path to the CSV file.
a. Keep a reference to the resulting tuple by creating a variable
3. Now that we have data let’s ask the user what data they want to retrieve from the data set:
a. Create a tuple of all the column names
b. Enumerate (https://docs.python.org/3/library/functions.html#enumerate) all the column names and display to the user.
For example
1) Zip Code
2) Total Population
3) Median Age
4) Total Males
5) Total Females
6) Total Households
7) Average Household Size
c. ask the user which column to filter data by.
d. Read the input from the user (the integer).
e. Validate user input:
i. User input corresponds to one of the sought numbers [1, 7].
ii. If the user’s input is invalid, ask for re-entry.
iii. Allow the user to retry three times and then exit the program if the user keeps providing an invalid value.
4. Let’s ask the user to provide a floor value to filter our data by.
a. Read the input from the user.
b. Validate the user input:
i. User input is a positive integer.
ii. In the same way as 2eiii, allow three retries and then exit the program.
5. Now that we have the column name and a floor value to filter by, we need to filter the data by writing filter_data_by_column_and_floor (place the function in the same file as load_census_data and declare the new function right below it to keep your code organized).
a. the function must take a tuple (our data), a column name, and a floor value as parameters
b. iterate through each row of the data and build a new list of all the “rows” having the selected column’s value greater than the floor value.
c. Return a tuple built from the list created in b.
6. Invoke filter_data_by_column_and_floor with the appropriate arguments: the tuple of rows (our data), the column name selected by the user at step 3. (we want the string value here e.g. “Zip Code”.), the floor value selected by the user at step 5.
7. Now that we have data filtered let’s ask the user to know what column they want to sort by.
a. Read the input from the user (the integer).
b. Remember to validate the user input:
i. the input corresponds to one of the sought numbers[1, 7].
ii. Apply the same validation you did in 3.
8. Using the sorted function (https://docs.python.org/3.6/library/functions.html#sorted), sort the filtered data by the column name selected by the user. You may need to create a function as key to sort. (use the default sorting order).
9. Now, let’s save our data to a CSV file so the user can use it later. We are going to place the file into a folder named exports.
a. Create a folder named exports if it does not already exist ( https://docs.python.org/3/library/os.html#os.makedirs ). It is OK to create it wherever you want on the file system.
b. Prompt the user for a file name (make sure that is ends with “.csv” and is not empty).
c. Use the filename in conjunction with the “os.join” python function to create the path ( https://docs.python.org/3/library/os.path.html#os.path.join ) where the file will be saved under the folder exports
d. Make sure that the file does not already exist (ask for a new filename if it does)
i. Allow the user to retry three times and then exit the program if the user keeps providing an invalid filename.
e. Write the filtered, sorted data to the file.
i. Make sure to use with to open the file
ii. Use the csv module to write the file (https://docs.python.org/3.6/library/csv.html#csv.DictWriter).
Code/Comment Format
Good code includes well named variables that are consistent from the beginning to the end of the program. Naming of objects should be self-explanatory. For instance, iterator_for_noun_list is much better than i.
Every program consists of a sequence of paragraphs, each of which has objectives, and which builds on the previous paragraphs. We are mostly interested in objectives that are valid at the end of the program so we can verify the program's design. The following is a preferred form for such paragraph headings. The # sign is adequate when the comment is a single line.
#This is an in-line comment – used to document the code for you, or anyone else, that intends
#To extend the code
In-line comments are helpful when one has to go back to the code 6 months later to make changes.
For doc strings, python allows the use of triple quotes. The triple quotes can be either single or double quotes. A doc sting is generally used as user documentation. It does not need to include details of the implementation of the program, but instead it provides documentation as how to use the API for the program (input, output etc.)
For example:
“””
This is an example of a doc string
It allows multiple lines within the string.
“””
‘’’
This is an example of a doc string
It allows multiple lines within the string.
‘’’
This becomes significant when using functions, classes etc. as the triple quotes help to self-document the parameters and return values of the function.
What to Deliver
You are required to supply Last Name_First Name_Assignment2.py and all of the files used for the assignment with the resources folder in a zip format. Otherwise, the program will not run for your facilitator, which will be a deduction.
Notes
· Assignments can be submitted once. If extenuating circumstances exist, contact your facilitator.
· Start by identifying and ordering the objectives.
· There are no testing requirements for this assignment. However, it would be prudent to make sure your program does not crash and all input validation is performed correctly.
· Note the statement in the syllabus on timeliness of submissions (the gist being that all assignments must observe the deadlines). A ten point deduction per day late up to 20 points. After 2 days late, a 0 will be given. Please see the syllabus. This is waived ONLY with the prior approval of your facilitator.
Grading
Requirement Points
1 18 (i. thru vi. 3 points each)
2 4
3 23 (a thru d 5 points each, ei-iii 1 point each)
4 6 ( a – 2 points, bi-ii, 2 points each)
5 9 (a-c 3 points each)
6 4
7 6 ( a – 2 points, bi-ii, 2 points each)
8 10
9 20 – (a-d 4 points each, ei-ii 2 points each)