Last active
October 21, 2019 01:01
-
-
Save solidiquis/f64cc4be633b3dbedacaf48b03fb0b57 to your computer and use it in GitHub Desktop.
Priority
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from prioritize import MaxHeapq | |
import code | |
class Task: | |
tasks = [] | |
def __init__(self, *args, **kwargs): | |
self.description = kwargs["description"] | |
self.duration = str(kwargs["duration"]) + " min" | |
self.multitask = kwargs["multitask"] | |
self.dependencies = kwargs["dependencies"] | |
self.status = kwargs["status"] | |
self.id = self.assign_id() if self.tasks else 1 | |
self.tasks.append(self) | |
def execute(self): | |
if not self.multitask: | |
self.duration = 0 | |
self.status = "COMPLETED" | |
def ongoing_dependencies(self): | |
ongoing_dependencies = [] | |
for id in self.dependencies: | |
dependency = self.find_by_id(id) | |
if dependency.status == "NOT-YET-STARTED" or dependency.status == "IN-PROGRESS": | |
ongoing_dependencies.append(id) | |
return ongoing_dependencies | |
def priority(self, priority_score=100): | |
return priority_score - len(self.get_total_dependencies()) | |
def get_total_dependencies(self): | |
# soft and hard dependencies | |
if len(self.ongoing_dependencies()) == 0: | |
return set() | |
total_dependencies = set(self.ongoing_dependencies()) | |
for dependency_id in self.ongoing_dependencies(): | |
dependency = self.find_by_id(dependency_id) | |
total_dependencies |= dependency.get_total_dependencies() | |
return total_dependencies | |
def assign_id(self): | |
return self.last().id + 1 | |
@classmethod | |
def find_by_id(cls, id): | |
for i in cls.tasks: | |
if i.id == id: | |
return i | |
else: | |
print(f"Task with ID: {id} does not exist.") | |
return None | |
@classmethod | |
def last(cls): | |
return cls.tasks[-1] | |
@classmethod | |
def all(cls): | |
return cls.tasks | |
@classmethod | |
def update_dependencies(cls, completed_task_id): | |
for i in cls.all(): | |
if completed_task_id in i.dependencies: | |
i.dependencies.remove(completed_task_id) | |
@classmethod | |
def recalculate_priorities(): | |
pass | |
golden1 = Task( | |
description="Walk around lake", | |
duration=20, | |
multitask=True, | |
dependencies=[], | |
status="NOT-YET-STARTED" | |
) | |
golden2 = Task( | |
description="Buy museum tickets", | |
duration=3, | |
multitask=False, | |
dependencies=[golden1.id], | |
status="NOT-YET-STARTED" | |
) | |
golden3 = Task( | |
description="Explore museum", | |
duration=30, | |
multitask=True, | |
dependencies=[golden1.id, golden2.id], | |
status="NOT-YET-STARTED" | |
) | |
#Discover music | |
music1 = Task( | |
description="Research music", | |
duration=5, | |
multitask=False, | |
dependencies=[], | |
status="NOT-YET-STARTED" | |
) | |
music2 = Task( | |
description="Choose songs", | |
duration=10, | |
multitask=False, | |
dependencies=[music1.id], | |
status="NOT-YET-STARTED" | |
) | |
music3 = Task( | |
description="Create playlist", | |
duration=10, | |
multitask=False, | |
dependencies=[music1.id, music2.id], | |
status="NOT-YET-STARTED" | |
) | |
music4 = Task( | |
description="Listen songs", | |
duration=100, | |
multitask=True, | |
dependencies=[music1.id, music2.id, music3.id], | |
status="NOT-YET-STARTED" | |
) | |
#Castro St | |
castro1 = Task( | |
description="Take bus", | |
duration=30, | |
multitask=True, | |
dependencies=[], | |
status="NOT-YET-STARTED" | |
) | |
castro2 = Task( | |
description="Walk on Castro", | |
duration=30, | |
multitask=True, | |
dependencies=[castro1.id], | |
status="NOT-YET-STARTED" | |
) | |
castro3 = Task( | |
description="Buy snacks", | |
duration=10, | |
multitask=True, | |
dependencies=[castro1.id], | |
status="NOT-YET-STARTED" | |
) | |
castro4 = Task( | |
description="Theatre", | |
duration=120, | |
multitask=False, | |
dependencies=[castro1.id, castro2.id], | |
status="NOT-YET-STARTED" | |
) | |
#Corona heights | |
corona1 = Task( | |
description="Decide route", | |
duration=5, | |
multitask=False, | |
dependencies=[], | |
status="NOT-YET-STARTED" | |
) | |
corona2 = Task( | |
description="Get to trail", | |
duration=5, | |
multitask=False, | |
dependencies=[castro1.id, castro2.id, castro3.id, castro4.id], | |
status="NOT-YET-STARTED" | |
) | |
corona3 = Task( | |
description="Walk trail", | |
duration=60, | |
multitask=True, | |
dependencies=[corona2.id], | |
status="NOT-YET-STARTED" | |
) | |
corona4 = Task( | |
description="Drink Water", | |
duration=10, | |
multitask=True, | |
dependencies=[corona2.id], | |
status="NOT-YET-STARTED" | |
) | |
corona5 = Task( | |
description="Go for meal", | |
duration=30, | |
multitask=True, | |
dependencies=[corona3.id, corona4.id], | |
status="NOT-YET-STARTED" | |
) | |
#Financial Districr | |
financial1 = Task( | |
description="Eat meal", | |
duration=40, | |
multitask=True, | |
dependencies=[corona5.id], | |
status="NOT-YET-STARTED" | |
) | |
financial2 = Task( | |
description="Discover skyscrapers", | |
duration=40, | |
multitask=True, | |
dependencies=[financial1.id], | |
status="NOT-YET-STARTED" | |
) | |
financial3 = Task( | |
description="Research skyscrapers story", | |
duration=10, | |
multitask=False, | |
dependencies=[financial1.id], | |
status="NOT-YET-STARTED" | |
) | |
activities = [ | |
(golden1.priority(), golden1), | |
(golden2.priority(), golden2), | |
(golden3.priority(), golden3), | |
(music1.priority(), music1), | |
(music2.priority(), music2), | |
(music3.priority(), music3), | |
(music4.priority(), music4), | |
(castro1.priority(), castro1), | |
(castro2.priority(), castro2), | |
(castro3.priority(), castro3), | |
(castro4.priority(), castro4), | |
(corona1.priority(), corona1), | |
(corona2.priority(), corona2), | |
(corona3.priority(), corona3), | |
(corona4.priority(), corona4), | |
(corona5.priority(), corona5), | |
(financial1.priority(), financial1), | |
(financial2.priority(), financial2), | |
(financial3.priority(), financial3) | |
] | |
heaptask = MaxHeapq() | |
for task in activities: | |
heaptask.insert(task) | |
for i in heaptask.heap: | |
i[1].execute() | |
Task.update_dependencies(i[1].id) | |
heaptask.extract_max() | |
# Update the heap (because priority score changed) | |
# Reform the heap | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment