Skip to content

Commit ba37d80

Browse files
Format, write notes and add type hints for sequence.py
1 parent ed5bacd commit ba37d80

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[MASTER]
22
py-version=3.5
3-
disable=R0902,R0913,R0917,R0912
3+
disable=R0902,R0903,R0913,R0917,R0912

cyaron/sequence.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,73 @@
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+
216

317
class Sequence:
4-
"""Class Sequence: the tool class for sequences.
5-
"""
18+
"""A class for creating and managing sequences."""
619

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.
1130
"""
1231
if not callable(formula):
13-
raise Exception("formula must be a function")
32+
raise TypeError("formula must be a function")
1433
self.formula = formula
1534
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)))
1738
elif isinstance(initial_values, dict):
1839
self.values = initial_values
1940
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.")
2143

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+
"""
2354
if i in self.values:
2455
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)
2757
return self.values[i]
2858

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+
"""
3071
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

Comments
 (0)