Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DATE FORMATS Date formats vary across countries and result in various challenges, whether entering dates in Excel or printing values in a database. For example,

DATE FORMATS Date formats vary across countries and result in various challenges, whether entering dates in Excel or printing values in a database. For example, what does 03/07/02 mean? It could mean March 7th, 2002 or July 3rd, 2002 or July 2nd 2003 depending on the date format system. In this assignment, you will be processing dates and writing operations for them. You should ignore leap years for all functions (i.e., FEB always has 28 days). The dates are stored in an input file that has zero or more lines in the following format: MONTH,DAY,YEAR following the U.S. standard. Interestingly, the United States is the only country that uses this format. You will convert each row to a date tuple represented by (month, day, year) where month is a string and day and year are integers. Example: the data March 30, 2023 is represented by the tuple (MAR, 30, 2023). Operations You must create the following functions in the file hw2.py. processFile Parameters: Name of the input file (string) Returns: A list of date tuples. The function is also capable of throwing exceptions (see description). Description: Reads and parses the input file. Creates and returns a list of date tuples. Each line in the file represents a single date and the order of the items in the list must match the order in the file. A valid line representing a time consists of these three fields, in this order, separated by commas: 1. month (string from JAN to DEC) 2. day (integer from 1-31, can have a leading zero such as 09) 3. year (integer from 1950 to 2023, inclusive) The line is invalid if any of the following are true: incorrect number of fields a field has the wrong type a field has the correct type but an unacceptable value (such as Feb 31 or Dec 50 or year 1800). This function must also perform error checking by throwing the following programmer-defined exceptions: EmptyFileError: The file is empty. ImproperDateError: This exception is raised if any of the dates are invalid.

The function also can throw the system-defined exception FileNotFoundError if the file does not exist. This exception is automatically thrown by open so no additional work by the programmer is necessary. Other exceptions may be possible, but they are not relevant for this assignment. IMPORTANT: This function only throws the exceptions. It does not catch them. The exceptions are caught in the top-level functionality. dateConvertGen(dateList, dateFormat): Parameters: dateList list of date tuples from processFile dateFormat a string indicating the date format, optional argument with a default value American Returns: A generator object. Description: This function is a generator function that will yield for each date tuple in dateList, a string representation of the date. American: mm-dd-yyyy (ex. 01-23-2023) International: dd-mm-yyyy (ex. 23-01-2023) Asian: yyyy-mm-dd (ex. 2023-01-23) In any other format is entered for dateFormat, use the American format. compare(date1, date2): Parameters: date1, date2 two date tuples Returns: An integer. Description: This function compares two dates to check which date is earlier (i.e., older). Used as a helper function for other parts of the program. Returns -1 if date1 is earlier, 0 if the dates are equal, and 1 if date2 is earlier. calcDifference(date1, date2): Parameters: date1, date2 two date tuples Description: This function calculates the difference between two dates. Returns: A tuple with three integers (years, months, days) This function must also perform error checking by throwing the following programmer-defined exceptions: InvalidPairError: This exception is raised if date2 is earlier than date1. Example usage Date 1 Date 2 Difference (Y, M, D) ("JUL", 3, 2023) ("SEP", 10, 2023) (0, 2, 7) ("JAN", 18, 2020) ("FEB", 8, 2020) (0, 0, 21) ("AUG", 10, 1995) ("DEC", 15, 1997) (2, 4, 5) ("AUG", 10, 1995) ("JAN", 5, 1998) (2, 4, 26) ("AUG", 10, 1995) ("DEC", 15, 1993) INVALID PAIR Main program The program has the following top-level functionality:

1. Get the name of an input file from the command line (using sys.argv). WARNING: Do not prompt the user for a file name. 2. Call processFile in the input filename and store the result in dateList. 3. Create exception handlers for the EmptyFileError, ImproperTimeError, and FileNotFoundError exceptions. Each exception must have their own exception handler that prints an error message specific to the type of exception and exits the program. 4. Print the dateList as a list of strings using a single list comprehension that shows each date tuple as a string in the format JAN 2, 2023. 5. Call dateConvertGen with dateList from step 2 (and no other argument) and store it as AmGen. 6. Print the first item generated by AmGen. 7. Call dateConvertGen with dateList from step 2 and International as arguments. Store it as IntGen. 8. Print all the values from IntGen from step 7 until the generator runs out of values. Catch StopIteration with a specific error message and any other exception that may occur with a general error message. 9. In a single line, find the most recent year in dateList. This line should include list comprehension. Print the result in the format Most recent year: 1989 10. Using a single call to sorted, print the dates in ascending order (earliest date to the most recent date). To accomplish this, you will need to import cmp_to_key from functools. You can use other functions to assist but you must use sorted (only once) to sort the list. 11. Create a variable target that is the last entry in dateList. 12. Use a single list comprehension to create a list of differences between each value in dateList and target. Print this list. 13. Compare all dates in dateList with January 1, 2000 as date2 using list comprehension. The result should be a list of -1, 0, and 1s. Notes: You may NOT import any module or library for this assignment except for the import specified for steps 1 and 10. You may create other functions and variables beyond what is listed here. The input file may be empty. Write the driver accordingly. Test your code with additional input files to check the exception handling. If your program is working correctly, it should have the following output for these steps: Step 4 (result of processFile): ['SEP 1, 2000', 'SEP 17, 1960', 'DEC 28, 1991', 'MAR 30, 2023'] Step 6 (result of dateConvertGen with a default value): 09-01-2000 Step 8 (result of dateConvertGen with 'International'): 01-09-2000 17-09-1960 28-12-1991 30-03-2023 No more dates Step 9: Most recent year: 2023 Step 10 (result of sorted): [('SEP', 17, 1960), ('DEC', 28, 1991), ('SEP', 1, 2000), ('MAR', 30, 2023)] Step 12 (result of calcDifference): [(22, 6, 29), (62, 6, 13), (31, 3, 2), (0, 0, 0)] Step 13 (result of compare): [1, -1, -1, 1] IMPORTANT! Do not assume that testing is complete if your program produces the correct output for this provided input file. During grading, your assignment will be tested with other input files. Additional notes: Note that a test that crashes due to a run-time exception will be considered a failing test. Programs that contain syntax errors will receive a zero. Up to 5 points may be deducted if the program is very unreadable. Up to 40 points may be deducted if your program uses the datetime library for this assignment. 6 point deduction if the file name is not obtained from the command line for part 1.

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

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions