Je t’embrasse Salutations from Silicon Valley, California

2Apr/190

Python DateSeries Object

In general there is often a need to iterate over a period of time in fixed delta steps.
This object provides a way to do that between two timestamps, allowing you to modify the step-size as necessary to vary the speed that you traverse the series in.

# Description:
#  - Generator/Iterator class object that produces a series of date/date
#    tuples of size 'delta', starting at 'since', ending at 'until'. By default
#    the original 'delta' value is calculated to yield the entire range in a
#    single request.
#
# :since    ISO-8601 datestamp for beginning of iteration sequence
# :until    ISO-8601 datestamp for end of iteration sequence
class DateSeries(object):
    def __init__(self, since, until):
        self._since = self.toDatetime(since)
        self._until = self.toDatetime(until)
        if (self._until >= self._since):
            self._delta = (self._until - self._since)
        else:
            self._delta = (self._since - self._until)
        self.__delta = deep(self._delta) # one "immutable" copy

    # Formatting Helpers
    #__________________________________________________________________________
    def toDatetime(self, value):
        if isinstance(value, datetime): return value
        return datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
    def toString(self, value):
        if isinstance(value, str): return value
        s = str(value).replace(' ','T')
        if s[-1] != 'Z': s += 'Z'
        return s
    def toTimedelta(self, value):
        if isinstance(value, timedelta): return value
        return timedelta(seconds=value)

    # Getters
    #__________________________________________________________________________
    def getOriginalDelta(self): return int(self.__delta.total_seconds())
    def getCurrentDelta(self): return int(self._delta.total_seconds())
    def getSince(self): return self.toString(self._since)
    def getUntil(self): return self.toString(self._until)

    # Setters
    #__________________________________________________________________________
    def resetDelta(self, value=None):
        if value != None: self._delta = self.toTimedelta(value)
        return self._delta.total_seconds()
    def setSince(self, value=None):
        if value != None: self._since = self.toDatetime(value)
        return self.toString(self._since)
    def setUntil(self, value=None):
        if value != None: self._until = self.toDatetime(value)
        return self.toString(self._until)

    # Iteration/Generator
    #__________________________________________________________________________
    def __iter__(self):
        if self._since == self._until:
            yield (self.toString(self._since),self.toString(self._until))

        elif self._since < self._until:
            (since, until) = (self._since, self._until)
        else:
            (since, until) = (self._until, self._since)

        while (since < until):
            chunk = since + self._delta
            if (chunk > until): chunk = until # capped at 'until'
            yield (self.toString(since), self.toString(chunk))
            since = chunk
Filed under: Python No Comments