Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

User-defined "global" variable in a python package

$
0
0

I wrote a package that does all sorts of calculations and also provides several plotting functions with options to save the plots as .jpg or .png in a folder set by the user. I ensure that the folder exists with a decorator:

# plots.pyfrom my_package.params import out_pathdef mkdir_plots(func):    @functools.wraps(func)    def wrapper_mkdir_plots(*args, **kwargs):        if not os.path.exists(out_path):            os.makedirs(out_path)        func(*args, **kwargs)    return wrapper_mkdir_plots@mkdir_plotsdef plot_fun1(vars):    # ...    plt.savefig(os.path.join(out_path, fname))
#params.pyout_path = 'plots'def set_outpath(fname):    global out_path    out_path = fname

The function set_outpath() is supposed to allow user to define the folder name themselves. The function set_outpath() sets global variable params.out_path to anything I like but the plotting module will always use 'plots'.

Ideally, the variable out_path set by the user is accessible not only from within the plotting module (plot.py) but also from other modules that are part of my package (calc.py), although this is not a must.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>