Skip to content

Instantly share code, notes, and snippets.

@ale-rt
Created April 24, 2014 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ale-rt/13235e8a158f31b87e63 to your computer and use it in GitHub Desktop.
Save ale-rt/13235e8a158f31b87e63 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from pyinter.interval import Interval
def slots_to_points(slots):
''' Return a list of point starting from the slots
'''
class LowerEndpoint(int):
''' Lower Endpoint
'''
class UpperEndpoint(int):
''' Upper Endpoint8
'''
class TimeSlot(Interval):
''' Overrides and simplifies pyinter.Interval
'''
_lower = Interval.CLOSED
_upper = Interval.CLOSED
def __init__(self, lower, upper):
self._lower_value = LowerEndpoint(lower)
self._upper_value = UpperEndpoint(upper)
def __len__(self):
return self._upper_value - self.lower_value
def __sub__(self, value):
''' Subtract something from this
:value: can be an interval or an iterable containing intervals
:return: a list of Interval objects
'''
if isinstance(value, Interval):
value = [value]
points = []
# We filter not overlapping intervals
good_intervals = [x for x in value if x.overlaps(self)]
[points.extend([x.lower_value, x.upper_value]) for x in good_intervals]
points.sort()
start = self.lower_value
intervals = []
for x in points:
if isinstance(x, LowerEndpoint) and x > start:
intervals.append(TimeSlot(start, x))
# we raise the bar waiting for another stop
start = self.upper_value
elif isinstance(x, UpperEndpoint):
start = x
intervals.append(TimeSlot(start, self.upper_value))
return intervals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment