0

I've been using the excellent schedule library to run a variety of jobs, but there is one thing I can't find.

The Python code for running a job every 10 minutes looks like this:

schedule.every(10).minutes.do(job)

Question is: how would I elegantly incorporate the time unit as a variable? I want to do something like this:

interval = 3
time_unit = 'hours'
schedule.every(interval).units(time_unit).do(job)

to run a job every 3 hours. Using variables would allow the schedule to be configurable.

Except the example above isn't working: the units member is a readable value only.

eval is not safe in this context.

Nic
  • 1,518
  • 12
  • 26
  • What you need exactly ? Way to parse the yml or anything else.? – Murali Jun 07 '16 at 07:38
  • Thanks @Murali, I hope it's clear now – Nic Jun 07 '16 at 07:58
  • What is the exception or error you are getting? – Murali Jun 07 '16 at 08:30
  • What do you mean by "`eval` is not safe in this context"? Testing the time_unit value isn't elegant enought? – Luc Giffon Jun 07 '16 at 09:24
  • Also, lambda can be used ! – Murali Jun 07 '16 at 10:28
  • Just to clear up what I mean about `eval`. I could write `eval('schedule.every(interval).{units}.do(job)'.format(units=time_unit)` but that is potentially insecure depending on where the time_unit comes from. The eval could be used to call someone else's code by altering something in a config file, for example. http://lybniz2.sourceforge.net/safeeval.html is worth a read if you want to know more. – Nic Jun 09 '16 at 02:30

1 Answers1

0

I've answered my own question. Thanks to those who replied.

# specify time interval units as a variable in code...
interval = 3
time_unit = 'hours'
schedule.every(interval).set_units(time_unit).do(job)

Hotfix in the schedule library (class Job)-- add the following method:

def set_units(self, unit):
    self.unit = unit if unit[-1] == 's' else unit + 's'
    return self
Nic
  • 1,518
  • 12
  • 26