0

I have the following json input parameters file

#input.json
{
        "nx": 401, 
        "ny": 401,
        "T" : 10,
        "nt" : 20,
        "D" : 0.2,
        "Sgma": 0.243,
        "M": 0.0052
}

that is passed onto different python scripts, for eg.

#test1.py
import numpy,os

def simple(nx,ny,D,Sgma, M, T,nt):  #parameters as function arguments
    k = Sgma + 0.02
    print("this is domain size", ny)
    print("this is dif time", T)
    print("this is K param", k)
#test2.py
import numpy,os

def simple_n(nx,ny,D,Sgma,M,T,nt):  #parameters as function arguments
    k = M + 0.02
    print("this is domain size", ny)
    print("this is sim time", D)
    print("this is K param", k)

I execute both the above python scripts through a main.py that passes the input parameters with argparse.

As you can see, only some, not all of the parameters are required in individual scripts. My question: Is there a way to pass only the required arguments in simple() ?

For example: def simple(ny,T,k) for test1.py and def simple(ny,D,k) for test2.py rather than def simple(nx,ny,D,Sgma, M,T,nt). Since I will be dealing with large sets of parameters as initial config, I would like to know an efficient way to pass the initial parameters in different python codes as function arguments. Thanks

newstudent
  • 402
  • 6
  • 19
  • Does this answer your question? [How do I define a function with optional arguments?](https://stackoverflow.com/questions/9539921/how-do-i-define-a-function-with-optional-arguments) – finngu Jul 20 '22 at 10:01

1 Answers1

1

You can use **kwargs for that. It is a pattern often used in python where you pass named parameters to a function and inside the function you access the values you need.

def simple(**kwargs):
    ny = kwargs.get('ny', None)

    if ny is None:
        print("Parameter 'ny' is required.")

# Call the function with only required arguments
simple(ny=1, D=15, nt=2)

Or you could add default values to your function and pass values by their name.

def simple(nx=0, ny=0, D=0, Sgma=0, M=0, T=0, nt=0):
    pass

# Call the function with only required arguments
simple(ny=1, D=15, nt=2)

UPDATE

a = {"foo": "bar", "baz": 1}
def simple(foo=None, bar=None, baz=None):
    print(foo)
    print(bar)
    print(baz)

>>> simple(**a)
bar
None
1
finngu
  • 457
  • 4
  • 23
  • so for every parameter nt,nx etc. I need to specify like `ny = kwargs.get('ny', None)` and give the values `simple(ny=1, D=15, nt=2)` for the required parameters in the function ? – newstudent Jul 20 '22 at 11:11
  • Exactly. Instead of `None` in my example, you could pass any default value. The `get()` function will either return the value for the key passed as first argument or return the default passed as second argument. – finngu Jul 20 '22 at 11:39
  • Thanks, but I would like to use the `json file` for all default parameters and not specify it in `test1.py`, as I would not like the user to go into complex codes to provide/modify defaults parameters or functions – newstudent Jul 20 '22 at 12:24
  • I don't really understand your question. If your function `simple()` has default values for all parameters, you can pass it only those, that you really require. Then the problem would only be getting these from JSON, which you can do by passing it as a dict. See my updated answer. – finngu Jul 20 '22 at 12:43