I start up sessions in Jupyter with Python with a partly tailor-made script for the application I work with. The script contains both application dependent dictionaries and functions, but some function are of a general kind. I would like to make a general module and make the start-up script contain only application parts. The difficulty is that I want the functions in the general module have application dictionaries as default. So how to connect such workspace dictionaries to the imported functions?
A very simplifed example below illustrate the problem. First you see the original total startup script. This codes works.
import numpy as np
import matplotlib.pyplot as plt
parDict = {}
parDict['Y'] = 0.5
parDict['qSmax'] = 1.0
parDict['Ks'] = 0.1
def par(parDict=parDict, *x, **x_kwarg):
""" Set parameter values if available in the predefined dictionaryt parDict. """
x_kwarg.update(*x)
x_temp = {}
for key in x_kwarg.keys():
if key in parDict.keys():
x_temp.update({key: x_kwarg[key]})
else:
print(key, 'seems not an accessible parameter')
parDict.update(x_temp)
And I can in the notebook give a command like par(Y=0.4) and then inspect the results in the dictionary parDict.
Second (below) you see an attempt to break out the general functions into a module and this functions are imported in the start-up script. And below the actual module. This code does not work. The error message is: name 'parDict' is not defined How to fix it?
parDict = {}
parDict['Y'] = 0.5
parDict['qSmax'] = 1.0
parDict['Ks'] = 0.1
from test_module import par
And test_module.py
def par(parDict=parDict, *x, **x_kwarg):
""" Set parameter values if available in the predefined dictionaryt parDict. """
x_kwarg.update(*x)
x_temp = {}
for key in x_kwarg.keys():
if key in parDict.keys():
x_temp.update({key: x_kwarg[key]})
else:
print(key, 'seems not an accessible parameter')
parDict.update(x_temp)
If I in the function take away the default argument parDict then it works, but I must then have a lengthier call like par(parDict, Y=0.4). I would like avoid this lengthy call and provide the default parDict automatically. One idea is to in the start-up script make a new function from the imported function and here make the connection to the dictionary. But seems a clumsy way to do it, or the only choice?