From 6ee18517fc9172a430a62e6215decf8761de2316 Mon Sep 17 00:00:00 2001 From: kinegratii Date: Tue, 19 Dec 2023 20:31:40 +0800 Subject: [PATCH] Add FestivalLibrary.extend_term_festivals --- borax/calendars/festivals2.py | 6 +++++- docs/guides/festivals2.md | 24 +++++++++++++++++------- tests/test_festival_library.py | 5 +++++ tests/test_lunardate.py | 8 ++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/borax/calendars/festivals2.py b/borax/calendars/festivals2.py index e4cf839..8289627 100644 --- a/borax/calendars/festivals2.py +++ b/borax/calendars/festivals2.py @@ -1000,6 +1000,9 @@ def extend_unique(self, other): pass return self + def extend_term_festivals(self): + return self.extend_unique([f'400{i:02d}0' for i in range(24)]) + def delete_by_indexes(self, indexes: list): """Delete items by indexes.""" index_list = sorted(indexes, reverse=True) @@ -1195,7 +1198,8 @@ def filter(self, catalogs: Sequence = None) -> 'FestivalLibrary': def load_term_festivals(self): """Add 24-term festivals.""" - return self.extend_unique([f'400{i:02d}0' for i in range(24)]) + warnings.warn('This function is deprecated. Use extend_term_festivals instead.', DeprecationWarning) + return self.extend_term_festivals() @classmethod def load_builtin(cls, identifier: str = 'basic') -> 'FestivalLibrary': diff --git a/docs/guides/festivals2.md b/docs/guides/festivals2.md index fab0e25..32bbee0 100644 --- a/docs/guides/festivals2.md +++ b/docs/guides/festivals2.md @@ -88,12 +88,12 @@ print(ld) # LunarDate(2020, 11, 18, 0) Period 是一个工具类,提供了一系列方法,这些方法均返回一个包含起始日期和终止日期的二元素元组。 -| 方法 | 描述 | -| -------------------------------------------------------- | ----------------- | -| Period.solar_year(year) | 公历year年 | -| Period.solar_month(year, month) | 公历year年month月 | -| Period.lunar_year(year) | 农历year年 | -| Period.lunar_month(year, month, leap=_IGNORE_LEAP_MONTH) | 农历year年month月 | +| 方法 | 描述 | +| ------------------------------------------------------------ | ----------------- | +| Period.solar_year(year) -> Tuple[date, date] | 公历year年 | +| Period.solar_month(year, month) -> Tuple[date, date] | 公历year年month月 | +| Period.lunar_year(year) -> Tuple[LunarDate, LunarDate] | 农历year年 | +| Period.lunar_month(year, month, leap=_IGNORE_LEAP_MONTH) -> Tuple[LunarDate, LunarDate] | 农历year年month月 | 需要注意的是,当leap为默认值且农历year年month月有闰月时,将返回的是两个月时间段的起始日期。下面是 `lunar_month` 方法不同取值的返回的结果。 @@ -376,7 +376,7 @@ Festival.get_one_day(start_date=None, end_date=None) -> Optional[WrappedDate] ### 倒计时 ```python -Festival.countdown(date_obj: MixedDate = None) -> Tuple[int, Optional[WrappedDate]]) +Festival.countdown(date_obj: MixedDate = None) -> Tuple[int, Optional[WrappedDate]] ``` 计算本 festival 匹配的日期距离 date_obj 的天数及其日期。 @@ -417,6 +417,16 @@ FestivalLibrary.extend_unique(other) 添加多个节日对象,类似于 extend 方法,但是如果code已经存在则不再加入。 +### extend_term_festivals + +> Add in v4.0.1 + +``` +FestivalLibrary.extend_term_festivals() +``` + +添加24个节气节日。 + ### delete_by_indexes > Add in v4.0.0 diff --git a/tests/test_festival_library.py b/tests/test_festival_library.py index 4253640..160558e 100644 --- a/tests/test_festival_library.py +++ b/tests/test_festival_library.py @@ -128,3 +128,8 @@ def test_filter_exclude_copy(self): fl3 = fl.exclude_(schema__in=[FestivalSchema.WEEK, FestivalSchema.TERM]) self.assertEqual(len(fl), len(fl1) + len(fl2)) self.assertEqual(len(fl3), len(fl1)) + + def test_festivals_functional_program(self): + fl = FestivalLibrary.load_builtin() + fl2 = FestivalLibrary(filter(lambda f: f.schema == FestivalSchema.SOLAR, fl)) + self.assertTrue(isinstance(fl2, FestivalLibrary)) diff --git a/tests/test_lunardate.py b/tests/test_lunardate.py index 8e4d6c0..6bfc51e 100644 --- a/tests/test_lunardate.py +++ b/tests/test_lunardate.py @@ -97,6 +97,14 @@ def test_immutable_feature(self): ld1 = LunarDate(2018, 6, 1) ld2 = LunarDate(2018, 6, 1) self.assertEqual(1, len({ld1, ld2})) + # Act as dict keys + dic = {ld1: 'day1'} + self.assertEqual('day1', dic.get(ld1)) + ld3 = LunarDate(2018, 6, 1) + self.assertEqual('day1', dic.get(ld3)) + dic[ld3] = 'day2' + self.assertEqual(1, len(dic)) + self.assertEqual('day2', dic.get(ld1)) def test_term_ganzhi_feature(self): ld = LunarDate(2018, 6, 26)