lässt sich folgendes nicht eleganter lösen?
Code: Alles auswählen
if self.style == 1:
return date.weekday() in self.days
else:
return date.day in self.days
Code: Alles auswählen
if self.style == 1:
return date.weekday() in self.days
else:
return date.day in self.days
Code: Alles auswählen
ret = date.weekday() in self.days if self.style == 1 else date.day in self.days
return ret
Code: Alles auswählen
day = date.weekday() if self.style == 1 else date.day
return day in self.days
Code: Alles auswählen
from datetime import timedelta as Timedelta
from itertools import ifilter
def iter_dates(start, end, step=Timedelta(days=1)):
while start <= end:
yield start
start += step
def week_number(date):
return int(date.strftime('%W'))
class Recurrence(object):
def __init__(self, date, exceptions):
self.date = date
self.execptions = set(exceptions)
def is_active(self, date):
return date not in self.exceptions
def iter_active_days(self, start, end):
return ifilter(self.is_active, iter_dates(start, end))
class DailyRecurrence(Recurrence):
def __init__(self, date, exceptions, every_n_days):
Recurrence.__init__(self, date, exceptions)
self.every_n_days = every_n_days
def is_active(self, date):
return (Recurrence.is_active(self, date)
and (date - self.date).days % self.every_n_days == 0)
class WeeklyRecurrence(Recurrence):
def __init__(self, date, exceptions, every_n_weeks, on_days):
Recurrence.__init__(self, date, exceptions)
self.every_n_weeks = every_n_weeks
self.on_days = on_days
def is_active(self, date):
distance = week_number(date) - week_number(self.date)
return (Recurrence.is_active(self, date)
and distance % self.every_n_weeks == 0
and date.weekday() in self.on_days)
Das find' ich seeehr GUT!BlackJack hat geschrieben:Bei einem `set()` für die Ausnahmen geht der ``in``-Operator schneller als bei einer Liste.
Code: Alles auswählen
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if width == 0 and height == 0 and \
color == 'red' and emphasis == 'strong' or \
highlight > 100:
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
Code: Alles auswählen
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if width == 0 and height == 0 \
and color == 'red' and emphasis == 'strong' \
or highlight > 100:
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and color == 'red' \
or emphasis is None:
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
Hmm, dann würde ich doch direkt mal den 29. Februar als jährlich wiederkehrenden Termin eintragen.BlackJack hat geschrieben:@mutetella: IMHO nicht so gut. Ich würde das vielleicht über zwei verschiedene Klassen lösen, eine für fixe Termine (also "jährlich") und eine für in regelmässigen Abständen wiederkehrende Termine (täglich/wöchentlich/alle x Tage).