Question: Q 1 : Date Parser with Validation File: q 1 . py Implement a function called parse _ date ( year _ str , month
Q: Date Parser with Validation
File: qpy
Implement a function called parsedateyearstr monthstr daystr that:
Attempts to convert yearstr monthstr and daystr into integers.
Validates that the year is between and
Validates that the month is between and
Validates that the day is valid for the given month and year eg February should not be allowed
Returns the date as a string
Use Python's datetime module to attempt creating a datetime.date object. This will naturally raise a ValueError for invalid dates, which you should catch and reraise with a more descriptive message.
Example Usage of datetime.date function:
year month day date datetime.dateyear month, day printdate # Output:
Error Conditions:
If any of yearstr monthstr or daystr cannot be converted to an integer, raise a ValueError with the message:
"Year, month, and day strings must contain integers."
If year is outside the range raise a custom exception YearRangeError with the message:
"Year must be between and
If the date is invalid eg April or month or day raise a ValueError with the message:
"Invalid date specified."
If Successful:
Return the date as a string in the format YYYYMMDD
Example Usage:
parsedate # returns parsedate # raises YearRangeErrorYear must be between and parsedate # raises ValueErrorInvalid date specified." parsedate "Feb", # raises ValueErrorYear month, and day must be integers."
Q: TabDelimited File Validator
File: qpy
Implement a function validatetabfilefilepath, expectedfields, requiredpattern that:
Attempts to open and read a file linebyline.
Checks that each line contains exactly expectedfields fields, separated by tabs t
Confirms that at least one line contains a field that matches the given requiredpattern a substring
You will create and use a custom exception TabFileValidationError to handle validation failures.
Behavior:
If the file is not found, return "File not found."
If any line does not have the correct number of fields expectedfields raise TabFileValidationError.
Include in the exception message:
The filepathThe line number based index of the first offending lineThe discrepancy in the number of fields found vs expected
If none of the lines contains a field that includes requiredpattern as a substring, raise TabFileValidationError indicating the missing pattern.
Normal Output:
If all criteria are met, return "Tabdelimited file validation passed."
Custom Exception Details:
TabFileValidationError should provide clear, detailed information. For example:
If a line is malformed:
"File path: data.txt Line : Expected fields, found If the required pattern is missing:
"File path: data.txt No field contains required pattern 'userid
FileNotFoundError should provide:
File Not Found. If the file does not exist.
Example Usage:
# Suppose data.txt contents are: # name age country email # John Canada john@example.com # Alice USA alice@example.com validatetabfiledatatxt expectedfields requiredpattern"example" # returns "Tabdelimited file validation passed." validatetabfiledatatxt expectedfields requiredpattern"missingpattern" # raises TabFileValidationErrorFile path: data.txt No field contains required pattern 'missingpattern'." validatetabfilemissingfile.txt "example" # returns "File not found." # If data.txt had a line with fewer fields, for example: # John Canada validatetabfiledatatxt "example" # raises TabFileValidationErrorFile path: data.txt Line : Expected fields, found
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
