From 5b663a64c49b4cb24da65f0a6a488238847508ee Mon Sep 17 00:00:00 2001 From: lihuanshuai Date: Wed, 20 Sep 2023 17:37:22 +0800 Subject: [PATCH 1/6] add flake8 and pytest workflow --- .flake8 | 2 ++ .github/workflows/python-app.yml | 36 ++++++++++++++++++++++ README.md | 14 ++++----- requirements-test.txt | 3 ++ sample_zhdate.py | 20 +++++++------ setup.py | 8 ++--- test_zhdate.py => tests/test_zhdate.py | 4 ++- zhdate/__init__.py | 41 +++++++++++++++----------- zhdate/constants.py | 10 +++---- 9 files changed, 95 insertions(+), 43 deletions(-) create mode 100644 .flake8 create mode 100644 .github/workflows/python-app.yml create mode 100644 requirements-test.txt rename test_zhdate.py => tests/test_zhdate.py (99%) diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..7da1f96 --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 100 diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..add8987 --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,36 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python application + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.7 + uses: actions/setup-python@v2 + with: + python-version: 3.7 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Install dependencies for testing + run: | + if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --show-source --statistics + - name: Test with pytest + run: | + python -m pytest diff --git a/README.md b/README.md index 853a7a1..615dd02 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,14 @@ pip install zhdate --upgrade ```python from zhdate import ZhDate -date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 +date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 print(date1) # 直接返回农历日期字符串 dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型 dt_date2 = datetime(2010, 2, 6) -date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 +date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 -date3 = ZhDate(2020, 4, 30, leap_month=True) # 新建农历 2020年闰4月30日 +date3 = ZhDate(2020, 4, 30, leap_month=True) # 新建农历 2020年闰4月30日 print(date3.to_datetime()) # 支持比较 @@ -55,12 +55,12 @@ if ZhDate(2019, 1, 1) == ZhDate.from_datetime(datetime(2019, 2, 5)): pass # 减法支持 -new_zhdate = ZhDate(2019, 1, 1) - 30 #减整数,得到差额天数的新农历对象 -new_zhdate2 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) #两个zhdate对象相减得到两个农历日期的差额 -new_zhdate3 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 +new_zhdate = ZhDate(2019, 1, 1) - 30 # 减整数,得到差额天数的新农历对象 +delta1 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) # 两个zhdate对象相减得到两个农历日期的差额 +delta2 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 # 加法支持 -new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 +new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 # 中文输出 new_zhdate5 = ZhDate(2019, 1, 1) diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..f5d95a9 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,3 @@ +flake8==3.9.0 +importlib-metadata==4.13.0 +pytest==6.0.1 diff --git a/sample_zhdate.py b/sample_zhdate.py index 65b7b7d..f0737f4 100644 --- a/sample_zhdate.py +++ b/sample_zhdate.py @@ -1,15 +1,17 @@ +# -*- coding=utf-8 -*- + from zhdate import ZhDate from datetime import datetime if __name__ == '__main__': - date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 + date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 # print(date1) # 直接返回农历日期字符串 - dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型 + dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型 - dt_date2 = datetime(2010, 2, 6) - date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 + dt_date2 = datetime(2010, 2, 6) + date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 - date3 = ZhDate(2020, 4, 29, leap_month=True) # 新建农历 2020年闰4月30日 + date3 = ZhDate(2020, 4, 29, leap_month=True) # 新建农历 2020年闰4月30日 # print(date3.to_datetime()) # 支持比较 @@ -17,12 +19,12 @@ pass # 减法支持 - new_zhdate = ZhDate(2019, 1, 1) - 30 #减整数,得到差额天数的新农历对象 - new_zhdate2 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) #两个zhdate对象相减得到两个农历日期的差额 - new_zhdate3 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 + new_zhdate = ZhDate(2019, 1, 1) - 30 # 减整数,得到差额天数的新农历对象 + delta1 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) # 两个zhdate对象相减得到两个农历日期的差额 + delta2 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 # 加法支持 - new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 + new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 print(ZhDate(1900, 9, 1, False).chinese()) print(ZhDate.today().chinese()) diff --git a/setup.py b/setup.py index 61fc4d6..e77971a 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +# -*- coding=utf-8 -*- + import setuptools ''' @@ -6,7 +8,7 @@ LastEditors : PandaWithBeard LastEditTime : 2023-01-21 02:30:41 FilePath : /zhdate/setup.py -Description : +Description : ''' with open('README.md', 'r') as file: @@ -21,12 +23,10 @@ long_description=long_description, long_description_content_type="text/markdown", url='https://github.com/CutePandaSh/zhdate', - packages=setuptools.find_packages(), + packages=setuptools.find_packages(exclude=['tests']), classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", "Operating System :: OS Independent", ], ) - - \ No newline at end of file diff --git a/test_zhdate.py b/tests/test_zhdate.py similarity index 99% rename from test_zhdate.py rename to tests/test_zhdate.py index 5c65fdd..3092f71 100644 --- a/test_zhdate.py +++ b/tests/test_zhdate.py @@ -1,3 +1,5 @@ +# -*- coding=utf-8 -*- + import unittest from datetime import datetime from zhdate import ZhDate @@ -95,4 +97,4 @@ def test_validate(self): ] for case in test_cases: - self.assertEqual(ZhDate.validate(*case[0]), case[1]) \ No newline at end of file + self.assertEqual(ZhDate.validate(*case[0]), case[1]) diff --git a/zhdate/__init__.py b/zhdate/__init__.py index 8475272..86a1a97 100644 --- a/zhdate/__init__.py +++ b/zhdate/__init__.py @@ -1,13 +1,14 @@ -''' --*- coding: utf-8 -*- +# -*- coding: utf-8 -*- + +""" File: zhdate.py File Created: Sunday, 17th February 2019 4:58:02 pm Author: Wang, Yi (denniswangyi@gmail.com) -''' -''' +----- changed: Saturday, 21st January 2023 by: Eilles Wan (EillesWan@outlook.com) -''' +""" + from datetime import datetime, timedelta from itertools import accumulate from .constants import CHINESENEWYEAR, CHINESEYEARCODE @@ -55,10 +56,10 @@ def from_datetime(dt): ZhDate -- 生成的农历日期对象 """ lunar_year = dt.year - # 如果还没有到农历正月初一 农历年份减去1 - lunar_year -= (datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') - dt).total_seconds() > 0 # 当时农历新年时的日期对象 newyear_dt = datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') + # 如果还没有到农历正月初一 农历年份减去1 + lunar_year -= (newyear_dt - dt).total_seconds() > 0 # 查询日期距离当年的春节差了多久 days_passed = (dt - newyear_dt).days # 被查询日期的年份码 @@ -66,6 +67,8 @@ def from_datetime(dt): # 取得本年的月份列表 month_days = ZhDate.decode(year_code) + month = 0 + lunar_day = 0 for pos, days in enumerate(accumulate(month_days)): if days_passed + 1 <= days: month = pos + 1 @@ -94,13 +97,13 @@ def __days_passed(self): int -- 差值天数 """ month_days = ZhDate.decode(self.year_code) - #当前农历年的闰月,为0表示无润叶 - month_leap = self.year_code & 0xf + # 当前农历年的闰月,为0表示无润叶 + month_leap = self.year_code & 0xf - #当年无闰月,或者有闰月但是当前月小于闰月 + # 当年无闰月,或者有闰月但是当前月小于闰月 if (month_leap == 0) or (self.lunar_month < month_leap): days_passed_month = sum(month_days[:self.lunar_month - 1]) - #当前不是闰月,并且当前月份和闰月相同 + # 当前不是闰月,并且当前月份和闰月相同 elif (not self.leap_month) and (self.lunar_month == month_leap): days_passed_month = sum(month_days[:self.lunar_month - 1]) else: @@ -157,7 +160,11 @@ def __str__(self): Returns: str -- 标准格式农历日期字符串 """ - return "农历{}年{}{}月{}日".format(self.lunar_year,"闰" if self.leap_month else "",self.lunar_month,self.lunar_day) + return "农历{}年{}{}月{}日".format( + self.lunar_year, "闰" if self.leap_month else "", + self.lunar_month, + self.lunar_day + ) def __repr__(self): return self.__str__() @@ -194,7 +201,7 @@ def __sub__(self, another): def __tiandi(anum): tian = '甲乙丙丁戊己庚辛壬癸' di = '子丑寅卯辰巳午未申酉戌亥' - return '{}{}'.format(tian[anum % 10],di[anum % 12]) + return '{}{}'.format(tian[anum % 10], di[anum % 12]) @staticmethod def validate(year, month, day, leap): @@ -217,15 +224,15 @@ def validate(year, month, day, leap): # 有闰月标志 if leap: - if (year_code & 0xf) != month: # 年度闰月和校验闰月不一致的话,返回校验失败 + if (year_code & 0xf) != month: # 年度闰月和校验闰月不一致的话,返回校验失败 return False elif day == 30: # 如果日期是30的话,直接返回年度代码首位是否为1,即闰月是否为大月 return (year_code >> 16) == 1 - else: # 年度闰月和当前月份相同,日期不为30的情况,返回通过 + else: # 年度闰月和当前月份相同,日期不为30的情况,返回通过 return True - elif day <= 29: # 非闰月,并且日期小于等于29,返回通过 + elif day <= 29: # 非闰月,并且日期小于等于29,返回通过 return True - else: # 非闰月日期为30,返回年度代码中的月份位是否为1,即是否为大月 + else: # 非闰月日期为30,返回年度代码中的月份位是否为1,即是否为大月 return ((year_code >> (12 - month) + 4) & 1) == 1 @staticmethod diff --git a/zhdate/constants.py b/zhdate/constants.py index 4df7f92..f74c8ad 100644 --- a/zhdate/constants.py +++ b/zhdate/constants.py @@ -1,9 +1,10 @@ -''' --*- coding: utf-8 -*- +# -*- coding: utf-8 -*- + +""" File: constant.py File Created: Saturday, 21st January 2023 01:42 am Author: Eilles Wan (EillesWan@outlook.com) -''' +""" CHINESEYEARCODE = [ 19416, @@ -41,7 +42,7 @@ 最后四位:表示闰月的月份,0表示当年无闰月 前四位和最后四位应该结合使用,如果最后四位为0,则不考虑前四位 -例: +例: 1901年代码为 19168,转成二进制为 0b100101011100000, 最后四位为0,当年无闰月,月份数据为 010010101110 分别代表12月的大小情况 1903年代码为 21717,转成二进制为 0b101010011010101,最后四位为5,当年为闰五月,首四位为0,闰月为29天,月份数据为 010101001101 分别代表12月的大小情况 @@ -93,4 +94,3 @@ ''' 从1900年,至2100年每年的农历春节的公历日期 ''' - From a10154d23f5b587e7346565c641293c70ee45b0f Mon Sep 17 00:00:00 2001 From: lihuanshuai Date: Wed, 20 Sep 2023 18:27:01 +0800 Subject: [PATCH 2/6] bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e77971a..5f0bac1 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setuptools.setup( name='zhdate', - version='1.0', + version='1.1', author="PandaWithBeard", author_email="9861649@qq.com", description="A pachage to convert Chinese Lunar Calendar to datetime", From 66c87b75f295c1e484ce48651c85d2af192bbc6c Mon Sep 17 00:00:00 2001 From: lihuanshuai Date: Tue, 26 Sep 2023 11:43:54 +0800 Subject: [PATCH 3/6] update workflow --- .github/workflows/python-app.yml | 7 +++++-- zhdate/__init__.py | 1 - 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index add8987..997f97b 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -5,9 +5,12 @@ name: Python application on: push: - branches: [ master ] + branches: + - master pull_request: - branches: [ master ] + branches: + - master + - dev jobs: build: diff --git a/zhdate/__init__.py b/zhdate/__init__.py index 9c10247..822b9fd 100644 --- a/zhdate/__init__.py +++ b/zhdate/__init__.py @@ -64,7 +64,6 @@ def from_datetime(cls, dt): newyear_dt = datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') # 如果还没有到农历正月初一 农历年份减去1 lunar_year -= (newyear_dt - dt).total_seconds() > 0 - # 查询日期距离当年的春节差了多久 days_passed = (dt - newyear_dt).days # 被查询日期的年份码 From ee48c6f95b0d6059b03e66bcaff17b5c8a5caf67 Mon Sep 17 00:00:00 2001 From: lihuanshuai Date: Tue, 26 Sep 2023 11:51:14 +0800 Subject: [PATCH 4/6] fix --- setup.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 5f0bac1..ad85af3 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,15 @@ # -*- coding=utf-8 -*- -import setuptools - -''' +""" Author : PandaWithBeard Date : 2023-01-21 02:30:41 LastEditors : PandaWithBeard LastEditTime : 2023-01-21 02:30:41 FilePath : /zhdate/setup.py Description : -''' +""" + +import setuptools with open('README.md', 'r') as file: long_description = file.read() From 91f1eca27d0a78018f222bcd41c0c596dac3656c Mon Sep 17 00:00:00 2001 From: lihuanshuai Date: Wed, 27 Sep 2023 18:24:03 +0800 Subject: [PATCH 5/6] fix --- zhdate/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zhdate/__init__.py b/zhdate/__init__.py index 822b9fd..fe16686 100644 --- a/zhdate/__init__.py +++ b/zhdate/__init__.py @@ -64,6 +64,8 @@ def from_datetime(cls, dt): newyear_dt = datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') # 如果还没有到农历正月初一 农历年份减去1 lunar_year -= (newyear_dt - dt).total_seconds() > 0 + # 更正当时农历新年时的日期对象 + newyear_dt = datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') # 查询日期距离当年的春节差了多久 days_passed = (dt - newyear_dt).days # 被查询日期的年份码 From 8b153fec647afb2416304c5907ae5afa613639d3 Mon Sep 17 00:00:00 2001 From: lihuanshuai Date: Wed, 27 Sep 2023 18:32:35 +0800 Subject: [PATCH 6/6] add test --- tests/test_zhdate.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_zhdate.py b/tests/test_zhdate.py index 3092f71..3a0ad2e 100644 --- a/tests/test_zhdate.py +++ b/tests/test_zhdate.py @@ -62,6 +62,7 @@ def test_from_datetime(self): ((1903, 5, 17), datetime(1903, 6, 12)), ((1903, 5, 17, True), datetime(1903, 7, 11)), ((1900, 1, 20), datetime(1900, 2, 19)), + ((1900, 12, 30), datetime(1901, 2, 18)), ((2050, 1, 28), datetime(2050, 2, 19)), ((2050, 3, 30, True), datetime(2050, 5, 20)), ((1900, 1, 1), datetime(1900, 1, 31)),