Exercise: create a module and reuse code from it (1 h)

Extend the exercise from today by applying what you've just learned about packages and code reusability.

Outline

  1. Put the function into a separate .py file
  2. Create yet another function that takes the name of the region as an input and returns SST values for the corresponding region
  3. Use import to access these functions from another file or a notebook
  4. Create the wind speed data
  5. Create a dictionary of data, whose keys are names of regions and values are lists of heat fluxes data
  6. Save the dictionary to text file (bonus: to a json file), both keys and values

1. Put the function into a separate .py file

  • Copy a code block with the "heat flux" function from the previous exercise
  • Open a Python script (not a notebook!), e.g. "my_awesome_module.py" in the current working directory
  • Paste the code block in the file and make sure there are no syntax mistakes
  • Congratulations, you've just created a module!

2. Create yet another function that takes the name of the region as an input and returns SST values for the corresponding region

  • This function can look something like the one below
  • Feel free to modify or extend it
  • You can replace region_name by experiment_name or whatever you prefer
  • For convenience, make sure the length of the returned list is the same

In [1]:
def create_sst(region_name):
    """
    Create fake SST data (degC) for a given region
    
    Inputs
    ------
    region_name: ...continue the docstring...
    
    n: integer, optional. Length of the returned data list      
    
    Returns
    -------
    ...continue the docstring...
    """
    
#     if region_name == 'NS':
#         # North Sea
#         sst = list(range(5, 15, 1))
#     elif region_name == 
#         # Sea of Tranquility
#         sst =
#     elif region_name ==
#         sst = 
#     else:
#         raise ValueError('Input value of {} is not recognised'.format(region_name))
        
#     return sst

Once you are happy with this new function, put it in the same module too.

3. Use import to access these functions from another file or a notebook


In [2]:
# import my_super_awesome_module

or


In [3]:
# from my_super_awesome_module import heat_flux_function, create_sst

4. Create the wind speed data

You can just copy the line from the previous exercise


In [4]:
# wind_speed =

5. Create a dictionary of data, whose keys are names of regions and values are lists of heat fluxes data

  • Create a list of names of the regions/experiments
  • Create an empty dictionary, named hf_dict or whatever sounds better to you
  • Loop over the names, call the create_sst() function and assign it to a variable, e.g. fake_sst
  • Still inside the name-loop, write another loop to iterate over SST and wind values, just as you did in the previous exercise, and calculate the heat flux.
  • Assign the result to the corresponding key of hf_dict

In [5]:
# Your code here...

Print the result to test yourself.

6. Save the dictionary to text file (bonus: to a json file), both keys and values

  • You can copy the code for writing data to a text file from the previous exercise
  • Modify it so that the output file would include hf_dict's keys as row (column) names

In [6]:
# with open(...

Bonus


In [7]:
import json

Explore what json.dump() does.