-1

I have to call a function in python like this

job = scheduler.add_job(job_func, 'interval', minutes=10, id=req_json['job_id'], replace_existing=True)

where I want to pass custom intervals like minutes/seconds/etc based on request params, so I was trying this

job = scheduler.add_job(job_func, 'interval', interval_[1]=float(interval_[0]), id=req_json['job_id'], replace_existing=True, args=[req_json])

where interval_ = "10 seconds".split(' ')

How do i do this?

Aishwat Singh
  • 4,331
  • 2
  • 26
  • 48

1 Answers1

2

Dictionary unpacking **kwargs can be used to pass keyword arguments to functions

kwargs = {interval_[1]: float(interval_[0])}
job = scheduler.add_job(job_func, 'interval', id=req_json['job_id'], replace_existing=True, args=[req_json], **kwargs)

For example

kwargs = {'a': 1, 'b': 2}
foo(**kwargs)

Is equivalent to

foo(a=1, b=2)

The advantage to using this approach is that you can dynamically set the keys in a dictionary and as such can dynamically set the keyword parameters you pass to the function

Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50