|
1 | | -from .utils import * |
| 1 | +""" |
| 2 | +This module provides a `Sequence` class for generating sequences |
| 3 | +based on a given formula and initial values. |
| 4 | +
|
| 5 | +Classes: |
| 6 | + Sequence: A class for creating and managing sequences. |
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Callable, Dict, List, Optional, Tuple, TypeVar, Union |
| 10 | +from typing import cast as typecast |
| 11 | + |
| 12 | +from .utils import list_like |
| 13 | + |
| 14 | +T = TypeVar('T') |
| 15 | + |
2 | 16 |
|
3 | 17 | class Sequence: |
4 | | - """Class Sequence: the tool class for sequences. |
5 | | - """ |
| 18 | + """A class for creating and managing sequences.""" |
6 | 19 |
|
7 | | - def __init__(self, formula, initial_values=()): |
8 | | - """__init__(self, formula, initial_values=() -> None |
9 | | - Create a sequence object. |
10 | | - int formula(int, function) -> the formula function ... |
| 20 | + def __init__(self, |
| 21 | + formula: Callable[[int, Callable[[int], T]], T], |
| 22 | + initial_values: Optional[Union[List[T], Tuple[T, ...], |
| 23 | + Dict[int, T]]] = ()): |
| 24 | + """ |
| 25 | + Initialize a sequence object. |
| 26 | + Parameters: |
| 27 | + formula: A function that defines the formula for the sequence. |
| 28 | + initial_values (optional): Initial values for the sequence. |
| 29 | + Can be a list, tuple, or dictionary. Defaults to an empty tuple. |
11 | 30 | """ |
12 | 31 | if not callable(formula): |
13 | | - raise Exception("formula must be a function") |
| 32 | + raise TypeError("formula must be a function") |
14 | 33 | self.formula = formula |
15 | 34 | if list_like(initial_values): |
16 | | - self.values = dict(enumerate(initial_values)) |
| 35 | + self.values = dict( |
| 36 | + enumerate( |
| 37 | + typecast(Union[List[T], Tuple[T, ...]], initial_values))) |
17 | 38 | elif isinstance(initial_values, dict): |
18 | 39 | self.values = initial_values |
19 | 40 | else: |
20 | | - raise Exception("Initial_values must be either a list/tuple or a dict.") |
| 41 | + raise TypeError( |
| 42 | + "Initial_values must be either a list/tuple or a dict.") |
21 | 43 |
|
22 | | - def __get_one(self, i): |
| 44 | + def get_one(self, i: int): |
| 45 | + """ |
| 46 | + Retrieve the value at the specified index, computing it if necessary. |
| 47 | + Args: |
| 48 | + i (int): The index of the value to retrieve. |
| 49 | + Returns: |
| 50 | + The value at the specified index. |
| 51 | + If the value at the specified index is not already computed, it will be |
| 52 | + calculated using the provided formula and stored for future access. |
| 53 | + """ |
23 | 54 | if i in self.values: |
24 | 55 | return self.values[i] |
25 | | - |
26 | | - self.values[i] = self.formula(i, self.__get_one) |
| 56 | + self.values[i] = self.formula(i, self.get_one) |
27 | 57 | return self.values[i] |
28 | 58 |
|
29 | | - def get(self, left_range, right_range=None): |
| 59 | + def get(self, left_range: int, right_range: Optional[int] = None): |
| 60 | + """ |
| 61 | + Retrieve a sequence of elements within the specified range. |
| 62 | + If only `left_range` is provided, a single element is retrieved. |
| 63 | + If both `left_range` and `right_range` are provided, a list of elements |
| 64 | + from `left_range` to `right_range` (inclusive) is retrieved. |
| 65 | + Args: |
| 66 | + left_range: The starting index or the single index to retrieve. |
| 67 | + right_range (optional): The ending index for the range retrieval. Defaults to None. |
| 68 | + Returns: |
| 69 | + A single element if `right_range` is None, otherwise a list of elements. |
| 70 | + """ |
30 | 71 | if right_range is None: |
31 | | - return self.__get_one(left_range) |
32 | | - |
33 | | - return [self.__get_one(i) for i in range(left_range, right_range+1)] |
| 72 | + return self.get_one(left_range) |
| 73 | + return [self.get_one(i) for i in range(left_range, right_range + 1)] |
0 commit comments