-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into pr/OranPie/137
- Loading branch information
Showing
10 changed files
with
250 additions
and
74 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Python package | ||
|
||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
platform: [ubuntu-20.04, windows-latest] | ||
runs-on: ${{ matrix.platform }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python 3.6 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.6' | ||
- name: Set up Python 3.8 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.8' | ||
- name: Set up Python 3.10 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.12' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install tox | ||
- name: Test with tox | ||
run: python -m tox |
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 |
---|---|---|
|
@@ -138,3 +138,5 @@ venv/ | |
|
||
# VS Code | ||
.vscode | ||
|
||
.python-version |
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 | ||
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
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: 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)] |
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import unittest | ||
from cyaron.vector import * | ||
|
||
|
||
def has_duplicates(lst: list): | ||
return len(lst) != len(set(lst)) | ||
|
||
|
||
class TestVector(unittest.TestCase): | ||
def test_unique_vector(self): | ||
v = Vector.random(10**5, [10**6]) | ||
self.assertFalse(has_duplicates(list(map(lambda tp: tuple(tp), v)))) | ||
self.assertTrue(all(map(lambda v: 0 <= v[0] <= 10**6, v))) | ||
v = Vector.random(1000, [(10**5, 10**6)]) | ||
self.assertTrue(all(map(lambda v: 10**5 <= v[0] <= 10**6, v))) | ||
with self.assertRaises( | ||
Exception, | ||
msg="1st param is so large that CYaRon can not generate unique vectors", | ||
): | ||
v = Vector.random(10**5, [10**4]) | ||
|
||
def test_repeatable_vector(self): | ||
v = Vector.random(10**5 + 1, [10**5], VectorRandomMode.repeatable) | ||
self.assertTrue(all(map(lambda v: 0 <= v[0] <= 10**5, v))) | ||
self.assertTrue(has_duplicates(list(map(lambda tp: tuple(tp), v)))) | ||
v = Vector.random(1000, [(10**5, 10**6)], VectorRandomMode.repeatable) | ||
self.assertTrue(all(map(lambda v: 10**5 <= v[0] <= 10**6, v))) | ||
|
||
def test_float_vector(self): | ||
v = Vector.random(10**5, [10**5], VectorRandomMode.float) | ||
self.assertTrue(all(map(lambda v: 0 <= v[0] <= 10**5, v))) | ||
v = Vector.random(10**5, [(24, 25)], VectorRandomMode.float) | ||
self.assertTrue(all(map(lambda v: 24 <= v[0] <= 25, v))) |
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
Oops, something went wrong.