Closed hfboyce closed 3 years ago
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
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"
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"
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
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"
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
]
Module 7 Screen recordings
1. Working with other files - moving functions into your notebook
.py
file so that we can use them again.exponent_a_list
(In the comment here) and save it as a.py
file in your folder.exponent_a_list
into the notebook.?exponent_a_list
.2. Working with other files - pytest
exponent_a_list
function back in Module 6 (in the comment here). Explain that we can save these in a.py
file like we did in our functionexponent_a_list
.exponent_a_list
into the test.py
file like we did in the previous section and save it as a new.py
file.pytest
.!pytest filname.py
-This shows them that the tests (hopefully all pass). (explains why we separate our functions into separate tests)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
Ok so my idea for this is to first explain in a few regular slide what kinds of formatting should be used and what is good standards which you can record like a regular slide decks.
Then after that, start a new recording where you implement flake8 and black. I think that will work better.
Take a file with bad code (This code here from Tom's lecture 4 that i've pasted here)
Show them the file and where it's saved.
In a Jupyter notebook check for all the "grammar" mistakes using
Next show them the tool
black
and how it follows a slightly different styling guide.This tool can directly change and format your code using
Note that you may need to explain flake8 takes a very specific file path (in the assignment it required the code:
!flake8 /home/jupyter/source/assignment7/sampling.py
Show them the code that's been changed with black formatting
Explain that since pep8 and black follow different style guides, if we rerun flake8, there still may be code that isn't being formatted by the
black
style.