From b210c9077abe6e50191bcc16ce58f744b3791c93 Mon Sep 17 00:00:00 2001 From: Jia Chunying Date: Tue, 25 Dec 2018 19:53:22 +0800 Subject: [PATCH 1/4] 2018.12.25 v1.1.5, modify version and method name random_date_str -> gen_random_date #142 --- fishbase/__init__.py | 2 +- fishbase/fish_date.py | 9 +++++---- test/test_date.py | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/fishbase/__init__.py b/fishbase/__init__.py index f82a38a..c1e441f 100644 --- a/fishbase/__init__.py +++ b/fishbase/__init__.py @@ -25,5 +25,5 @@ from .fish_project import * from .fish_data import * -__version__ = '1.1.4' # type: str +__version__ = '1.1.5' # type: str diff --git a/fishbase/fish_date.py b/fishbase/fish_date.py index f17806c..1038654 100644 --- a/fishbase/fish_date.py +++ b/fishbase/fish_date.py @@ -109,6 +109,7 @@ def get_years(months=0, refer=None): return ''.join(['%04d' % y, '%02d' % m]) +# v1.1.15 edit by Jia Chunying #142 # v1.1.14 edit by Hu Jun #142 # v1.0.16 edit by Hu Jun #87 class GetRandomTime(object): @@ -188,7 +189,7 @@ def date_time_this_year(): return this_year_start + timedelta(seconds=random_seconds) @staticmethod - def random_date_str(year): + def gen_random_date(year): """ 获取当前年的随机时间字符串 @@ -200,13 +201,13 @@ def random_date_str(year): 举例如下:: - print('--- GetRandomTime.random_date_str demo ---') - print(GetRandomTime.random_date_str(2010)) + print('--- GetRandomTime.gen_random_date demo ---') + print(GetRandomTime.gen_random_date(2010)) print('---') 执行结果:: - --- GetRandomTime.date_time_this_year demo demo --- + --- GetRandomTime.gen_random_date demo --- 20101008 --- """ diff --git a/test/test_date.py b/test/test_date.py index 09585c6..3bf5f94 100644 --- a/test/test_date.py +++ b/test/test_date.py @@ -54,17 +54,17 @@ def test_date_time_this_year_01(self): # 测试 GetRandomTime() tc def test_random_date_str_01(self): - date_str = GetRandomTime.random_date_str(2018) + date_str = GetRandomTime.gen_random_date(2018) assert date_str[:4] == '2018' # 测试 GetRandomTime() tc def test_random_date_str_02(self): with pytest.raises(ValueError): - GetRandomTime.random_date_str('201812') + GetRandomTime.gen_random_date('201812') with pytest.raises(ValueError): - GetRandomTime.random_date_str(18) + GetRandomTime.gen_random_date(18) # 测试 get_time_interval() tc def test_get_time_interval_01(self): From 328ea2b3a4cfd6e55a9f94dd7d886d69fb3e0ab1 Mon Sep 17 00:00:00 2001 From: Jia Chunying Date: Tue, 25 Dec 2018 20:36:32 +0800 Subject: [PATCH 2/4] 2018.12.25 v1.1.5, #142 modify method name gen_random_date -> gen_date_by_year and add gen_date_by_range #164 --- docs/change_log.rst | 5 +++ fishbase/fish_date.py | 73 ++++++++++++++++++++++++++++++++++--------- test/test_date.py | 6 ++-- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/docs/change_log.rst b/docs/change_log.rst index 26becc4..5c023c9 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -1,5 +1,10 @@ 更新记录 =========================== +2018.12.25 v1.1.5 +--------------------------- +* `#146 `_, common, add function :meth:`fish_date.GetRandomTime.gen_date_by_range`, doc and unittest; +* `#142 `_, common, edit function :meth:`fish_date.GetRandomTime.gen_date_by_year`, doc and unittest; + 2018.12.14 v1.1.4 --------------------------- * `#142 `_, common, add function :meth:`fish_date.GetRandomTime.random_date_str`, doc and unittest; diff --git a/fishbase/fish_date.py b/fishbase/fish_date.py index 1038654..791a653 100644 --- a/fishbase/fish_date.py +++ b/fishbase/fish_date.py @@ -109,7 +109,7 @@ def get_years(months=0, refer=None): return ''.join(['%04d' % y, '%02d' % m]) -# v1.1.15 edit by Jia Chunying #142 +# v1.1.15 edit by Jia Chunying #142 #164 # v1.1.14 edit by Hu Jun #142 # v1.0.16 edit by Hu Jun #87 class GetRandomTime(object): @@ -189,7 +189,7 @@ def date_time_this_year(): return this_year_start + timedelta(seconds=random_seconds) @staticmethod - def gen_random_date(year): + def gen_date_by_year(year): """ 获取当前年的随机时间字符串 @@ -201,14 +201,14 @@ def gen_random_date(year): 举例如下:: - print('--- GetRandomTime.gen_random_date demo ---') - print(GetRandomTime.gen_random_date(2010)) + print('--- GetRandomTime.gen_date_by_year demo ---') + print(GetRandomTime.gen_date_by_year("2010")) print('---') 执行结果:: - --- GetRandomTime.gen_random_date demo --- - 20101008 + --- GetRandomTime.gen_date_by_year demo --- + 20100505 --- """ if isinstance(year, int) and len(str(year)) != 4: @@ -217,23 +217,66 @@ def gen_random_date(year): if isinstance(year, str) and len(year) != 4: raise ValueError("year should be string year like '2018', but we got {}, {}". format(year, type(year))) + if isinstance(year, int): + year = str(year) - # 设置开始日期时间元组(1980-01-01 00:00:00) - start_time_tuple = (year, 1, 1, 0, 0, 0, 0, 0, 0) - # 设置结束日期时间元组(1980-12-31 23:59:59) - end_time_tuple = (year, 12, 31, 23, 59, 59, 0, 0, 0) + date_str = GetRandomTime.gen_date_by_range(year + "-01-01", year + "-12-31", "%Y%m%d") - # 生成开始时间戳 - start_timestamp = time.mktime(start_time_tuple) - # 生成结束时间戳 - end_timestamp = time.mktime(end_time_tuple) + return date_str + + @staticmethod + def gen_date_by_range(begin_date, end_date, date_format="%Y-%m-%d"): + """ + + 指定一个日期范围,随机生成区间内的某一个日期,该区间为闭区间 + + :param: + * begin_date: (string) 范围的起始日期,字符串 yyyy-MM-dd eg. 2018-01-01 + * end_date: (string) 范围的结束日期,字符串 yyyy-MM-dd eg. 2018-12-31 + * date_format: 返回的日期格式,字符串:默认格式yyyyMMdd default: "%Y%m%d" + + :return: + * date_str + 日期区间内的一个指定格式的合法的随机日期 + + 举例如下:: + + print('--- GetRandomTime.gen_date_by_range demo ---') + print(GetRandomTime.gen_date_by_range("2010-01-01","2010-12-31")) + print('---') + + 执行结果:: + + --- GetRandomTime.gen_date_by_range demo --- + 20100124 + --- + """ + # 设置开始日期 + begin_date_info = begin_date.split("-") + begin_date_info = [int(x) for x in begin_date_info] + begin_date_info.extend([0, 0, 0, 0, 0, 0]) + begin_date_tuple = tuple(begin_date_info) + # 设置结束日期 + end_date_info = end_date.split("-") + end_date_info = [int(x) for x in end_date_info] + end_date_info.extend([23, 59, 59, 59, 0, 0]) + end_date_tuple = tuple(end_date_info) + + try: + # 生成开始时间戳 + start_timestamp = time.mktime(begin_date_tuple) + # 生成结束时间戳 + end_timestamp = time.mktime(end_date_tuple) + except TypeError as e: + raise TypeError(e, "begin_date/end_date format error") # 在开始和结束时间戳中随机取出一个 rand_timedelta = random.randint(start_timestamp, end_timestamp) # 将时间戳生成时间元组 date_tuple = time.localtime(rand_timedelta) + # 将时间元组转成格式化字符串 - date_str = time.strftime("%Y%m%d", date_tuple) + date_str = time.strftime(date_format, date_tuple) return date_str diff --git a/test/test_date.py b/test/test_date.py index 3bf5f94..7b459ee 100644 --- a/test/test_date.py +++ b/test/test_date.py @@ -54,17 +54,17 @@ def test_date_time_this_year_01(self): # 测试 GetRandomTime() tc def test_random_date_str_01(self): - date_str = GetRandomTime.gen_random_date(2018) + date_str = GetRandomTime.gen_date_by_year(2018) assert date_str[:4] == '2018' # 测试 GetRandomTime() tc def test_random_date_str_02(self): with pytest.raises(ValueError): - GetRandomTime.gen_random_date('201812') + GetRandomTime.gen_date_by_year('201812') with pytest.raises(ValueError): - GetRandomTime.gen_random_date(18) + GetRandomTime.gen_date_by_year(18) # 测试 get_time_interval() tc def test_get_time_interval_01(self): From 39a727bab31eccf466d3f7e4c0d64f584d7b280a Mon Sep 17 00:00:00 2001 From: Jia Chunying Date: Tue, 25 Dec 2018 20:39:37 +0800 Subject: [PATCH 3/4] 2018.12.25 v1.1.5, #164 modify change_log.rst --- docs/change_log.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/change_log.rst b/docs/change_log.rst index 5c023c9..d23eef6 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -2,7 +2,7 @@ =========================== 2018.12.25 v1.1.5 --------------------------- -* `#146 `_, common, add function :meth:`fish_date.GetRandomTime.gen_date_by_range`, doc and unittest; +* `#164 `_, common, add function :meth:`fish_date.GetRandomTime.gen_date_by_range`, doc and unittest; * `#142 `_, common, edit function :meth:`fish_date.GetRandomTime.gen_date_by_year`, doc and unittest; 2018.12.14 v1.1.4 From 8141b1a9366db4364b195b4051e19551e852ef52 Mon Sep 17 00:00:00 2001 From: Jia Chunying Date: Wed, 26 Dec 2018 09:32:25 +0800 Subject: [PATCH 4/4] 2018.12.26 v1.1.5, add AppVeyor CI support --- appveyor.yml | 22 ++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 23 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..6095e9f --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,22 @@ +build: false + +environment: + matrix: + - PYTHON: "C:/Python34" + - PYTHON: "C:/Python35" + - PYTHON: "C:/Python36" + - PYTHON: "C:/Python37" + +init: + - "ECHO %PYTHON%" + - ps: "ls C:/Python*" + +install: + - "curl -fsS -o C:/get-pip.py https://bootstrap.pypa.io/get-pip.py" + - "%PYTHON%/python.exe C:/get-pip.py" + - "%PYTHON%/Scripts/pip.exe install -U setuptools" + - "%PYTHON%/python.exe setup.py develop" + - "%PYTHON%/Scripts/pip.exe install -U -r requirements.txt" + +test_script: + - "%PYTHON%/Scripts/py.test.exe -sv test/" diff --git a/requirements.txt b/requirements.txt index d7d316c..08c7fd8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ python-dateutil coverage +pytest pytest-cov coveralls pyyaml