Skip to content

Commit

Permalink
✨ Add FestivalLibrary.load
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Jan 25, 2024
1 parent e9f4297 commit fb66fad
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions borax/calendars/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

def get_festival_dataset_path(identifier: str) -> Path:
"""Return the full path for festival dataset csv file."""
if identifier not in _FILE_DICT:
raise ValueError(f'Festival Dataset {identifier} not found!')
return Path(__file__).parent / _FILE_DICT.get(identifier)
11 changes: 11 additions & 0 deletions borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,3 +1222,14 @@ def load_builtin(cls, identifier: str = 'basic') -> 'FestivalLibrary':
if identifier == 'empty':
return FestivalLibrary()
return cls.load_file(get_festival_dataset_path(identifier))

@classmethod
def load(cls, identifier_or_path: Union[str, Path]) -> 'FestivalLibrary':
"""Create a FestivalLibrary object from borax builtin dataset or custom file."""
if identifier_or_path == 'empty':
return FestivalLibrary()
try:
path_o = get_festival_dataset_path(identifier_or_path)
return cls.load_file(path_o)
except ValueError:
return cls.load_file(identifier_or_path)
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- 功能更新
- 新增 [Borax日历应用](/guides/borax_calendar_app)
-`borax.apps` 变更为 `borax.capp`
- 新增方法 `FestivalLibrary.extend_term_festivals`
- 新增方法 `FestivalLibrary.extend_term_festivals`
- 新增方法 `FestivalLibrary.load`
- 新增 `borax.ui.widgets.MessageLabel`
- `Festival` 类新增 `code` 属性
- 项目构建
Expand Down
18 changes: 18 additions & 0 deletions docs/guides/festivals2.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ FestivalLibrary.delete_by_indexes(indexes:List[int])

按照位置删除多个元素。

### load

> Add in 4.1.0

```python
FestivalLibrary.load(cls, identifier_or_path: Union[str, Path]) -> 'FestivalLibrary'
```

加载Borax内部数据或自定义文件。

```python
fl = FestivalLibrary.load('basic')

fl2 = FestivalLibrary.load('/usr/my/my_festivals.csv')
```



### load_file

```python
Expand Down
6 changes: 6 additions & 0 deletions tests/test_festival_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def test_library(self):

self.assertIn('元旦', [g.name for g in gd_days])

def test_new_load(self):
fl = FestivalLibrary.load('basic')
self.assertEqual(33, len(fl))
with self.assertRaises(FileNotFoundError):
fl2 = FestivalLibrary.load('not-found')

def test_list_days(self):
fl = FestivalLibrary.load_builtin()
fes_list = []
Expand Down

0 comments on commit fb66fad

Please sign in to comment.