Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a python script (demographics.py) using the following guidelines and skeleton. Student demographic data for students from the class of 2013 through 2025 has

Please write a python script (demographics.py) using the following guidelines and skeleton.

Student demographic data for students from the class of 2013 through 2025 has been provided and looks something like this:

$ curl -sL URL | head -n 10 Year,Gender,Ethnic 2013,M,C 2014,M,C 2015,F,N 2016,F,N 2017,M,C 2018,M,C 2013,M,C 2014,M,S 2015,M,C 

As can be seen above, the demographics CSV data has three columns:

The first column represents the graduating class year that student belongs to.

The second column records the gender of the student and contains either M or F which stands for Male or Female respectively.

The third column records the ethnicity of the student and consists of the following mapping:

Letter Ethnicity
C Caucasian
O Asian
S Hispanic
B Black or African American
N Native American / Pacific Islanders
T Multiple Selection
U Undeclared

The demographics.py script takes the following arguments:

# Display usage message $ ./demographics.py -h Usage: demographics.py [options] [URL] -y YEARS Which years to display (default: all) -p Display data as percentages. -G Do not include gender information. -E Do not include ethnic information. 

The -y flag specifies which years to display (the default is to display all the years in the CSV data).

The -p flag forces the script to output data as percentages rather than raw counts.

The -G and -E flags suppresses outputting the gender and ethnic data respectively.

The user may specify a URL to the CSV file. If one is not specified then the script will use https://yld.me/raw/e2Ad as the URL.

Note, the years are always displayed in ascending order.

Examples

Here are some examples of demographics.py in action:

# No arguments $ ./demographics.py 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 ================================================================================================================ M 49 44 58 60 65 101 96 92 89 124 114 120 105 F 14 12 16 19 26 45 54 43 46 41 52 57 39 ---------------------------------------------------------------------------------------------------------------- B 3 2 4 1 5 3 3 4 6 4 2 4 7 C 43 43 47 53 60 107 96 92 87 106 98 110 82 N 1 1 1 7 5 5 13 14 13 14 16 11 12 O 7 5 9 9 12 10 13 7 8 14 11 22 13 S 7 4 10 9 3 13 10 10 11 17 26 19 18 T 2 1 1 0 6 8 15 7 9 8 11 7 8 U 0 0 2 0 0 0 0 1 1 1 2 4 4 ---------------------------------------------------------------------------------------------------------------- # Show percentages rather than raw counts $ ./demographics.py -p 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 ================================================================================================================ M 77.8% 78.6% 78.4% 75.9% 71.4% 69.2% 64.0% 68.1% 65.9% 75.2% 68.7% 67.8% 72.9% F 22.2% 21.4% 21.6% 24.1% 28.6% 30.8% 36.0% 31.9% 34.1% 24.8% 31.3% 32.2% 27.1% ---------------------------------------------------------------------------------------------------------------- B 4.8% 3.6% 5.4% 1.3% 5.5% 2.1% 2.0% 3.0% 4.4% 2.4% 1.2% 2.3% 4.9% C 68.3% 76.8% 63.5% 67.1% 65.9% 73.3% 64.0% 68.1% 64.4% 64.2% 59.0% 62.1% 56.9% N 1.6% 1.8% 1.4% 8.9% 5.5% 3.4% 8.7% 10.4% 9.6% 8.5% 9.6% 6.2% 8.3% O 11.1% 8.9% 12.2% 11.4% 13.2% 6.8% 8.7% 5.2% 5.9% 8.5% 6.6% 12.4% 9.0% S 11.1% 7.1% 13.5% 11.4% 3.3% 8.9% 6.7% 7.4% 8.1% 10.3% 15.7% 10.7% 12.5% T 3.2% 1.8% 1.4% 0.0% 6.6% 5.5% 10.0% 5.2% 6.7% 4.8% 6.6% 4.0% 5.6% U 0.0% 0.0% 2.7% 0.0% 0.0% 0.0% 0.0% 0.7% 0.7% 0.6% 1.2% 2.3% 2.8% ---------------------------------------------------------------------------------------------------------------- # Show only gender percentages for the years 2022, 2023, and 2024 (with explicit URL) $ ./demographics.py -y 2023,2024,2025 -E -p URL 2023 2024 2025 ================================ M 68.7% 67.8% 72.9% F 31.3% 32.2% 27.1% -------------------------------- 

