class - How to deal with global parameters in a scientific Python script -


i writing piece of scientific software in python comprises both poisson equation solver (using newton method) on rectangular mesh, , particle-in-cell code. i've written newton solver , particle-in-cell code separate functions, called main script.

i had written code 1 large script, decided break script more modular, , individual functions called on own. problem have large number of "global" variables consider parameters problem. includes problem constants , parameters define problem geometry , mesh (such dimensions, locations of boundary conditions, boundary conditions etc.).

these parameters required both main script , individual functions. question is: best way (and proper) store these variables such can accessed both main script , functions.

my current solution define class in separate module (parameters.py) so:

class parameters:     length = 0.008     width = 0.0014      nz = 160     nr = 28      dz = length/nz     dr = width/nr     ... 

in main script have:

from parameters import parameters  par = parameters()  coeff_a = -2 * (1/par.dr**2 + 1/par.dz**2) ... 

this method allows me use par container parameters can passed functions want. provides easy way set problem space run 1 of functions on own. concern each function not require stored in par, , hence seems inefficient passing forward time. remove many of parameters par, need recalculate them every time function called, seems more inefficient.

is there standard solution people use in these scenarios? should mention functions not changing attributes of par, reading them. interested in achieving high performance, if possible.

generally, when program requires many parameters in different places, makes sense come neat configuration system, class provides interface own code.

upon instantiation of class, have configuration object @ hand can pass around. in places might want populate it, in other places might want use it. in case, configuration object globally accessible. if program python package, configuration mechanism might written in own module can import other modules in package.

the configuration class might provide useful features such parameter registration (a code section says needs parameter set), definition of defaults , parameter validation.

the actual population of parameters based on defaults, user-given commandline arguments or user-given input files.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -