diff options
-rw-r--r-- | dateutil/_common.py | 33 | ||||
-rw-r--r-- | dateutil/relativedelta.py | 35 | ||||
-rw-r--r-- | dateutil/rrule.py | 38 |
3 files changed, 45 insertions, 61 deletions
diff --git a/dateutil/_common.py b/dateutil/_common.py new file mode 100644 index 0000000..cd2a338 --- /dev/null +++ b/dateutil/_common.py @@ -0,0 +1,33 @@ +""" +Common code used in multiple modules. +""" + +class weekday(object): + __slots__ = ["weekday", "n"] + + def __init__(self, weekday, n=None): + self.weekday = weekday + self.n = n + + def __call__(self, n): + if n == self.n: + return self + else: + return self.__class__(self.weekday, n) + + def __eq__(self, other): + try: + if self.weekday != other.weekday or self.n != other.n: + return False + except AttributeError: + return False + return True + + __hash__ = None + + def __repr__(self): + s = ("MO", "TU", "WE", "TH", "FR", "SA", "SU")[self.weekday] + if not self.n: + return s + else: + return "%s(%+d)" % (s, self.n) diff --git a/dateutil/relativedelta.py b/dateutil/relativedelta.py index 6c71d2f..7e3bd12 100644 --- a/dateutil/relativedelta.py +++ b/dateutil/relativedelta.py @@ -8,41 +8,12 @@ from math import copysign from six import integer_types from warnings import warn -__all__ = ["relativedelta", "MO", "TU", "WE", "TH", "FR", "SA", "SU"] - - -class weekday(object): - __slots__ = ["weekday", "n"] - - def __init__(self, weekday, n=None): - self.weekday = weekday - self.n = n - - def __call__(self, n): - if n == self.n: - return self - else: - return self.__class__(self.weekday, n) - - def __eq__(self, other): - try: - if self.weekday != other.weekday or self.n != other.n: - return False - except AttributeError: - return False - return True - - __hash__ = None - - def __repr__(self): - s = ("MO", "TU", "WE", "TH", "FR", "SA", "SU")[self.weekday] - if not self.n: - return s - else: - return "%s(%+d)" % (s, self.n) +from ._common import weekday MO, TU, WE, TH, FR, SA, SU = weekdays = tuple([weekday(x) for x in range(7)]) +__all__ = ["relativedelta", "MO", "TU", "WE", "TH", "FR", "SA", "SU"] + class relativedelta(object): """ diff --git a/dateutil/rrule.py b/dateutil/rrule.py index 9b79e66..da94351 100644 --- a/dateutil/rrule.py +++ b/dateutil/rrule.py @@ -19,6 +19,8 @@ from six import advance_iterator, integer_types from six.moves import _thread, range import heapq +from ._common import weekday as weekdaybase + # For warning about deprecation of until and count from warnings import warn @@ -58,37 +60,15 @@ FREQNAMES = ['YEARLY','MONTHLY','WEEKLY','DAILY','HOURLY','MINUTELY','SECONDLY'] easter = None parser = None - -class weekday(object): - __slots__ = ["weekday", "n"] - - def __init__(self, weekday, n=None): +class weekday(weekdaybase): + """ + This version of weekday does not allow n = 0. + """ + def __init__(self, wkday, n=None): if n == 0: - raise ValueError("Can't create weekday with n == 0") + raise ValueError("Can't create weekday with n==0") - self.weekday = weekday - self.n = n - - def __call__(self, n): - if n == self.n: - return self - else: - return self.__class__(self.weekday, n) - - def __eq__(self, other): - try: - if self.weekday != other.weekday or self.n != other.n: - return False - except AttributeError: - return False - return True - - def __repr__(self): - s = ("MO", "TU", "WE", "TH", "FR", "SA", "SU")[self.weekday] - if not self.n: - return s - else: - return "%s(%+d)" % (s, self.n) + super(weekday, self).__init__(wkday, n) MO, TU, WE, TH, FR, SA, SU = weekdays = tuple([weekday(x) for x in range(7)]) |