From 553862fdadbbf883d5c344188bda4f7727551ac7 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 24 Aug 2022 09:59:34 -0700 Subject: [PATCH] [FEATURE] Add day_of_year variables (#195) --- .../entries/variables/entry_variables.py | 69 ++++++++++++++++--- tests/unit/entries/conftest.py | 4 ++ tests/unit/entries/test_entry.py | 17 +++++ 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/ytdl_sub/entries/variables/entry_variables.py b/src/ytdl_sub/entries/variables/entry_variables.py index 2b56572d..faa3b646 100644 --- a/src/ytdl_sub/entries/variables/entry_variables.py +++ b/src/ytdl_sub/entries/variables/entry_variables.py @@ -8,12 +8,14 @@ from ytdl_sub.entries.base_entry import BaseEntry # This file contains mixins to a BaseEntry subclass. Ignore pylint's "no kwargs member" suggestion # pylint: disable=no-member +# pylint: disable=too-many-public-methods -def _pad(num): - if num < 10: - return f"0{num}" - return str(num) +def _pad(num: int, width: int = 2): + return str(num).zfill(width) + + +_days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] class SourceVariables: @@ -228,13 +230,9 @@ class EntryVariables(SourceVariables): The upload day, but reversed using ``{total_days_in_month} + 1 - {upload_day}``, i.e. August 8th would have upload_day_reversed of ``31 + 1 - 8`` = ``24`` """ - total_days_in_month: int = 30 - if self.upload_month in [1, 3, 5, 7, 8, 10, 12]: - total_days_in_month = 31 - elif self.upload_month == 2: - total_days_in_month = 28 - if self.upload_year % 4 == 0: # leap year - total_days_in_month = 29 + total_days_in_month = _days_in_month[self.upload_month] + if self.upload_month == 2 and self.upload_year % 4 == 0: # leap year + total_days_in_month += 1 return total_days_in_month + 1 - self.upload_day @@ -248,6 +246,55 @@ class EntryVariables(SourceVariables): """ return _pad(self.upload_day_reversed) + @property + def upload_day_of_year(self) -> int: + """ + Returns + ------- + int + The day of the year, i.e. February 1st returns ``32`` + """ + output = sum(_days_in_month[: self.upload_month]) + self.upload_day + if self.upload_month > 2 and self.upload_year % 4 == 0: + output += 1 + + return output + + @property + def upload_day_of_year_padded(self) -> str: + """ + Returns + ------- + str + The upload day of year, but padded i.e. February 1st returns "032" + """ + return _pad(self.upload_day_of_year, width=3) + + @property + def upload_day_of_year_reversed(self) -> int: + """ + Returns + ------- + int + The upload day, but reversed using ``{total_days_in_year} + 1 - {upload_day}``, + i.e. February 2nd would have upload_day_of_year_reversed of ``365 + 1 - 32`` = ``334`` + """ + total_days_in_year = 365 + if self.upload_year % 4 == 0: + total_days_in_year += 1 + + return total_days_in_year + 1 - self.upload_day_of_year + + @property + def upload_day_of_year_reversed_padded(self) -> str: + """ + Returns + ------- + str + The reversed upload day of year, but padded i.e. December 31st returns "001" + """ + return _pad(self.upload_day_of_year_reversed, width=3) + @property def upload_date_standardized(self) -> str: """ diff --git a/tests/unit/entries/conftest.py b/tests/unit/entries/conftest.py index 84a97b1e..1c0fe0bb 100644 --- a/tests/unit/entries/conftest.py +++ b/tests/unit/entries/conftest.py @@ -72,6 +72,10 @@ def mock_entry_to_dict( "upload_day_padded": "12", "upload_day_reversed": 20, "upload_day_reversed_padded": "20", + "upload_day_of_year": 12, + "upload_day_of_year_padded": "012", + "upload_day_of_year_reversed": 354, + "upload_day_of_year_reversed_padded": "354", "thumbnail_ext": thumbnail_ext, } diff --git a/tests/unit/entries/test_entry.py b/tests/unit/entries/test_entry.py index d1650401..4885420d 100644 --- a/tests/unit/entries/test_entry.py +++ b/tests/unit/entries/test_entry.py @@ -36,3 +36,20 @@ class TestEntry(object): assert mock_entry.upload_month_reversed_padded == month_rev_pad assert mock_entry.upload_day_reversed_padded == day_rev_pad + + @pytest.mark.parametrize( + "upload_date, day_year, day_year_rev, day_year_pad, day_year_rev_pad", + [ + ("20000228", 59, 308, "059", "308"), + ("20210808", 220, 146, "220", "146"), + ], + ) + def test_entry_upload_day_of_year_variables( + self, mock_entry, upload_date, day_year, day_year_rev, day_year_pad, day_year_rev_pad + ): + mock_entry._kwargs["upload_date"] = upload_date + + assert mock_entry.upload_day_of_year == day_year + assert mock_entry.upload_day_of_year_reversed == day_year_rev + assert mock_entry.upload_day_of_year_padded == day_year_pad + assert mock_entry.upload_day_of_year_reversed_padded == day_year_rev_pad