Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docstrings and type hints for schedules.Schedule #233

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions ciw/schedules.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
from typing import List, Tuple, Union, Generator, NoReturn

class Schedule:
"""
A Schedule class, for storing information about server schedules and
generating the next shifts.
A Schedule class for storing information about server schedules and generating the next shifts.

Parameters
----------
schedule : List[Tuple[int, float]]
A list of tuples representing shifts, where each tuple contains the number of servers and the shift date.
preemption : Union[bool, str], optional
Pre-emption option, should be either 'resume', 'restart', 'resample', or False. Default is False.

Attributes
----------
schedule_type : str
Type of the schedule.
schedule_dates : List[float]
List of shift dates.
schedule_servers : List[int]
List of corresponding server numbers.
preemption : Union[bool, str]
Pre-emption option.
cyclelength : float
Length of the schedule cycle.

Methods
-------
initialise()
Initializes the generator object at the beginning of a simulation.
get_schedule_generator(boundaries, values)
A generator that yields the next time and number of servers according to a given schedule.
get_next_shift()
Updates the next shifts from the generator.
"""
def __init__(self, schedule, preemption=False):
def __init__(self, schedule: List[Tuple[int, float]], preemption: Union[bool, str] = False) -> NoReturn:
"""
Initialises the instance of the Schedule object
Initializes the instance of the Schedule object.

Parameters
----------
schedule : List[Tuple[int, float]]
A list of tuples representing shifts, where each tuple contains
the number of servers and the shift date.
preemption : Union[bool, str], optional
Pre-emption option, should be either 'resume', 'restart', 'resample', or False.
Default is False.
"""
if preemption not in [False, 'resume', 'restart', 'resample']:
raise ValueError("Pre-emption options should be either 'resume', 'restart', 'resample', or False.")
Expand All @@ -15,17 +54,29 @@ def __init__(self, schedule, preemption=False):
self.preemption = preemption
self.cyclelength = self.schedule_dates[-1]

def initialise(self):
def initialise(self) -> NoReturn:
"""
Initialises the generator object at the beginning of a simulation
Initializes the generator object at the beginning of a simulation.
"""
self.next_c = self.schedule_servers[0]
self.schedule_generator = self.get_schedule_generator(self.schedule_dates, self.schedule_servers)
self.get_next_shift()

def get_schedule_generator(self, boundaries, values):
def get_schedule_generator(self, boundaries: List[float], values:List[int]) -> Generator[Tuple[float, int], None, None]:
"""
A generator that yields the next time and number of servers according to a given schedule.

Parameters
----------
boundaries : List[float]
List of shift boundaries.
values : List[int]
List of corresponding server numbers.

Yields
------
Tuple[float, int]
A tuple representing the next shift date and the number of servers.
"""
num_boundaries = len(boundaries)
index = 0
Expand All @@ -35,9 +86,9 @@ def get_schedule_generator(self, boundaries, values):
index += 1
yield date, values[index % num_boundaries]

def get_next_shift(self):
def get_next_shift(self) -> NoReturn:
"""
Updates the next shifts from the generator
Updates the next shifts from the generator.
"""
self.c = self.next_c
date, c = next(self.schedule_generator)
Expand Down
Loading