A simple wrapper around a `dateutil.rrule` allowing flexible date tick specifications.
| 941 | |
| 942 | |
| 943 | class rrulewrapper: |
| 944 | """ |
| 945 | A simple wrapper around a `dateutil.rrule` allowing flexible |
| 946 | date tick specifications. |
| 947 | """ |
| 948 | def __init__(self, freq, tzinfo=None, **kwargs): |
| 949 | """ |
| 950 | Parameters |
| 951 | ---------- |
| 952 | freq : {YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY} |
| 953 | Tick frequency. These constants are defined in `dateutil.rrule`, |
| 954 | but they are accessible from `matplotlib.dates` as well. |
| 955 | tzinfo : `datetime.tzinfo`, optional |
| 956 | Time zone information. The default is None. |
| 957 | **kwargs |
| 958 | Additional keyword arguments are passed to the `dateutil.rrule`. |
| 959 | """ |
| 960 | kwargs['freq'] = freq |
| 961 | self._base_tzinfo = tzinfo |
| 962 | |
| 963 | self._update_rrule(**kwargs) |
| 964 | |
| 965 | def set(self, **kwargs): |
| 966 | """Set parameters for an existing wrapper.""" |
| 967 | self._construct.update(kwargs) |
| 968 | |
| 969 | self._update_rrule(**self._construct) |
| 970 | |
| 971 | def _update_rrule(self, **kwargs): |
| 972 | tzinfo = self._base_tzinfo |
| 973 | |
| 974 | # rrule does not play nicely with timezones - especially pytz time |
| 975 | # zones, it's best to use naive zones and attach timezones once the |
| 976 | # datetimes are returned |
| 977 | if 'dtstart' in kwargs: |
| 978 | dtstart = kwargs['dtstart'] |
| 979 | if dtstart.tzinfo is not None: |
| 980 | if tzinfo is None: |
| 981 | tzinfo = dtstart.tzinfo |
| 982 | else: |
| 983 | dtstart = dtstart.astimezone(tzinfo) |
| 984 | |
| 985 | kwargs['dtstart'] = dtstart.replace(tzinfo=None) |
| 986 | |
| 987 | if 'until' in kwargs: |
| 988 | until = kwargs['until'] |
| 989 | if until.tzinfo is not None: |
| 990 | if tzinfo is not None: |
| 991 | until = until.astimezone(tzinfo) |
| 992 | else: |
| 993 | raise ValueError('until cannot be aware if dtstart ' |
| 994 | 'is naive and tzinfo is None') |
| 995 | |
| 996 | kwargs['until'] = until.replace(tzinfo=None) |
| 997 | |
| 998 | self._construct = kwargs.copy() |
| 999 | self._tzinfo = tzinfo |
| 1000 | self._rrule = rrule(**self._construct) |
no outgoing calls
searching dependent graphs…