简体   繁体   中英

Access APScheduler cron trigger field values in python

Similar to this question I want to extract the info of a cron job trigger from APScheduler. However, I need the "day_of_week" field and not everything. Using

for job in scheduler.get_jobs():
  for f in job.trigger.fields:
    print(f.name + " " + str(f))

i can see all the fields, eg week,hour,day_of_week , but

job.trigger.day_of_week is seemingly 'not an attribute' of the "CronTrigger" object. I'm confused as to what kind of object this job.trigger is and how its fields are packed. I tried to read the code on github, but it is even more puzzling.

How do I extract only the one field day_of_week , and how is this trigger class structured?

Diving deeper I found that

apscheduler.triggers.cron.fields.DayOfWeekField

I can find by indexing the job.trigger.fields[4] , which seems really bad style, since it depends on the 'position'of the field. What I get is this DayOfWeekField, from which comically I am not able to retrieve it's value either:

a.get_value
<bound method DayOfWeekField.get_value of DayOfWeekField('day_of_week', '1,2,3,4')>

The structure of the fields is coded here , but I don't know what to do with dateval, the argument of get_value() .

Eventually, after hopefully understanding the concept, I want to do

if job-day_of_week contains mon

if job-day_of_week == '*'

print ( job-day_of_week )

I am grateful for any suggestions/hints!

Looking at the code, you should be able to get the day_of_week field without hardcoding the index by using the CronTrigger class's FIELD_NAMES property, eg

dow_index = CronTrigger.FIELD_NAMES.index('day_of_week')
dow = job.trigger.fields[dow_index]

Getting the value of the field is a bit more complicated, but it appears that BaseField implements the str function that should give you the value of the expression that created the field as a string that you could parse to find what you want:

dow_value_as_string = str(dow)
if 'mon' in dow_value_as_string:
   # do something
if dow_value_as_string = "*":
   # do something else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM