Created
November 24, 2023 12:48
-
-
Save roganjoshp/bf2cb5dc545367c3f30e0f54737e8df0 to your computer and use it in GitHub Desktop.
This file contains 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
# File deliberately empty, but gist won't allow it |
This file contains 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
import random | |
import uuid | |
import datetime as dt | |
SOME_VARIABLE = 2 | |
class Job: | |
def __init__( | |
self, | |
service_id: str, | |
start_time: int, | |
end_time: int, | |
duration: int, | |
_id: str = uuid.uuid4().hex | |
): | |
self.service_id = service_id | |
self.start_time = start_time | |
self.end_time = end_time | |
self.duration = duration | |
self._id = _id # Attributes with leading _ are "private" | |
def get_human_timeslot(self): | |
start_hours = self.start_time // 60 | |
start_mins = self.start_time - (start_hours * 60) | |
end_hours = self.end_time // 60 | |
end_mins = self.end_time - (end_hours * 60) | |
print(( | |
dt.datetime(1900, 1, 1, start_hours, start_mins)).strftime("%H:%M"), | |
'-', | |
dt.datetime(1900, 1, 1, end_hours, end_mins).strftime("%H:%M") | |
) | |
class Delivery(Job): | |
def __init__( | |
self, | |
capacity: int, | |
**kwargs, | |
): | |
super().__init__(**kwargs) | |
self.capacity = capacity | |
class Vehicle: | |
def __init__( | |
self, | |
capacity: int = 10 | |
): | |
self.capacity = capacity | |
class Problem: | |
def __init__(self): | |
self._jobs = [] | |
def add_job(self, job): | |
if not isinstance(job, Job): | |
raise TypeError("Not a job!") | |
self._jobs.append(job) | |
def show_job_timeslots(self): | |
if self._jobs: | |
for item in self._jobs: | |
item.get_human_timeslot() | |
else: | |
print("No jobs exist") | |
problem_a = Problem() | |
problem_b = Problem() | |
for x in range(5): | |
start_time = random.randint(300, 1100) | |
job = Job( | |
service_id=str(random.randint(100, 999)), | |
start_time=start_time, | |
end_time=start_time + random.randint(0, 60), | |
duration=random.randint(10, 30) | |
) | |
problem_a.add_job(job) | |
delivery = Delivery( | |
service_id=str(random.randint(100, 999)), | |
start_time=start_time, | |
end_time=start_time + random.randint(0, 60), | |
duration=random.randint(10, 30), | |
capacity=random.randint(1, 15) | |
) | |
problem_a.add_job(delivery) | |
problem_a.show_job_timeslots() | |
print() | |
# This breaks the interface contract | |
# car_1 = Vehicle() | |
# problem_a.add_job(car_1) | |
# This is empty. We never added anything to this *instance* of Problem | |
problem_b.show_job_timeslots() | |
print("#############") | |
# Checking for instance hierarchy | |
a_job = Job( | |
service_id=str(random.randint(100, 999)), | |
start_time=start_time, | |
end_time=start_time + random.randint(0, 60), | |
duration=random.randint(10, 30) | |
) | |
a_delivery = Delivery( | |
service_id=str(random.randint(100, 999)), | |
start_time=start_time, | |
end_time=start_time + random.randint(0, 60), | |
duration=random.randint(10, 30), | |
capacity=random.randint(1, 15) | |
) | |
print(isinstance(a_job, Job)) | |
print(isinstance(a_delivery, Job)) | |
print(isinstance(a_job, Delivery)) | |
This file contains 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
from example import problem_a, Delivery, SOME_VARIABLE | |
# Try hashing out ALL of the code below and run this file. Note that you still | |
# get things printed out. That's because the act of importing a module will | |
# also run its full contents! That's where `if __name__ == 'main__':` guard | |
# comes in | |
import random | |
start_time = random.randint(1, 40) | |
# This job will be in the evening, unlike all the others | |
new_job = Delivery( | |
service_id=str(random.randint(100, 999)), | |
start_time=start_time, | |
end_time=start_time + random.randint(0, 60), | |
duration=random.randint(10, 30), | |
capacity=random.randint(1, 15) | |
) | |
problem_a.add_job(new_job) | |
problem_a.show_job_timeslots() | |
print(SOME_VARIABLE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment