Skip to content

Commit

Permalink
Addind support for sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
gtkacz committed Jun 17, 2024
1 parent e3c3707 commit b6f5b18
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 51 deletions.
14 changes: 1 addition & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Utilitário `generate_voter_id` [#220](https://github.com/brazilian-utils/brutils-python/pull/220)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-dateutil
python-dateutil
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion temporal_adjuster/common/decorators/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .sequence_processor import sequenceable
from .sequence_processor import sequenceable
13 changes: 10 additions & 3 deletions temporal_adjuster/common/decorators/sequence_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand All @@ -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]:
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions temporal_adjuster/temporal_adjuster.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .modules import (_TemporalAdjusterForFirstAndLastDays,
_TemporalAdjusterForWeekday)
from .modules import _TemporalAdjusterForFirstAndLastDays, _TemporalAdjusterForWeekday


class TemporalAdjuster(
Expand Down Expand Up @@ -30,4 +29,5 @@ class TemporalAdjuster(
```
"""

pass
4 changes: 2 additions & 2 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
coverage
pandas
pre-commit
python-dateutil
coverage
pandas
63 changes: 52 additions & 11 deletions tests/test_sequenceable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
)
)
23 changes: 7 additions & 16 deletions tests/test_weekday_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b6f5b18

Please sign in to comment.