Question

I am using dateutil rrule function .. i get the weekdays from my data model in django. when i put it in rrule function like this

for x in lgs:
    teams = Team.objects.filter(league=x.id)
    teamcount = len(teams)
    playdays = "rrule." + str(x.play_days)
    fixdays = rrule.rrule(rrule.DAILY,byweekday = (playdays),dtstart=startdate,until=endate)
    print list(fixdays)

Traceback (most recent call last):
File "<console>", line 5, in <module>
File "/usr/lib/pymodules/python2.7/dateutil/rrule.py", line 345, in __init__
    elif not wday.n or freq > MONTHLY:
AttributeError: 'str' object has no attribute 'n'

if i use the function normally, and replace line 5 with

    fixdays = rrule.rrule(rrule.DAILY,byweekday = (rrule.SU, rrule.MO),dtstart=startdate,until=endate)

i get no problem. what could be wrong.

Was it helpful?

Solution

(rrule.SU, rrule.MO)

is a list (tuple) of dateutil.rrule.weekday objects, while

"rrule." + str(x.play_days)

is a string (that does not have the .n attribute).

You could define a dictionary:

weekday = {'MO': rrule.MO, 'TU': rrule.TU, … }

and then call it:

playdays = weekday[str(x.play_days)]

Also, remember that

byweekday = (playdays)

is the same as

byweekday = playdays

If you want to define a tuple from single element, add comma:

byweekday = (playdays,)

OTHER TIPS

"rrule." + str(x.play_days) returns a string not the variable with that name, while in your hardcoded example you're passing a set of variables

You probably want to do something like this instead:

playdays = [getattr(rrule, pd) for pd in x.play_days]

and then pass playdays to byweekday without the brackets, like byweekday=playdays, ..... as eumiro said in his answer it's not necessary :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top