1

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, e.g. 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!

Jason Jenkins
  • 5,332
  • 3
  • 24
  • 29
mike
  • 791
  • 11
  • 26

1 Answers1

1

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, e.g.

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
Jason Jenkins
  • 5,332
  • 3
  • 24
  • 29
  • Hey, thank you! Could you just elaborate a little bit, how this FIELD_NAMES property and the indexing works? Why are the fields of CronTrigger ordered - is this python itself? – mike Oct 29 '18 at 14:00
  • FIELD_NAMES is a property of the CronTrigger class which is defined [here.](https://github.com/agronholm/apscheduler/blob/master/apscheduler/triggers/cron/__init__.py). (It's explicitly defined in the code, not some sort of built-in python magic. ) The property is a list of field names and the `index` function is a built-in for lists, returning the index of the first occurrence of 'day_of_week' in the list. – Jason Jenkins Nov 05 '18 at 14:07