Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from datetime import date, datetime, timedelta
def main():
events = [
ZakladniInterval(dt('2020-01-01'), dt('2030-01-01')),
Prodlouzeni(id=222, roky=3),
Zruseni(id=333, zrusit_id=222),
Zruseni(id=444, zrusit_id=333),
Zruseni(id=555, zrusit_id=444),
]
zacatek, konec = spocitej(events)
print(f'{zacatek} - {konec}')
def spocitej(events):
while True:
old_events = events
for event in events:
events = event.process_events(events)
if old_events == events:
break
interval = None
for event in events:
interval = event.process_interval(interval)
return interval
def dt(s):
return datetime.strptime(s, '%Y-%m-%d').date()
class BaseEvent:
def __init__(self, id):
self.id = id
def process_events(self, events):
return events
def process_interval(self, interval):
return interval
class ZakladniInterval (BaseEvent):
def __init__(self, zacatek, konec):
super().__init__(id=None)
self.zacatek = zacatek
self.konec = konec
def process_interval(self, interval):
assert interval is None
return (self.zacatek, self.konec)
class Prodlouzeni (BaseEvent):
def __init__(self, id, roky):
self.id = id
self.roky = roky
def process_interval(self, interval):
zacatek, konec = interval
konec = konec.replace(year=konec.year + self.roky)
return (zacatek, konec)
class Zruseni (BaseEvent):
def __init__(self, id, zrusit_id):
self.id = id
self.zrusit_id = zrusit_id
def process_events(self, events):
event, = [ev for ev in events if ev.id == self.zrusit_id]
return [Zruseno(ev, self.id) if ev is event and not isinstance(event, Zruseno) else ev for ev in events]
class Zruseno (BaseEvent):
def __init__(self, puvodni_event, id_zruseno_kym):
self.puvodni_event = puvodni_event
self.id = puvodni_event.id
self.id_zruseno_kym = id_zruseno_kym
def __eq__(self, other):
return isinstance(other, Zruseno) and self._get_data() == other._get_data()
def _get_data(self):
return (self.puvodni_event, self.id_zruseno_kym)
def process_events(self, events):
event_ktery_toto_zrusil, = [ev for ev in events if ev.id == self.id_zruseno_kym]
if isinstance(event_ktery_toto_zrusil, Zruseno):
return [self.puvodni_event if ev is self else ev for ev in events]
else:
return events
if __name__ == '__main__':
main()
from datetime import date, datetime, timedelta
def main():
events = [
ZakladniInterval(dt('2020-01-01'), dt('2030-01-01')),
Prodlouzeni(id=123, roky=3),
ZruseniProdlouzeni(id_prodlouzeni=123)
]
zacatek, konec = spocitej(events)
print(f'{zacatek} - {konec}')
def spocitej(events):
for event in list(events):
events = event.process_events(events)
interval = None
for event in events:
interval = event.process_interval(interval)
return interval
def dt(s):
return datetime.strptime(s, '%Y-%m-%d').date()
class BaseEvent:
def process_events(self, events):
return events
def process_interval(self, interval):
return interval
class ZakladniInterval (BaseEvent):
def __init__(self, zacatek, konec):
self.zacatek = zacatek
self.konec = konec
def process_interval(self, interval):
assert interval is None
return (self.zacatek, self.konec)
class Prodlouzeni (BaseEvent):
def __init__(self, id, roky):
self.id = id
self.roky = roky
def process_interval(self, interval):
zacatek, konec = predchozi
konec = konec.replace(year=konec.year + roky)
return (zacatek, konec)
class ZruseniProdlouzeni (BaseEvent):
def __init__(self, id_prodlouzeni):
self.id_prodlouzeni = id_prodlouzeni
def process_events(self, events):
event, = [ev for ev in events if isinstance(ev, Prodlouzeni) and ev.id == self.id_prodlouzeni]
return [ev for ev in events if ev is not event]
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment