1
votes

I'm using dateutil to generate a schedule based on an input file.

my config file looks like this:

[alarm1]
time = 8:00
days = MO

[alarm2]
time = 9:00
days = TU, WE

[alarm3]
time = 22:00
days = MO, WE, FR
etc..

I'm using dateutil to generate a schedule based off this.

here's some sample code, where I omitted the control flows and import code for the sake of brevity.

from dateutil.rrule import *
from dateutil.parser import *
import datetime
import configparser


alarms = rruleset()
time = parse(alarm_parser.get(alarm_section, 'time')).time()
date = datetime.date.today()
alarm_time= datetime.datetime.combine(date, time)
days = '(' + alarm_parser.get(alarm_section, 'days') + ',)'
alarm_days = eval(days)

alarms.rrule(rrule(WEEKLY, dtstart = alarm_datetime, byweekday = alarm_days,  count = 1))

this code now works thanks to eval However, eval apparently risky.

I've tried rrulestr(), but the command doesn't seem to accept byweekday = as part of the argument.

how do I convert the string in the config file to a tuple that rrule can work with without using eval? I've tried ast.literal_eval, but that gives me the following error

ValueError: malformed node or string: <_ast.Name object at 0xb68e2430>
2
i've come across eval as a possible answer. however, I'm a bit worried about the possible risks. However, using ast.literal_eval seems to give me an error. Anyway, I updated the code to reflect the (partial) solution I have foundMarc Wagner

2 Answers

1
votes

I needed a slightly more complex version of Paul's simple code (one that could handle parenthesized offsets like "FR(+2)"). Here is what I came up with:

import re
from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU

_daymap = {"MO": MO, "TU": TU, "WE": WE, "TH": TH, "FR": FR, "SA": SA, "SU": SU}
_dayre = re.compile(r"(MO|TU|WE|TH|FR|SA|SU)(?=\(([-+]?[0-9]+)\))?")


def parsedays(daystr):
    for day in daystr.split(","):
        m = _dayre.match(day.strip())
        if m:
            weekday, offset = m.groups()
            result = _daymap[weekday]
            if offset:
                result = result(int(offset))
            yield result


def dumpdays(days):
    return ",".join(str(d) for d in days)

Tested using Python 3.7.3

0
votes

The simple way to do this is some variation on this:

from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU

daymap = {'MO': MO, 'TU': TU, 'WE': WE, 'TH': TH, 'FR': FR, 'SA': SA, 'SU': SU}
daystr = 'TU, TH'

days = [daymap[day.strip()] for day in daystr.split(',')]

However, you can also accomplish this with rrulestr using the BYDAY (not BYWEEKDAY) parameter:

from dateutil.rrule import rrulestr
base_rrstr = ('DTSTART:{alarm_datetime:%Y%m%dT%H%M%S}\n' +
              'FREQ={freq_str};COUNT={count_str};BYDAY={day_str}')

alarm_datetime = datetime.datetime.today()
freq_str = "WEEKLY"
count_str = "1"
day_str = "TU,TH"

crr = rrulestr(base_rrstr.format(alarm_datetime=alarm_datetime,
                                 freq_str=freq_str,
                                 count_str=count_str,
                                 day_str=day_str))