Rrule - 读取 'freq'、'interval' 等值?

Rrule - reading 'freq', 'interval', etc values?

如何从 Rrule

中提取 'freq'、'interval' 等值
from dateutil.rrule import *

my_rrule = rrule(freq=DAILY, interval=2, count=3)
print(str(my_rrule))
print(my_rrule.count())
print(my_rrule.interval())
>>>
DTSTART:20190130T025306
RRULE:FREQ=DAILY;INTERVAL=2;COUNT=3
3
Traceback (most recent call last):
  File "/home/pi/test2.py", line 6, in <module>
    print(my_rrule.interval())
AttributeError: 'rrule' object has no attribute 'interval'
>>>

此方法读取 'count' 属性值,但会中断 'interval' 、 'freq' 等

更改代码

print(my_rrule.interval())

print(my_rrule._interval)

你可以从rrule的init函数中得到线索,它在class中将interval存储为_interval,这意味着你最好不要尝试从外部使用这个变量,因为实例可能有通过更改其值或其逻辑来使用此变量的副作用取决于它。

def __init__(self, freq, dtstart=None,
             interval=1, wkst=None, count=None, until=None, bysetpos=None,
             bymonth=None, bymonthday=None, byyearday=None, byeaster=None,
             byweekno=None, byweekday=None,
             byhour=None, byminute=None, bysecond=None,
             cache=False):
    super(rrule, self).__init__(cache)
    global easter
    if not dtstart:
        if until and until.tzinfo:
            dtstart = datetime.datetime.now(tz=until.tzinfo).replace(microsecond=0)
        else:           
            dtstart = datetime.datetime.now().replace(microsecond=0)
    elif not isinstance(dtstart, datetime.datetime):
        dtstart = datetime.datetime.fromordinal(dtstart.toordinal())
    else:
        dtstart = dtstart.replace(microsecond=0)
    self._dtstart = dtstart
    self._tzinfo = dtstart.tzinfo
    self._freq = freq
    self._interval = interval
    self._count = count

这是我的操作方法(格式()的循环):

    def parse_rrule(r):
        """Sample:
        DTSTART:19970930T090000
        RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1
        EXDATE:19960402T010000,19960403T010000,19960404T010000
        """
        r = str(r)      # If it's an rrule, convert to the string representation
        result = {}
        elements = r.split('\n')
        for element in elements:
            if ':' in element:
                name, values = element.split(':')
            else:
                name = element
                values = ''

            vls = {}
            for value in values.split(';'):
                if '=' in value:
                    k, v = value.split('=')
                    v = todatetime(toint(v))
                    if isinstance(v, str) and ',' in v:
                        l = []
                        for e in v.split(','):
                            e = todatetime(toint(e))
                            l.append(e)
                        v = l
                    vls[k] = v
                else:
                    vls[value] = None
            if len(vls) == 1 and list(vls.values())[0] is None:
                vls = list(vls.keys())[0]
                if isinstance(vls, str) and ',' in vls:
                    l = []
                    for e in vls.split(','):
                        e = todatetime(toint(e))
                        l.append(e)
                    vls = l
                else:
                    vls = todatetime(toint(vls))
            result[name] = vls
        log.debug(f'parse_rrule({r}) = {result}')
        return result