-4

I need to schedule the function automatically by the selected days. So I used schedule for this function but I can't pass arguments to the schedule object dynamically. If anyone knows how to achieve that, please provide me the solution. Below is my code:

import schedule
def job(ip_id):
    print "Schedule started" + ip_id

ip_id = 1
sname = "weekly"
sched = schedule.every().monday.do(job, ip_id).tag(sname)

It works and scheduled on monday but when I pass the value from list, error arise.

import schedule
def job(ip_id):
    print "Schedule started" + ip_id
ip_id = 1
sname = "weekly"
cron = [monday, wednesday, sunday]
for i in cron:
   sched = schedule.every().i.do(job, ip_id).tag(sname)

How to pass value to that place is my problem ?

Antony
  • 581
  • 4
  • 7
  • 24
  • "What is the issue in question?" the question as a whole just makes very little sense. What are the `monday`, `wednesday`, and `sunday` in your `cron` list? What is your `jschedule.every()`? Do you want to use the values behind `monday`, `wednesday`, and `sunday` to access the `every()`, or do you prefer the variable names? None of this just seems right and we have way too little context. – Markus Meskanen Jun 01 '17 at 06:29
  • `job()` can be without arguments. When it is calling it can obtain arguments from another place (where `ip_id` is stored). Or you can make closure: `lambda: joib(ip_id)` as argument of `do(..)` – RandomB Jun 01 '17 at 06:37
  • @Paul-AG His question is about how to get `.every().i` to work with the variable `i`. – Barmar Jun 01 '17 at 06:40
  • @Barmar yes my question is that. – Antony Jun 01 '17 at 06:41
  • @Antro Does the suggested duplicate answer your question? – Barmar Jun 01 '17 at 06:44
  • @MarkusMeskanen jschedule is schedule package i just renamed it and I need to pass variable values i.e(monday, tuesday,...) dynamically after every(). – Antony Jun 01 '17 at 06:45
  • @Barmar I tried it but I can't get the solution. I will work over it. Thanks for your support – Antony Jun 01 '17 at 06:46

1 Answers1

1

No magic here. "monday" is only name of missing attribute :)

import schedule

def job(*a, **k):
    pass

ip_id = 0
sname = "sname"

cron = ['monday', 'wednesday', 'sunday']
for i in cron:
   sched = getattr(schedule.every(), i).do(job, ip_id).tag(sname)
RandomB
  • 3,367
  • 19
  • 30