Question
Instructions
Starting from your local repository's master branch, create a branch called exceptions-and-testing. This is branch you will use to complete this assignment. You will submit this branch as a pull request and use the pull request review feature to have others review your assignment. You will merge this branch into the master branch once you have completed the assignment. If you have additional questions, follow the instructions for submitting a Github repository in the Submitting Assignments document. This assignment uses code reviews, so make sure you have two people review your code.

At the end of this assignment, you will submit two files, one called util.py contains the implementation of the the utility functions described below, and one called test_util.py that implements the unit tests for those functions. Add these files to repository you created in an earlier. The test_util.py file should contain a if __name__ == '__main__' statement that runs the unit tests when run as a script. Use Python's unittest framework and use a single TestCase for all of the unit tests.

1. Integer conversion function
Create an integer conversion function to_int that converts an input value into an integer. If the function cannot convert the input value, it should return None. This function should only use exceptions and not if/elif/else statements or type checking.

2. Get value from sequence
Create a function called get_value that takes as arguments a series of items and a value. If the items are a list or tuple, the function should return the integer index of where the value is in the sequence. If the items are a dictionary, it should return the value associated with the input key. The function should return None if no value is found.

This function should only use exceptions and not if/elif/else statements or type checking. Below is an example of the expected output from this function.

In [1]: x = {'a': 1, 'b': 52, 'd': 6}
y = ['a', 'c', 'd']

print(get_value(x, 'q'))
Out[1]: None

In [2]: print(get_value(x, 'a'))
Out[2]: 1

In [3]: print(get_value(x, 'b'))
Out[3]: 52

In [4]: print(get_value(y, 'b'))
Out[4]: None

In [5]: print(get_value(y, 'd'))
Out[5]: 2

3. Unit Tests
3.1 Unit test script
Create script called test_util.py which contains a if __name__ == '__main__' and implements a unittest framework TestCase.

3.2 Implement Unit Test for to_int
Write unit tests for the to_int function defined in the previous problem. Use a different unit test for testing each type of input (e.g. test_float_str, test_invalid_str, test_int_input).

3.3 Implement Unit Test for get_value
Write unit tests for the get_value function defined in the previous problem. Use the examples provided in the problem as test cases. As a rule, write a separate unit test for each unique thing you are testing. As an example, you might have two unit test functions, test_valid_dict_input and test_dict_missing_key, to test dictionary based inputs. You should also test for valid and missing list inputs.

3.4 Implement Unit Test for get_date_joined
Write unit tests for the get_date_joined function defined in one of the previous lessons. Test various input strings and years to make sure it returns the correct date. Also test missing and invalid inputs.

4. Code Review
This assignment requires a code review with two reviewers.
Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

import datetime

'''
to_int function
'''
def to_int(input_value):
    try:
       return int(float(input_value))
    except ValueError:
       return None

'''
get_value function
'''
def get_value(items, value):
    try:
       return items.index(value)
    except:
       try:
            return items[value]
       except:
            return None

'''
get_date_joined function
'''

def get_date_joined(year_string, month_string):
    try:
       year = int(year_string)
    except:
       return None
This is only a preview of the solution.
Please use the purchase button to see the entire solution.
By purchasing this solution you'll be able to access the following files:
Solution.py
SolutionTest.py
Purchase Solution
$45.00
Google Pay
Amazon
Paypal
Mastercard
Visacard
Discover
Amex
View Available Computer Science Tutors 641 tutors matched
Ionut
(ionut)
Master of Computer Science
Hi! MSc Applied Informatics & Computer Science Engineer. Practical experience in many CS & IT branches.Research work & homework
5/5 (6,803+ sessions)
1 hour avg response
$15-$50 hourly rate
Pranay
(math1983)
Doctor of Philosophy (PhD)
Ph.D. in mathematics and working as an Assistant Professor in University. I can provide help in mathematics, statistics and allied areas.
4.6/5 (6,660+ sessions)
1 hour avg response
$40-$50 hourly rate
Leo
(Leo)
Doctor of Philosophy (PhD)
Hi! I have been a professor in New York and taught in a math department and in an applied math department.
4.9/5 (6,421+ sessions)
2 hours avg response

Similar Homework Solutions