Skip to content

Commit

Permalink
✨ Add Festival.code & Upgrade to python3.9+
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Jan 24, 2024
1 parent a6fd5eb commit b495518
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
Expand Down
24 changes: 16 additions & 8 deletions borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import enum
import warnings
from datetime import date, timedelta, datetime
from functools import cached_property
from pathlib import Path
from typing import List, Tuple, Optional, Union, Iterator, Set, Generator, Sequence

Expand Down Expand Up @@ -149,11 +150,11 @@ def from_simple_str(cls, date_str: str) -> 'WrappedDate':
def encode(self) -> str:
if self._fl == 'l':
festival = LunarFestival(month=self.lunar.month, day=self.lunar.day, leap=self.lunar.leap)
encoded_str = festival.encode()
encoded_str = festival.code
return '{}{:04d}{}'.format(encoded_str[0], self.lunar.year, encoded_str[1:])
else:
festival = SolarFestival(month=self.solar.month, day=self.solar.day)
encoded_str = festival.encode()
encoded_str = festival.code
return '{}{:04d}{}'.format(encoded_str[0], self.solar.year, encoded_str[1:])

@classmethod
Expand Down Expand Up @@ -231,6 +232,10 @@ def set_name(self, name):
self._name = name
return self

@cached_property
def code(self):
return self.encode()

@property
def schema(self):
return self._schema
Expand Down Expand Up @@ -980,19 +985,22 @@ def print_(self):
def get_code_set(self) -> Set[str]:
"""Get codes for all festivals.
"""
return set([f.encode() for f in self.data])
return set([f.code for f in self.data])

def extend_unique(self, other: Union[collections.UserList, List[str], 'FestivalLibrary']) -> 'FestivalLibrary':
def extend_unique(
self,
other: Union[collections.UserList, List[Union[str, Festival]], 'FestivalLibrary']
) -> 'FestivalLibrary':
"""Add a new festival if code does not exist.
"""
f_codes = {f.encode() for f in self.data}
f_codes = {f.code for f in self.data}
if isinstance(other, collections.UserList):
new_data = other.data
else:
new_data = other
for item in new_data:
if isinstance(item, Festival):
if item.encode() not in f_codes:
if item.code not in f_codes:
self.data.append(item)
elif isinstance(item, str):
try:
Expand Down Expand Up @@ -1160,7 +1168,7 @@ def to_csv(self, path_or_buf):
fileobj = path_or_buf
writer = csv.writer(fileobj)
for festival in self:
row = (festival.encode(), festival.name, festival.catalog)
row = (festival.code, festival.name, festival.catalog)
writer.writerow(row)

@classmethod
Expand All @@ -1185,7 +1193,7 @@ def load_file(cls, file_path: Union[str, Path], unique: bool = False) -> 'Festiv
fl.append(festival)
except ValueError:
continue
fl.sort(key=lambda x: x.encode())
fl.sort(key=lambda x: x.code)
return fl

def filter(self, catalogs: Sequence = None) -> 'FestivalLibrary':
Expand Down
14 changes: 12 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
# 更新日志

## v4.1.0
## v4.1.0 (20240330)

> Borax最低python版本要求为python3.9
[发布日志](/release-note/v410)

- 功能更新
- 新增 [Borax日历应用](/guides/borax_calendar_app)
-`borax.apps` 变更为 `borax.capp`
- 新增方法 `FestivalLibrary.extend_term_festivals`
- 新增 `borax.ui.widgets.MessageLabel`
- `Festival` 类新增 `code` 属性
- 项目构建
- 不再支持python3.7和python3.8
- 本地开发环境更新至 python3.11.7
- 使用 *pyproject.toml* 项目构建配置文件,构建命令 `python -m build -w`
- 支持python3.12
- 项目文档
- mkdocs-material 更新至 9.5.3
- 不再支持 docsify 构建(index.html 冲突)


## v4.0.0 (20221115)

[发布日志](/release-note/v400)

- 新增基于 tkinter 的 [节日界面库](/guides/festivals2-ui)
- 移除源代码文件编码声明行
- 移除 `borax.calendars.festival` 模块
Expand Down
27 changes: 23 additions & 4 deletions docs/guides/festivals2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> 模块: `borax.calendars.festivals2`
> Updated in 4.1.0:新增 Festival.code属性。
> Updated in 3.5.6: 星期型节日(WeekFestival)类支持倒数序号。如:“国际麻风节(1月最后一个星期天)”
> Updated in 3.5.6: 星期型节日(WeekFestival)类支持每月频率。
Expand Down Expand Up @@ -247,6 +249,14 @@ TermFestival(index=0)

## Festival属性

### code

> Add in 4.1.0

类型:str,编码字符串。 `FestivalLibrary` 以此属性作为唯一性的标志。

需要注意的是该属性使用 `cached_property` 进行修饰。

### name

类型:str,节日名称,如“元旦”、“中秋节”等。
Expand Down Expand Up @@ -408,25 +418,34 @@ print(spring_festival.countdown()) # (273, <WrappedDate:2022-02-01(二〇二二

`FestivalLibrary` 是集合容器类,提供了一些常用的节日。此类继承自 `collections.UserList` ,拥有 append/remove/extend/insert等方法。

需要注意的是,`FestivalLibrary` 并不重写这些方法的逻辑,因此如需保证节日不重复,可以使用 `extend_unique` 方法添加。

```python
class FestivalLibrary(collections.UserList):
pass
```

创建一个节日库对象主要有两种方法
创建一个节日库对象主要有三种方法

第一,从 borax 提供默认数据加载。

```python
fl = FestivalLibrary.load_builtin('basic') # 加载基础节日库,可选 empty / basic / ext1
```

第二,从已有的节日创建新的节日库。
第二,从某个 csv 文件加载。

```python
fl = FestivalLibrary.load_file('/usr/amy/festivals/my_festival.csv')
```

第三,从已有的节日创建新的节日库。

```python
fl1 = FestivalLibrary(fl) # 复制 fl节日库

fl2 = FestivalLibrary(filter(lambda f: f.schema == FestivalSchema.SOLAR, fl)) # 使用函数式编程过滤其中的公历型节日
# 使用函数式编程过滤其中的公历型节日
fl2 = FestivalLibrary(filter(lambda f: f.schema == FestivalSchema.SOLAR, fl))
```

### get_code_set
Expand Down Expand Up @@ -480,7 +499,7 @@ FestivalLibrary.load_file(cls, file_path: Union[str, Path]) -> 'FestivalLibrary'
### load_builtin

```python
FestivalLibrary.load_builtin(cls, identifier: str = 'zh-Hans') -> 'FestivalLibrary'
FestivalLibrary.load_builtin(cls, identifier: str = 'basic') -> 'FestivalLibrary'
```

加载Borax提供的节日库数据。
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ authors = [
]
description = "A tool collections.(Chinese-Lunar-Calendars/Python-Patterns)"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.9"
keywords = ["chinese lunar calendar", "python tool"]
license = { text = "MIT License" }
classifiers = [
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
lib_classifiers = [
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3 :: Only",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
Expand All @@ -31,7 +30,7 @@
setup(
name='borax',
version=version,
python_requires='>=3.7',
python_requires='>=3.9',
packages=find_packages(exclude=['tests']),
include_package_data=True,
license='MIT',
Expand Down

0 comments on commit b495518

Please sign in to comment.