Note, to pass the provided tests, you must match the spacing and formatting exactly. Only use spaces to align data (no \t) and make sure you have no trailing whitespace**.

Formatting hints are provided in the docstrings provided in the skeleton code.

Here is skeleton code you can use to start your demographics.py script:

import collections import os import sys import requests # Constants URL = URL TAB = ' '*8 GENDERS = ('M', 'F') ETHNICS = ('B', 'C', 'N', 'O', 'S', 'T', 'U') # Functions def usage(status=0): ''' Display usage information and exit with specified status ''' progname = os.path.basename(sys.argv[0]) print(f'''Usage: {progname} [options] [URL] -y YEARS Which years to display (default: all) -p Display data as percentages. -G Do not include gender information. -E Do not include ethnic information. ''') sys.exit(status) def load_demo_data(url=URL): ''' Load demographics from specified URL into dictionary >>> load_demo_data('URL').keys() dict_keys(['2013', '2014', '2015', '2016', '2017', '2018', '2019']) >>> load_demo_data('URL')['2013'] == {'M': 1, 'B': 2, 'F': 1, 'TOTAL': 2} True >>> load_demo_data('URL')['2019'] == {'M': 1, 'U': 2, 'F': 1, 'TOTAL': 2} True ''' return {} def print_demo_separator(years, char='='): ''' Print demographics separator Note: The row consists of the 8 chars for each item in years + 1. >>> print_demo_separator(['2012', '2013']) ======================== ''' print() def print_demo_years(years): ''' Print demographics years row Note: The row is prefixed by 4 spaces and each year is right aligned to 8 spaces ({:>8}). >>> print_demo_years(['2012', '2013']) 2012 2013 ''' print() def print_demo_fields(data, years, fields, percent=False): ''' Print demographics information (for particular fields) Note: The first column should be a 4-spaced field name ({:>4}), followed by 8-spaced right aligned data columns ({:>8}). If `percent` is True, then display a percentage ({:>7.1f}%) rather than the raw count. >>> data = load_demo_data('URL') >>> years = sorted(data.keys()) >>> print_demo_fields(data, years, GENDERS, False) M 1 1 1 1 1 1 1 F 1 1 1 1 1 1 1 ''' print() def print_demo_data(data, years=None, percent=False, gender=True, ethnic=True): ''' Print demographics data for the specified years and attributes ''' pass def print_demo_gender(data, years, percent=False): ''' Print demographics gender information ''' print_demo_fields(data, years, GENDERS, percent) print_demo_separator(years, '-') def print_demo_ethnic(data, years, percent=False): ''' Print demographics ethnic information ''' print_demo_fields(data, years, ETHNICS, percent) print_demo_separator(years, '-') def main(): ''' Parse command line arguments, load data from url, and then print demographic data. ''' arguments = sys.argv[1:] url = URL years = None gender = True ethnic = True percent = False # Main Execution if __name__ == '__main__': main() 

Hints

Read the TODO comments carefully. DO NOT REMOVE THE doctests!

load_demo_data: To fetch the demographic data, you can use the requests.get method. You can then access the raw data via the text property of the object returned by requests.get. You will then need to organize the CSV data into a dictionary of the following format:

{"year": {"gender": count, "ethnic": count, "TOTAL": count}} 

print_demo_separator: To display a separator line, this function should print 8 chars for each year + 1. Consider using string multiplication:

print(' '*8) # Prints 8 spaces 

print_demo_years: To display the years, this function should use the start with a string with 4 spaces and then concatenate to this string each year formatted so it is right-aligned to 8 spaces. You can accomplish alignment by doing the following:

# Print the value of variable aligned to the right and taking up at least 8 spaces print(f'{variable:>8}') 

print_demo_fields: To display all the rows for a given list of fields, this function should build a row of text column by column. Special care must be taken to display percentages versus raw counts.

print_demo_data: To display the table of data, this function should call the print_demo_years and print_demo_separator functions, followed by the print_demo_gender and print_demo_ethnic functions if necessary.

print_demo_gender: This function simply calls the print_demo_fields and print_demo_separator functions and is provided to you.

print_demo_ethnic: This function simply calls the print_demo_fields and print_demo_separator functions and is provided to you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions