Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Date Arithmetic Operations date_arithmetic()-> Tuple[datetime, datetime,int] Date arithmetic is far more difficult than you might imagine. For example, what is the date three

Part 1: Date Arithmetic Operations

date_arithmetic()-> Tuple[datetime, datetime,int]

Date arithmetic is far more difficult than you might imagine. For example, what is the date three days after Feb 27, 2020? It's not Feb 30. Did February 2020 have 28 or 29 days? Given two arbitrary dates, how many days are between those two dates?

Make a functiondate_arithmeticto use Python'sdatetimemodule to answer the following questions:

  1. What is the date three days afterFeb 27, 2020?
  2. What is the date three days afterFeb 27, 2019?
  3. How many days passed betweenFeb 1, 2019and Sept 30, 2019?

IMPORTANT: The function should return atuplewith three values in the following order:

  1. An instance ofclassdatetimerepresenting the date three days afterFeb 27, 2020.
  2. An instance ofclassdatetimerepresentingthe date three days afterFeb 27, 2019.
  3. Anintrepresenting the number of days betweenFeb 1, 2019andSept 30, 2019

Here below I have some pseudo code as a hint:

from datetime import datetime, timedelta def date_arithmetic() -> Tuple[datetime, datetime, int]: """ Code segment demonstrating expected return values. """ three_days_after_02272020: datetime = # your code goes here for calculation three_days_after_02272019: datetime = # your code goes here for calculation days_passed_02012019_09302019: int = # your code goes here for calculation return three_days_after_02272020, three_days_after_02272019, days_passed_01012019_09302019 

Note that the variable assignments are not mandatory, as long as you follow the PEP8 naming convention.

You may just call the appropriatedatetimemethods to answer the questions.

Part 2: field separated file reader

file_reader(str, int, str, bool) -> Iterator[List[str]]

Reading text files with a fixed number of fields, separated by a specific character, is a common task. For example, .CSV files (comma separated values) are common in Windows applications while Unix data files are typically separated by the '|' (vertical bar) character.

Make ageneratorfunctionfile_reader()to read field-separated text files and yield a tuple with all of the values from a single line in the file on each call tonext(). Your generatorMUSTmeet the following requirements:

  • You must implement a generator that reads the fileone line at a time,splits the line into fields on the separator,and yields a list of strings with each of the tokens in that line.You shouldNOTread the entire file into memory.
  • The generator definition should include following parameters:
  • path: str- the path of the file to be read (Do not hardcode the file path because we'll reuse this function many times in future assignments)
  • fields: int- the number of fields expected in each line
  • sep: str- anoptionalparameter to specify the field separator thatdefaults to comma (',')(that wasn't intended as an emoji...)
  • header: bool- anoptionalboolean parameter thatdefaults toFalseto specify if the first line in the file is a header line
  • The generator should raise aFileNotFoundexception if the specified file can't be opened for reading along with a meaningful message to help the reader to understand the problem
  • The generator should raise aValueErrorexception if a line in the file doesn't have exactly the expected number of fields, e.g. "ValueError: 'foo.txt' has 3 fields on line 26 but expected 4".The exception message should include:
  • the file name,e.g. 'foo.txt'
  • the line number in the file where the problem occurred
  • the number of fields found in the line
  • the number of fields expected in the line
  • Thenfields from each line should be yielded as alist of strings
  • If the call to the generator specified a header row, then the first call for data should return the second row from the file, skipping over the header row after checking that the header row has the expected number of fields in the header

Here's a sample file, student_majors.txt, that includes a header row and three fields, separated by '|': CWID, name, and major:

CWID|Name|Major 123|Jin He|Computer Science 234|Nanda Koka|Software Engineering 345|Benji Cai|Software Engineering 

Here's sample code using the generator to read a vertical bar separated file with a header row and 3 fields:

for cwid, name, major in file_reader(path, 3, sep='|', header=True):  print(f"cwid: {cwid} name: {name} major: {major}") 

Note: Be sure to include automated tests with at least one test file, and include your test file in your submission.

Hint: The code is much shorter than the description of the requirements

Part 3: Scanning directories and files

class FileAnalyzer

Python is a great tool for automating a number of tasks, including scanning and summarizing the files in a file system.

Make a Python class,FileAnalyzerthat given a directory name, searches that directory for Python files (i.e.files ending with.py). For each .py file in the directory, open each file and calculate a summary of the file including:

  • the file name
  • the total number of lines in the file
  • the total number of characters in the file
  • the number of Python functions (lines that begin with 'def ', including methods inside class definitions)
  • the number of Python classes (lines that begin with 'class ')

Generate a summary report with the directory name, followed by a tabular output that looks similar to the following:

The classFileAnalyzershould haveat leastthese attributes and these methods listed below:

  • Attributes:
  • self.files_summary: Dict[str, Dict[str, int]], a Python dictionary that stores the summarized data for the given file path. Thekeys of dictionary will be thefilenameof a python file, thevalueof each key will be adictas well.The value dictionary will have 4 key-value pairs that represent 4 data points we collect for each file. The structure ofself.files_summarywill look like below:
 { 'filename_0.py': { 'class': # number of classes in the file 'function': # number of functions in the file 'line': # number of lines in the file 'char': # number of characters in the file }, ... } 
  • Methods:
  • self.analyze_files(self) -> None: a method that populate the summarized data intoself.files_summary. Note that youMUST NOTpass any argument to this method, the given directory path is better stored as an attribute - there will be no naming requirement for this variable but I personally useself.directory.
  • self.pretty_print(self) -> None: a method that print out the pretty table from the data stored in theself.files_summary. Note that youMUST NOTpass any argument, other than self, to this method.

Note: Your should execute theself.analyze_filesin theself.__init__but youMUST NOTexecute theself.pretty_printin theself.__init__.

Keep in mind that you may see a file, but you may not be able to read it. In that case,raise aFileNotFoundexception if the specified file can't be opened for reading

Your program should use exceptions to protect the program against unexpected events.

ThePrettyTable module(Links to an external site.)

provides an easy way to create tables for output. You should use PrettyTable to format your table. See the lecture notes for an example.

You'll need to think about how to test this program with unittest. You do not need to automatically verify the output from the table but you should validate the various numeric values, e.g.number of classes, functions, lines, and characters. Your automated test should validate the counts for each file in the test directory.

Hints:

  1. Python'sosmodule has several helpful functions, e.g.os.listdir(directory)lists all of the files in the specified directory. Note that the file names returned byos.listdirdoNOTinclude the directory name.
  2. os.chdir(directory)will change the current directory to the specified directory when your program runs.
  3. Be sure to handle the cases where the user enters an invalid directory name
  4. Test your code against the sample files in Canvas
  5. Be sure to use unittest to test your program and follow the PEP-8 coding standards.

Deliverable

File Structure

Separate your code into two files- one for code logic and one for unit test. It's always a good practice to separate the code and the test.

You are going to havetwo .py files, one for logic code, one for test code

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Find the binomial coefficient. 1. 5C3 2. 7C6 3. 12C0 4. 20C20

Answered: 1 week ago