-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format, write notes and add type hints for sequence.py
- Loading branch information
1 parent
ed5bacd
commit ba37d80
Showing
2 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[MASTER] | ||
py-version=3.5 | ||
disable=R0902,R0913,R0917,R0912 | ||
disable=R0902,R0903,R0913,R0917,R0912 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,73 @@ | ||
from .utils import * | ||
""" | ||
This module provides a `Sequence` class for generating sequences | ||
based on a given formula and initial values. | ||
Classes: | ||
Sequence: A class for creating and managing sequences. | ||
""" | ||
|
||
from typing import Callable, Dict, List, Optional, Tuple, TypeVar, Union | ||
from typing import cast as typecast | ||
|
||
from .utils import list_like | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
class Sequence: | ||
"""Class Sequence: the tool class for sequences. | ||
""" | ||
"""A class for creating and managing sequences.""" | ||
|
||
def __init__(self, formula, initial_values=()): | ||
"""__init__(self, formula, initial_values=() -> None | ||
Create a sequence object. | ||
int formula(int, function) -> the formula function ... | ||
def __init__(self, | ||
formula: Callable[[int, Callable[[int], T]], T], | ||
initial_values: Optional[Union[List[T], Tuple[T, ...], | ||
Dict[int, T]]] = ()): | ||
""" | ||
Initialize a sequence object. | ||
Parameters: | ||
formula: A function that defines the formula for the sequence. | ||
initial_values (optional): Initial values for the sequence. | ||
Can be a list, tuple, or dictionary. Defaults to an empty tuple. | ||
""" | ||
if not callable(formula): | ||
raise Exception("formula must be a function") | ||
raise TypeError("formula must be a function") | ||
self.formula = formula | ||
if list_like(initial_values): | ||
self.values = dict(enumerate(initial_values)) | ||
self.values = dict( | ||
enumerate( | ||
typecast(Union[List[T], Tuple[T, ...]], initial_values))) | ||
elif isinstance(initial_values, dict): | ||
self.values = initial_values | ||
else: | ||
raise Exception("Initial_values must be either a list/tuple or a dict.") | ||
raise TypeError( | ||
"Initial_values must be either a list/tuple or a dict.") | ||
|
||
def __get_one(self, i): | ||
def get_one(self, i: int): | ||
""" | ||
Retrieve the value at the specified index, computing it if necessary. | ||
Args: | ||
i (int): The index of the value to retrieve. | ||
Returns: | ||
The value at the specified index. | ||
If the value at the specified index is not already computed, it will be | ||
calculated using the provided formula and stored for future access. | ||
""" | ||
if i in self.values: | ||
return self.values[i] | ||
|
||
self.values[i] = self.formula(i, self.__get_one) | ||
self.values[i] = self.formula(i, self.get_one) | ||
return self.values[i] | ||
|
||
def get(self, left_range, right_range=None): | ||
def get(self, left_range: int, right_range: Optional[int] = None): | ||
""" | ||
Retrieve a sequence of elements within the specified range. | ||
If only `left_range` is provided, a single element is retrieved. | ||
If both `left_range` and `right_range` are provided, a list of elements | ||
from `left_range` to `right_range` (inclusive) is retrieved. | ||
Args: | ||
left_range: The starting index or the single index to retrieve. | ||
right_range (optional): The ending index for the range retrieval. Defaults to None. | ||
Returns: | ||
A single element if `right_range` is None, otherwise a list of elements. | ||
""" | ||
if right_range is None: | ||
return self.__get_one(left_range) | ||
|
||
return [self.__get_one(i) for i in range(left_range, right_range+1)] | ||
return self.get_one(left_range) | ||
return [self.get_one(i) for i in range(left_range, right_range + 1)] |