From b6f5b18484f631d9ebf339c361eafee2cdfc447a Mon Sep 17 00:00:00 2001 From: gtkacz Date: Mon, 17 Jun 2024 14:53:40 -0300 Subject: [PATCH] Addind support for sequences --- .pre-commit-config.yaml | 14 +---- CHANGELOG.md | 2 +- requirements.txt | 2 +- setup.py | 2 +- .../common/decorators/__init__.py | 2 +- .../common/decorators/sequence_processor.py | 13 +++- temporal_adjuster/temporal_adjuster.py | 4 +- tests/requirements.txt | 4 +- tests/test_sequenceable.py | 63 +++++++++++++++---- tests/test_weekday_operations.py | 23 +++---- 10 files changed, 78 insertions(+), 51 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2e7fbab..e88cf5c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,29 +5,17 @@ repos: rev: v3.2.0 hooks: - id: trailing-whitespace - stages: [push] - id: end-of-file-fixer - stages: [push] - id: check-yaml - stages: [push] - id: check-added-large-files - stages: [push] - id: check-ast - stages: [push] - id: check-symlinks - stages: [push] - id: debug-statements - stages: [push] - - id: double-quote-string-fixer - stages: [push] + # - id: double-quote-string-fixer - id: end-of-file-fixer - stages: [push] - id: requirements-txt-fixer - stages: [push] - id: trailing-whitespace - stages: [push] - repo: https://github.com/psf/black rev: 24.4.2 hooks: - id: black - stages: [push] diff --git a/CHANGELOG.md b/CHANGELOG.md index 804ea78..51133d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,4 +13,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Utilitário `generate_voter_id` [#220](https://github.com/brazilian-utils/brutils-python/pull/220) \ No newline at end of file +- Utilitário `generate_voter_id` [#220](https://github.com/brazilian-utils/brutils-python/pull/220) diff --git a/requirements.txt b/requirements.txt index 4ea05ed..0f08daa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -python-dateutil \ No newline at end of file +python-dateutil diff --git a/setup.py b/setup.py index e7650f7..f57b748 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ with open("README.md", "r") as fh: long_description = fh.read() - + with open("requirements.txt", "r") as fh: requirements = fh.readlines() diff --git a/temporal_adjuster/common/decorators/__init__.py b/temporal_adjuster/common/decorators/__init__.py index 1fdb054..b9fa417 100644 --- a/temporal_adjuster/common/decorators/__init__.py +++ b/temporal_adjuster/common/decorators/__init__.py @@ -1 +1 @@ -from .sequence_processor import sequenceable \ No newline at end of file +from .sequence_processor import sequenceable diff --git a/temporal_adjuster/common/decorators/sequence_processor.py b/temporal_adjuster/common/decorators/sequence_processor.py index 4006339..357fd3e 100644 --- a/temporal_adjuster/common/decorators/sequence_processor.py +++ b/temporal_adjuster/common/decorators/sequence_processor.py @@ -2,13 +2,14 @@ from functools import wraps from typing import Callable, Sequence, TypeVar, Union -T = TypeVar('T') +T = TypeVar("T") def sequenceable(target: str): """ This decorator is used to process if a sequence of values passed as an argument to a function. The function is called for each value in the sequence, and the result is stored in the same position in the sequence. """ + def decorator(func: Callable[..., T]) -> Callable[..., Union[T, Sequence[T]]]: @wraps(func) def wrapper(*args, **kwargs) -> Union[T, Sequence[T]]: @@ -20,7 +21,11 @@ def wrapper(*args, **kwargs) -> Union[T, Sequence[T]]: # Determine if the target parameter is in args or kwargs target_value = bound_args.arguments.get(target) - if target_value is not None and hasattr(target_value, '__iter__') and not isinstance(target_value, str): + if ( + target_value is not None + and hasattr(target_value, "__iter__") + and not isinstance(target_value, str) + ): convert_back = None if type(target_value) in [set, tuple]: @@ -32,7 +37,9 @@ def wrapper(*args, **kwargs) -> Union[T, Sequence[T]]: result = func(*bound_args.args, **bound_args.kwargs) target_value[index] = result - return target_value if convert_back is None else convert_back(target_value) + return ( + target_value if convert_back is None else convert_back(target_value) + ) else: return func(*args, **kwargs) diff --git a/temporal_adjuster/temporal_adjuster.py b/temporal_adjuster/temporal_adjuster.py index 39850f5..6b9f7e6 100644 --- a/temporal_adjuster/temporal_adjuster.py +++ b/temporal_adjuster/temporal_adjuster.py @@ -1,5 +1,4 @@ -from .modules import (_TemporalAdjusterForFirstAndLastDays, - _TemporalAdjusterForWeekday) +from .modules import _TemporalAdjusterForFirstAndLastDays, _TemporalAdjusterForWeekday class TemporalAdjuster( @@ -30,4 +29,5 @@ class TemporalAdjuster( ``` """ + pass diff --git a/tests/requirements.txt b/tests/requirements.txt index 6975e21..ef1da94 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,4 +1,4 @@ +coverage +pandas pre-commit python-dateutil -coverage -pandas \ No newline at end of file diff --git a/tests/test_sequenceable.py b/tests/test_sequenceable.py index 81306d2..a5b7571 100644 --- a/tests/test_sequenceable.py +++ b/tests/test_sequenceable.py @@ -12,30 +12,71 @@ class TestSequenceable(TestCase): # Weekday operations module def test_next_success(self): tests = [ - (Weekday.SATURDAY, [date(2024, 6, 13), date(2024, 6, 15)], [date(2024, 6, 15), date(2024, 6, 22)], self.assertListEqual), - (Weekday.SATURDAY, set([date(2024, 6, 13), date(2024, 6, 15)]), set([date(2024, 6, 15), date(2024, 6, 22)]), self.assertSetEqual), - (Weekday.SATURDAY, tuple([date(2024, 6, 13), date(2024, 6, 15)]), tuple([date(2024, 6, 15), date(2024, 6, 22)]), self.assertTupleEqual), - (Weekday.SATURDAY, Series([date(2024, 6, 13), date(2024, 6, 15)]), Series([date(2024, 6, 15), date(2024, 6, 22)]), assert_series_equal), + ( + Weekday.SATURDAY, + [date(2024, 6, 13), date(2024, 6, 15)], + [date(2024, 6, 15), date(2024, 6, 22)], + self.assertListEqual, + ), + ( + Weekday.SATURDAY, + set([date(2024, 6, 13), date(2024, 6, 15)]), + set([date(2024, 6, 15), date(2024, 6, 22)]), + self.assertSetEqual, + ), + ( + Weekday.SATURDAY, + tuple([date(2024, 6, 13), date(2024, 6, 15)]), + tuple([date(2024, 6, 15), date(2024, 6, 22)]), + self.assertTupleEqual, + ), + ( + Weekday.SATURDAY, + Series([date(2024, 6, 13), date(2024, 6, 15)]), + Series([date(2024, 6, 15), date(2024, 6, 22)]), + assert_series_equal, + ), ] for index, test in enumerate(tests): with self.subTest( f"Testing method next (subtest {index}) with inputs: {test}" ): - test_input_weekday, test_input_date, test_expected_output, assertion_method = test + ( + test_input_weekday, + test_input_date, + test_expected_output, + assertion_method, + ) = test assertion_method( TemporalAdjuster.next(test_input_weekday, test_input_date), test_expected_output, ) - # First and last day operations module + # First and last day operations module def test_first_day_of_next_week_success(self): tests = [ - ([date(2024, 6, 13), date(2024, 12, 31)], [date(2024, 6, 17), date(2025, 1, 6)], self.assertListEqual), - (set([date(2024, 6, 13), date(2024, 12, 31)]), set([date(2024, 6, 17), date(2025, 1, 6)]), self.assertSetEqual), - (tuple([date(2024, 6, 13), date(2024, 12, 31)]), tuple([date(2024, 6, 17), date(2025, 1, 6)]), self.assertTupleEqual), - (Series([date(2024, 6, 13), date(2024, 12, 31)]), Series([date(2024, 6, 17), date(2025, 1, 6)]), assert_series_equal), + ( + [date(2024, 6, 13), date(2024, 12, 31)], + [date(2024, 6, 17), date(2025, 1, 6)], + self.assertListEqual, + ), + ( + set([date(2024, 6, 13), date(2024, 12, 31)]), + set([date(2024, 6, 17), date(2025, 1, 6)]), + self.assertSetEqual, + ), + ( + tuple([date(2024, 6, 13), date(2024, 12, 31)]), + tuple([date(2024, 6, 17), date(2025, 1, 6)]), + self.assertTupleEqual, + ), + ( + Series([date(2024, 6, 13), date(2024, 12, 31)]), + Series([date(2024, 6, 17), date(2025, 1, 6)]), + assert_series_equal, + ), ] for index, test in enumerate(tests): @@ -47,4 +88,4 @@ def test_first_day_of_next_week_success(self): assertion_method( TemporalAdjuster.first_day_of_next_week(test_input_date), test_expected_output, - ) \ No newline at end of file + ) diff --git a/tests/test_weekday_operations.py b/tests/test_weekday_operations.py index 6c8efff..92d4c78 100644 --- a/tests/test_weekday_operations.py +++ b/tests/test_weekday_operations.py @@ -52,8 +52,7 @@ def test_next_or_same_success(self): test_input_weekday, test_input_date, test_expected_output = test self.assertEqual( - TemporalAdjuster.next_or_same( - test_input_weekday, test_input_date), + TemporalAdjuster.next_or_same(test_input_weekday, test_input_date), test_expected_output, ) @@ -103,8 +102,7 @@ def test_last_or_same_success(self): test_input_weekday, test_input_date, test_expected_output = test self.assertEqual( - TemporalAdjuster.last_or_same( - test_input_weekday, test_input_date), + TemporalAdjuster.last_or_same(test_input_weekday, test_input_date), test_expected_output, ) @@ -208,8 +206,7 @@ def test_last_of_month_success(self): test_input_weekday, test_input_date, test_expected_output = test self.assertEqual( - TemporalAdjuster.last_of_month( - test_input_weekday, test_input_date), + TemporalAdjuster.last_of_month(test_input_weekday, test_input_date), test_expected_output, ) @@ -288,8 +285,7 @@ def test_first_of_year_success(self): test_input_weekday, test_input_date, test_expected_output = test self.assertEqual( - TemporalAdjuster.first_of_year( - test_input_weekday, test_input_date), + TemporalAdjuster.first_of_year(test_input_weekday, test_input_date), test_expected_output, ) @@ -335,8 +331,7 @@ def test_last_of_year_success(self): test_input_weekday, test_input_date, test_expected_output = test self.assertEqual( - TemporalAdjuster.last_of_year( - test_input_weekday, test_input_date), + TemporalAdjuster.last_of_year(test_input_weekday, test_input_date), test_expected_output, ) @@ -527,9 +522,7 @@ def test_nth_of_month_exception_no_nth_weekday(self): with self.subTest( f"Testing method nth_of_month (subtest {index}) with inputs: {test}" ): - test_input_weekday, test_input_date, test_input_n = ( - test - ) + test_input_weekday, test_input_date, test_input_n = test with self.assertRaises(Exception) as context: TemporalAdjuster.nth_of_month( @@ -606,9 +599,7 @@ def test_nth_of_year_exception_no_nth_weekday(self): with self.subTest( f"Testing method nth_of_year (subtest {index}) with inputs: {test}" ): - test_input_weekday, test_input_date, test_input_n = ( - test - ) + test_input_weekday, test_input_date, test_input_n = test with self.assertRaises(Exception) as context: TemporalAdjuster.nth_of_year(