UBC-MDS / programming-in-python-for-data-science

https://prog-learn.mds.ubc.ca/
Other
20 stars 22 forks source link

Module 7 - Tiffany #58

Closed hfboyce closed 3 years ago

hfboyce commented 4 years ago

Module 7 Screen recordings

1. Working with other files - moving functions into your notebook

2. Working with other files - pytest

If you want to show them a function that doesn't work for all the tests, you can show them this function and these tests located in this comment that will result in an error (hopefully).

3 Style Guides and Enforcement

hfboyce commented 4 years ago

Function they have seen before:

def exponent_a_list(numerical_list, exponent=2):
    """
    Creates a new list containing the square values of the input list. 

    Parameters
    ----------
    numerical_list : list
        The list from which to calculate squared values 

    Returns
    -------
    list
        A new list containing the squared value of each of the elements from the input list 

    Raises
    ------
    TypeError
        If the input argument numerical_list is not of type list

    Examples
    --------
    >>> exponent_a_list([1, 2, 3, 4])
    [1, 4, 9, 16]

    """
    if not isinstance(numerical_list, list):
        raise TypeError("You are not using a list for the numerical_list input.")

    if not all(type(element) is int for element in numerical_list):
        raise TypeError("The elements in numerical_list must all be of type int")

    new_exponent_list = list()
    for number in numerical_list:
        new_exponent_list.append(number ** exponent)
    return new_exponent_list
hfboyce commented 4 years ago

Tests they have seen before

assert exponent_a_list([1, 2, 4, 7], 2) == [1, 4, 16, 49], "incorrect output for exponent = 2"
assert exponent_a_list([1, 2, 3], 3) == [1, 8, 27], "incorrect output for exponent = 3"
assert exponent_a_list([], 3) == [], "incorrect output for empty list"
assert exponent_a_list([1, 12, 3], 0) == [1, 1, 1], "incorrect output for exponent 0"
assert exponent_a_list([1, 2], -2) == [1, 0.25], "incorrect output for a negative exponent"

Tests in separate functions

def exponent_2_test():
    assert exponent_a_list([1, 2, 4, 7], 2) == [1, 4, 16, 49], "incorrect output for exponent = 2"

def exponent_3_test(): 
    assert exponent_a_list([1, 2, 3], 3) == [1, 8, 27], "incorrect output for exponent = 3"

def empty_list_test():
    assert exponent_a_list([], 3) == [], "incorrect output for empty list"

def exponent_0_test():
    assert exponent_a_list([1, 12, 3], 0) == [1, 1, 1], "incorrect output for exponent 0"

def exponent_neg_test():
    assert exponent_a_list([1, 2], -2) == [1, 0.25], "incorrect output for a negative exponent"
hfboyce commented 4 years ago

Function that doesn't pass tests

def bad_function(numerical_list, exponent=2):
    new_exponent_list = [numerical_list[0] ** exponent] # seed list with first element
    for number in numerical_list[1:]:
        new_exponent_list.append(number ** exponent)
    return new_exponent_list

Tests

assert bad_function([1, 2, 4, 7], 2) == [1, 4, 16, 49], "incorrect output for exponent = 2"
assert bad_function([2, 1, 3], 3) == [8, 1, 27], "incorrect output for exponent = 3"
assert bad_function([], 2) == [], "empty list"
hfboyce commented 4 years ago

Bad Code

x = {  'a':37,'b':42,
'c':927}
very_long_variable_name = {'field': 1,
                        'is_debug': True}
this=True

if very_long_variable_name is not None and very_long_variable_name["field"] > 0 or very_long_variable_name['is_debug']:
 z = 'hello '+'world'
else:
 world = 'world'
 a = 'hello {}'.format(world)
 f = rf'hello {world}'
if (this): y = 'hello ''world'#FIXME: https://github.com/python/black/issues/26
class Foo  (     object  ):
  def f    (self   ):
    return       37*-2
  def g(self, x,y=42):
      return y
# fmt: off
custom_formatting = [
    0,  1,  2,
    3,  4,  5
]
# fmt: on
regular_formatting = [
    0,  1,  2,
    3,  4,  5
]