Skip to content

Commit

Permalink
Add FestivalLibrary.extend_term_festivals
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Dec 19, 2023
1 parent 26be8b7 commit 6ee1851
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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':
Expand Down
24 changes: 17 additions & 7 deletions docs/guides/festivals2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 方法不同取值的返回的结果。
Expand Down Expand Up @@ -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 的天数及其日期。
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_festival_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
8 changes: 8 additions & 0 deletions tests/test_lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6ee1851

Please sign in to comment.