Extend the exercise from today by applying what you've just learned about packages and code reusability.
import
to access these functions from another file or a notebook1. 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
region_name
by experiment_name
or whatever you prefer
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
hf_dict
or whatever sounds better to youcreate_sst()
function and assign it to a variable, e.g. fake_sst
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
hf_dict
's keys as row (column) names
In [6]:
# with open(...
Bonus
In [7]:
import json
Explore what json.dump()
does.