Skip to content

Commit

Permalink
✨ Make solar&lunar readonly for WrappedDate
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Jan 26, 2024
1 parent fb66fad commit 7cc0a84
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
21 changes: 11 additions & 10 deletions borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,25 @@ class FestivalSchema(enum.IntEnum):

class WrappedDate:
"""A date object with solar and lunar calendars."""
__slots__ = ['solar', 'lunar', 'name', '_fl']
__slots__ = ['_solar', '_lunar', 'name', '_fl']

def __init__(self, date_obj: MixedDate, name: str = ''):
self.solar = LCalendars.cast_date(date_obj, date)
self.lunar = LCalendars.cast_date(date_obj, LunarDate)
self._solar = LCalendars.cast_date(date_obj, date)
self._lunar = LCalendars.cast_date(date_obj, LunarDate)
self.name = name
if isinstance(date_obj, date):
self._fl = 's'
else:
self._fl = 'l'

@property
def solar(self):
return self._solar

@property
def lunar(self):
return self._lunar

def __iter__(self):
yield self.solar
yield self.lunar
Expand Down Expand Up @@ -122,13 +130,6 @@ def __hash__(self):
def __eq__(self, other):
return isinstance(self, type(other)) and self.__key() == other.__key()

def __getstate__(self):
return self.__key()

def __setstate__(self, state):
_year, _month, _day = state
self.solar = date(_year, _month, _day)

def full_str(self):
return f'{self.solar}({self.lunar.cn_str()})'

Expand Down
6 changes: 2 additions & 4 deletions borax/capp/borax_calendar_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

library = FestivalLibrary.load_builtin().sort_by_countdown()

__VERSION__ = '1.1.0'

PROJECT_URLS = {
'home': 'https://github.com/kinegratii/borax'
}
Expand Down Expand Up @@ -347,7 +345,7 @@ def start_festival_dlg(self):
self._create_festival_dialog()

def show_about_info(self):
showinfo('关于', f' 日历v{__VERSION__}\n\n Powered by Borax{borax_version}')
showinfo('关于', f' 日历v{borax_version}\n\n Powered by Borax{borax_version}')


def start_calendar_app():
Expand All @@ -356,7 +354,7 @@ def start_calendar_app():
x, y = int(root.winfo_screenwidth() / 2 - rw / 2), int(root.winfo_screenheight() / 2 - rh / 2)
root.geometry(f"{rw}x{rh}+{x}+{y}")
root.resizable(False, False)
root.title(f'日历 - v{__VERSION__}')
root.title(f'日历 - v{borax_version}')
global style
style = ttk.Style(root)
# style.theme_use('alt')
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- 新增方法 `FestivalLibrary.load`
- 新增 `borax.ui.widgets.MessageLabel`
- `Festival` 类新增 `code` 属性
- `WrappedDate.solar``WrappedDate.lunar` 属性变更为只读属性,不可写入
- 项目构建
- 不再支持python3.7和python3.8
- 本地开发环境更新至 python3.11.7
Expand Down
4 changes: 4 additions & 0 deletions mkdocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ theme:
- navigation.tabs
- navigation.sections
- navigation.footer
validation:
links:
absolute_links: ignore
unrecognized_links: ignore
extra:
social:
- icon: fontawesome/brands/github
Expand Down
31 changes: 22 additions & 9 deletions tests/test_date_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
import pickle
from io import BytesIO

from datetime import date

from borax.calendars.lunardate import LunarDate
from borax.calendars.festivals2 import WrappedDate


class WrappedDateBasicTestCase(unittest.TestCase):
def test_wrapped_date(self):
ld = LunarDate.today()
wd = WrappedDate(ld)
self.assertEqual(ld, wd.lunar)
with self.assertRaises(AttributeError):
wd.lunar = LunarDate(2024, 1, 1)


class DatePickleTestCase(unittest.TestCase):

def test_wd(self):
def test_lunardate_pickle(self):
ld1 = LunarDate.today()
fp = BytesIO()

Expand All @@ -18,11 +29,13 @@ def test_wd(self):
e_ld = pickle.load(fp)
self.assertEqual(ld1, e_ld)

wd1 = WrappedDate(ld1)
fp2 = BytesIO()

pickle.dump(wd1, fp2)

fp2.seek(0)
wd2 = pickle.load(fp2)
self.assertEqual(wd2, wd1)
def test_wrapped_date_pickle(self):
wd_list = [WrappedDate(date.today()), WrappedDate(LunarDate.today())]
for wd in wd_list:
with self.subTest(wd=wd):
fp = BytesIO()
pickle.dump(wd, fp)
fp.seek(0)
new_wd = pickle.load(fp)
self.assertEqual(wd.solar, new_wd.solar)
self.assertEqual(wd.lunar, new_wd.lunar)

0 comments on commit 7cc0a84

Please sign in to comment.