Add sample_module.py to the project
The file sample_module.py serves as an example in notebook 02 as to how to create a local module and then import it.
This commit is contained in:
parent
22a85bc2ba
commit
c3fc760e70
1 changed files with 75 additions and 0 deletions
75
sample_module.py
Normal file
75
sample_module.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
"""This is a sample module.
|
||||
|
||||
It defines three functions average(), average_evens(), and average_odds().
|
||||
The point is to show how we can put Python code in a .py file to be re-used
|
||||
in some other place.
|
||||
|
||||
We should never forget to document the code as well, both on the module
|
||||
level (i.e., this docstring) but also in every function it defines.
|
||||
|
||||
When imported, Python modules are executed top to bottom before the flow of
|
||||
execution returns to wherever they were imported into.
|
||||
|
||||
An important convention is to prefix variables and functions that are not to
|
||||
be used outside the module with a single underscore "_". This way, we can
|
||||
design the code within a module in a modular fashion and only "export" what we
|
||||
want.
|
||||
|
||||
Here, all three functions internally forward the computation to an internal
|
||||
utility function _scaled_average() that contains all the logic common to the
|
||||
three functions. Also, we define one _default_scalar variable that is used as
|
||||
the default for the scalar parameter in each of the functions.
|
||||
|
||||
While this example is stylized, it shows how Python modules are often
|
||||
designed.
|
||||
"""
|
||||
|
||||
_default_scalar = 1
|
||||
|
||||
|
||||
def _scaled_average(numbers, scalar):
|
||||
"""Internal utility function to calculate scaled averages."""
|
||||
average = sum(numbers) / len(numbers)
|
||||
return scalar * average
|
||||
|
||||
|
||||
def average(numbers, *, scalar=_default_scalar):
|
||||
"""Calculate the average of all numbers in a list.
|
||||
|
||||
Args:
|
||||
numbers (list): list of numbers; may be integers or floats
|
||||
scalar (float, optional): the scalar that multiplies the
|
||||
average of the even numbers
|
||||
|
||||
Returns:
|
||||
float: (scaled) average
|
||||
"""
|
||||
return _scaled_average(numbers, scalar)
|
||||
|
||||
|
||||
def average_evens(numbers, *, scalar=_default_scalar):
|
||||
"""Calculate the average of all even numbers in a list.
|
||||
|
||||
Args:
|
||||
numbers (list): list of numbers; may be integers or floats
|
||||
scalar (float, optional): the scalar that multiplies the
|
||||
average of the even numbers
|
||||
|
||||
Returns:
|
||||
float: (scaled) average
|
||||
"""
|
||||
return _scaled_average([n for n in numbers if n % 2 == 0], scalar)
|
||||
|
||||
|
||||
def average_odds(numbers, *, scalar=_default_scalar):
|
||||
"""Calculate the average of all odd numbers in a list.
|
||||
|
||||
Args:
|
||||
numbers (list): list of numbers; may be integers or floats
|
||||
scalar (float, optional): the scalar that multiplies the
|
||||
average of the even numbers
|
||||
|
||||
Returns:
|
||||
float: (scaled) average
|
||||
"""
|
||||
return _scaled_average([n for n in numbers if n % 2 != 0], scalar)
|
Loading…
Reference in a new issue