Skip to content

Commit

Permalink
Simpler ScoreTimeTicker code.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Jun 8, 2016
1 parent cb847bd commit ae58117
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 66 deletions.
91 changes: 33 additions & 58 deletions leather/ticks/score_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
#: The maximum length of a viable tick sequence
MAX_TICK_COUNT = 10

MIN_UNITS = 4

INTERVALS = [
(utils.to_year_count, utils.from_year_count, None, '%Y'),
(utils.to_month_count, utils.from_month_count, '%Y-%m', '%m'),
(utils.to_day_count, utils.from_day_count, '%m-%d', '%d'),
(utils.to_hour_count, utils.from_hour_count, '%d-%H', '%H'),
(utils.to_minute_count, utils.from_minute_count, '%H:%M', '%M'),
(utils.to_second_count, utils.from_second_count, '%H:%M:%S', '%S'),
(utils.to_microsecond_count, utils.from_microsecond_count, '%S-%f', '%f'),
]


class ScoreTimeTicker(ScoreTicker):
"""
Expand All @@ -39,65 +51,28 @@ def __init__(self, domain_min, domain_max):
else:
self._type = date

# Years
if self._domain_max.year - self._domain_min.year >= 4:
self._to_unit = utils.to_year_count
self._from_unit = partial(utils.from_year_count, t=self._type)
self._fmt = '%Y'
# Months
elif utils.to_month_count(self._domain_max) - utils.to_month_count(self._domain_min) >= 4:
self._to_unit = utils.to_month_count
self._from_unit = partial(utils.from_month_count, t=self._type)

if utils.to_year_count(self._domain_max) - utils.to_year_count(self._domain_min) >= 1:
self._fmt = '%Y-%m'
else:
self._fmt = '%m'
# Days
elif utils.to_day_count(self._domain_max) - utils.to_day_count(self._domain_min) >= 4:
self._to_unit = utils.to_day_count
self._from_unit = partial(utils.from_day_count, t=self._type)

if utils.to_month_count(self._domain_max) - utils.to_month_count(self._domain_min) >= 1:
self._fmt = '%m-%d'
else:
self._fmt = '%d'
# Hours
elif utils.to_hour_count(self._domain_max) - utils.to_hour_count(self._domain_min) >= 4:
self._to_unit = utils.to_hour_count
self._from_unit = utils.from_hour_count

if utils.to_day_count(self._domain_max) - utils.to_day_count(self._domain_min) >= 1:
self._fmt = '%d-%H'
else:
self._fmt = '%H'
# Minutes
elif utils.to_minute_count(self._domain_max) - utils.to_minute_count(self._domain_min) >= 4:
self._to_unit = utils.to_minute_count
self._from_unit = utils.from_minute_count

if utils.to_hour_count(self._domain_max) - utils.to_hour_count(self._domain_min) >= 1:
self._fmt = '%H:%M'
else:
self._fmt = '%M'
# Seconds
elif utils.to_second_count(self._domain_max) - utils.to_second_count(self._domain_min) >= 4:
self._to_unit = utils.to_second_count
self._from_unit = utils.from_second_count

if utils.to_minute_count(self._domain_max) - utils.to_minute_count(self._domain_min) >= 1:
self._fmt = '%H:%M:%S'
else:
self._fmt = '%S'
# Microseconds
else:
self._to_unit = utils.to_microsecond_count
self._from_unit = utils.from_microsecond_count
# Identify appropriate interval unit
self._to_unit = None
self._from_unit = None
self._fmt = None

previous_delta = 0

for to_func, from_func, overlap_fmt, simple_fmt in INTERVALS:
delta = to_func(self._domain_max) - to_func(self._domain_min)

if delta >= MIN_UNITS or to_func is utils.to_microsecond_count:
self._to_unit = to_func
self._from_unit = partial(from_func, t=self._type)

if previous_delta >= 1:
self._fmt = overlap_fmt
else:
self._fmt = simple_fmt

break

if utils.to_second_count(self._domain_max) - utils.to_second_count(self._domain_min) >= 1:
self._fmt = '%S-%f'
else:
self._fmt = '%f'
previous_delta = delta

# Compute unit min and max
self._unit_min = self._to_unit(self._domain_min)
Expand Down
16 changes: 8 additions & 8 deletions leather/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def to_hour_count(d):
return (d - datetime.min).total_seconds() / (60 * 60)


def from_hour_count(n):
def from_hour_count(n, t=datetime):
"""
n hours > date
"""
return datetime.min + timedelta(hours=n)
return t.min + timedelta(hours=n)


def to_minute_count(d):
Expand All @@ -139,11 +139,11 @@ def to_minute_count(d):
return (d - datetime.min).total_seconds() / 60


def from_minute_count(n):
def from_minute_count(n, t=datetime):
"""
n minutes > date
"""
return datetime.min + timedelta(minutes=n)
return t.min + timedelta(minutes=n)


def to_second_count(d):
Expand All @@ -153,11 +153,11 @@ def to_second_count(d):
return (d - datetime.min).total_seconds()


def from_second_count(n):
def from_second_count(n, t=datetime):
"""
n seconds > date
"""
return datetime.min + timedelta(seconds=n)
return t.min + timedelta(seconds=n)


def to_microsecond_count(d):
Expand All @@ -167,8 +167,8 @@ def to_microsecond_count(d):
return (d - datetime.min).total_seconds() * 1000


def from_microsecond_count(n):
def from_microsecond_count(n, t=datetime):
"""
n microseconds > date
"""
return datetime.min + timedelta(microseconds=n)
return t.min + timedelta(microseconds=n)

0 comments on commit ae58117

Please sign in to comment.