diff --git a/.gitignore b/.gitignore index a16f3cb..b500b47 100644 --- a/.gitignore +++ b/.gitignore @@ -108,4 +108,6 @@ demo/log # test cache data test/.test_cache_data/* -test/.test_cache_data \ No newline at end of file +test/.test_cache_data + +.idea \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index d8aecfe..0000000 --- a/README.md +++ /dev/null @@ -1,24 +0,0 @@ -![build](https://api.travis-ci.org/chinapnr/fishbase.svg?branch=master) -![doc](https://readthedocs.org/projects/fishbase/badge/?version=latest) -[![Coverage Status](https://coveralls.io/repos/github/chinapnr/fishbase/badge.svg?branch=master)](https://coveralls.io/github/chinapnr/fishbase?branch=master) - -## fishbase 简介 - -fishbase 是我们自己开发和整理的一套 Python 基础函数库。 从这几年的 Python 开发中抽象了很多常见的通用的业务逻辑,以希望减少日常开发中的一些重复的工作量。 - -我们从2016年左右逐渐开始用 Python 开发一些项目,还不能算很有经验,但是也经常碰到一些问题, fishbase 库并不是用来解决很复杂的问题,并且有些是对系统函数的进一步封装,以简化应用程序开发中的工作量和引用的复杂度。 - -目前,我们正在加快 fishbase 库的建设,包括完善文档和加入单元测试、示例代码、文档等。希望能够帮助到所有的 Python 爱好者和应用开发人员。 - ---- - -目前主要分为: - -* fish_common 基本函数包 -* fish_system 系统增强函数包 -* fish_file 文件处理增强函数包 -* fish_csv csv 处理增强函数包 -* fish_logger 日志记录增强函数包 -* fish_project project 目录结构生成函数包 - -详细帮助文档:http://fishbase.readthedocs.io/ \ No newline at end of file diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..f0fc77d --- /dev/null +++ b/README.rst @@ -0,0 +1,44 @@ + +.. image:: https://travis-ci.org/chinapnr/fishbase.svg?branch=master + :target: https://travis-ci.org/chinapnr/fishbase +.. image:: https://coveralls.io/repos/github/chinapnr/fishbase/badge.svg?branch=master + :target: https://coveralls.io/github/chinapnr/fishbase?branch=master +.. image:: https://readthedocs.org/projects/fishbase/badge/?version=latest + :target: https://fishbase.readthedocs.io/en/latest/?badge=latest + + + +.. _header-n22: + +fishbase 简介 +============= + +fishbase 是我们自己开发和整理的一套 Python 基础函数库。 从这几年的 +Python +开发中抽象了很多常见的通用的业务逻辑,以希望减少日常开发中的一些重复的工作量。 + +我们从2016年左右逐渐开始用 Python +开发一些项目,还不能算很有经验,但是也经常碰到一些问题, fishbase +库并不是用来解决很复杂的问题,并且有些是对系统函数的进一步封装,以简化应用程序开发中的工作量和引用的复杂度。 + +目前,我们正在加快 fishbase +库的建设,包括完善文档和加入单元测试、示例代码、文档等。希望能够帮助到所有的 +Python 爱好者和应用开发人员。 + +-------------- + +目前主要分为: + +- fish_common 基本函数包 + +- fish_system 系统增强函数包 + +- fish_file 文件处理增强函数包 + +- fish_csv csv 处理增强函数包 + +- fish_logger 日志记录增强函数包 + +- fish_project project 目录结构生成函数包 + +详细帮助文档:http://fishbase.readthedocs.io/ diff --git a/docs/change_log.rst b/docs/change_log.rst index 7afe7ee..dda6eea 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -1,5 +1,12 @@ 更新记录 =========================== +2018.9.23 v1.1.1 +--------------------------- +* `#115 `_, common, :meth:`fish_common.get_random_str`, optimize; +* `#114 `_, common, add function :meth:`fish_common.transform_hump_to_underline`, doc and unittest; +* `#101 `_, date, add function :meth:`fish_date.transform_datetime_to_unix`, doc and unittest; + + 2018.9.3 v1.1.0 --------------------------- * `#74 `_, common, add function :meth:`fish_common.get_group_list_data`, doc and unittest; @@ -9,11 +16,10 @@ * `#82 `_, project, add function :meth:`fish_project.init_project_by_yml`, doc and unittest; - 2018.8.2 v1.0.16 --------------------------- * `#87 `_, date, add function :meth:`fish_date.GetRandomTime`, doc and unittest; -* `#94 `_, date, edit function :meth:`fish_csv.csv_file_to_list`, doc and unittest; +* `#94 `_, csv, edit function :meth:`fish_csv.csv_file_to_list`, doc and unittest; * `#94 `_, common, edit function :meth:`fish_common.conf_as_dict`, doc and unittest; diff --git a/docs/conf.py b/docs/conf.py index 0e44624..83d544d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -57,9 +57,9 @@ # built documents. # # The short X.Y version. -version = '1.1.0' +version = '1.1.1' # The full version, including alpha/beta/rc tags. -release = '1.1.0' +release = '1.1.1' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/fish_common.rst b/docs/fish_common.rst index 0f0cde2..fc5606e 100644 --- a/docs/fish_common.rst +++ b/docs/fish_common.rst @@ -24,6 +24,7 @@ fish_common.get_query_param_from_url fish_common.get_sub_dict fish_common.get_group_list_data + fish_common.transform_hump_to_underline .. automodule:: fish_common :members: diff --git a/docs/fish_date.rst b/docs/fish_date.rst index 9a37861..8286608 100644 --- a/docs/fish_date.rst +++ b/docs/fish_date.rst @@ -7,6 +7,7 @@ fish_date.GetRandomTime fish_date.get_time_interval fish_date.transform_unix_to_datetime + fish_date.transform_datetime_to_unix .. automodule:: fish_date :members: diff --git a/fishbase/__init__.py b/fishbase/__init__.py index b79673b..e203445 100644 --- a/fishbase/__init__.py +++ b/fishbase/__init__.py @@ -24,5 +24,5 @@ from .fish_system import * from .fish_project import * -__version__ = '1.1.0' # type: str +__version__ = '1.1.1' # type: str diff --git a/fishbase/fish_common.py b/fishbase/fish_common.py index cb7787d..500fc38 100644 --- a/fishbase/fish_common.py +++ b/fishbase/fish_common.py @@ -12,6 +12,7 @@ # 2017.1.8 v1.0.9 #19003, remove file related functions to fish_file.py import sys import uuid +import copy import re import hashlib import hmac @@ -748,6 +749,7 @@ def decode(s): # v1.0.14 edit by Hu Jun, #51 +# v1.1.1 edit by Hu Jun, #115 def get_random_str(length, letters=True, digits=False, punctuation=False): """ 获得指定长度,不同规则的随机字符串,可以包含数字,字母和标点符号 @@ -787,6 +789,10 @@ def get_random_str(length, letters=True, digits=False, punctuation=False): random_source += string.digits if digits else '' random_source += string.punctuation if punctuation else '' + # 避免出现 ValueError: Sample larger than population or is negative + if length > len(random_source): + random_source *= (length//len(random_source) + 1) + random_str = ''.join(random.sample(random_source, length)) return random_str @@ -1002,3 +1008,40 @@ def get_sub_dict(data_dict, key_list, default_value='default_value'): for item in key_list: sub_dict.update({item: data_dict.get(item, default_value)}) return sub_dict + + +# v1.1.1 edit by Hu Jun, #114 +def transform_hump_to_underline(param_dict): + """ + 将驼峰命名的参数字典键转换为下划线参数 + + :param: + * param_dict(dict): 请求参数字典 + + :return: + * temp_dict(dict): 转换后的参数字典 + + 举例如下:: + + print('--- transform_hump_to_underline demo---') + hump_param_dict = {'firstName': 'Python', 'Second_Name': 'san', 'right_name': 'name'} + underline_param_dict = transform_hump_to_underline(hump_param_dict ) + print(underline_param_dict ) + print('---') + + 执行结果:: + + --- transform_hump_to_underline demo--- + {'first_name': 'Python', 'second_name': 'san', 'right_name': 'name'} + --- + + """ + temp_dict = copy.deepcopy(param_dict) + + # 正则 + hump_to_underline = re.compile(r'([a-z]|\d)([A-Z])') + for key in list(param_dict.keys()): + # 将驼峰值替换为下划线 + underline_sub = re.sub(hump_to_underline, r'\1_\2', key).lower() + temp_dict[underline_sub] = temp_dict.pop(key) + return temp_dict diff --git a/fishbase/fish_date.py b/fishbase/fish_date.py index d83a618..fa798af 100644 --- a/fishbase/fish_date.py +++ b/fishbase/fish_date.py @@ -1,5 +1,6 @@ # coding=utf-8 +import time from datetime import datetime, timedelta import calendar import random @@ -8,7 +9,7 @@ # 2016.4.26 # 输入: date_kind, eg 'last month', 'this month' # 输出: tuple, type datetime.date eg '2016-03-01' '2016-03-31' -# v1.0.14 #61, edit by Hu Jun +# v1.0.14 edit by Hu Jun #61 def get_date_range(dates, separator='-'): """ 获取某个月的日期范围,返回该月第一天和最后一天的字符串表示 @@ -59,7 +60,7 @@ def get_date_range(dates, separator='-'): return first_day, last_day -# v1.0.14 #37, edit by Hu Jun +# v1.0.14 edit by Hu Jun #37 def get_years(months=0, refer=None): """ 获取基准时月份增量的年月 @@ -106,7 +107,7 @@ def get_years(months=0, refer=None): return ''.join(['%04d' % y, '%02d' % m]) -# v1.0.16 #87, edit by Hu Jun +# v1.0.16 edit by Hu Jun #87 class GetRandomTime(object): """ 获取随机时间 @@ -186,7 +187,7 @@ def date_time_this_year(): return this_year_start + timedelta(seconds=random_seconds) -# v1.1.0 #90, edit by Hu Jun +# v1.1.0 edit by Hu Jun #90 def get_time_interval(start_time, end_time): """ 获取两个unix时间戳之间的时间间隔 @@ -236,7 +237,7 @@ def get_time_interval(start_time, end_time): return interval_dict -# v1.1.0 #93, edit by Hu Jun +# v1.1.0 edit by Hu Jun #93 def transform_unix_to_datetime(timestamp): """ 将unix时间戳转换成datetime类型 @@ -270,3 +271,37 @@ def transform_unix_to_datetime(timestamp): date_type = datetime.fromtimestamp(timestamp) return date_type + + +# v1.1.1 edit by Hu Jun #101 +def transform_datetime_to_unix(dtime=None): + """ + 将datetime类型转换成unix时间戳 + + :param: + * dtime: (datetime) datetime类型实例,默认为当前时间 + :return: + * data_type: (datetime) datetime类型实例 + + 举例如下:: + + print('--- transform_datetime_to_unix demo ---') + dtime = datetime.datetime.now() + ans_time = transform_datetime_to_unix(dtime) + print(ans_time) + print('---') + + 执行结果:: + + --- transform_datetime_to_unix demo --- + 1535108620.0 + --- + + """ + if not dtime: + dtime = datetime.now() + + if not isinstance(dtime, datetime): + raise TypeError('dtime should be datetime, but we got {}'.format(type(dtime))) + + return time.mktime(dtime.timetuple()) diff --git a/setup.py b/setup.py index d43aedf..8ecec69 100644 --- a/setup.py +++ b/setup.py @@ -2,20 +2,25 @@ from setuptools import setup import io import re + +with io.open('README.rst', 'rt', encoding='utf8') as f: + readme = f.read() + with io.open('fishbase/__init__.py', 'rt', encoding='utf8') as f: version = re.search(r'__version__ = \'(.*?)\'', f.read()).group(1) setup( name='fishbase', version=version, - install_requires=['python-dateutil'], + install_requires=['python-dateutil', + 'pyyaml'], url='https://github.com/chinapnr/fishbase', license='MIT', author='David Yi', author_email='wingfish@gmail.com', description='some useful functions for python', - + long_description=readme, packages=['fishbase'], # packages=['fishbase', 'fishbase.naive_bayes'], diff --git a/test/test_common.py b/test/test_common.py index a0da6b3..335b655 100644 --- a/test/test_common.py +++ b/test/test_common.py @@ -365,4 +365,12 @@ def test_get_sub_dict_02(self): get_sub_dict(dict1, 'test_list') with pytest.raises(TypeError): - get_sub_dict('test_dict', list1) \ No newline at end of file + get_sub_dict('test_dict', list1) + + # test transform_hump_to_underline() tc + def test_transform_hump_to_underline(self): + hump_param_dict = {'firstName': 'Python', 'Second_Name': 'zhangshan', 'right_name': 'name'} + underline_param_dict = transform_hump_to_underline(hump_param_dict) + + assert 'firstName' not in underline_param_dict + assert 'first_name' in underline_param_dict diff --git a/test/test_date.py b/test/test_date.py index 15d174e..04bf7e7 100644 --- a/test/test_date.py +++ b/test/test_date.py @@ -92,6 +92,21 @@ def test_transform_unix_to_datetime_01(self): # 测试 transform_unix_to_datetime() tc def test_transform_unix_to_datetime_02(self): timestamp = '1534938627' - + + with pytest.raises(TypeError): + transform_unix_to_datetime(timestamp) + + # 测试 transform_datetime_to_unix() tc + def test_transform_datetime_to_unix_01(self): + dtime = datetime.datetime.now() + ans_time = transform_datetime_to_unix(dtime=dtime) + + assert isinstance(ans_time, float) + assert len(str(ans_time)) == 12 + + # 测试 transform_datetime_to_unix() tc + def test_transform_datetime_to_unix_02(self): + dtime = '1534938627' + with pytest.raises(TypeError): - transform_unix_to_datetime(timestamp) \ No newline at end of file + transform_datetime_to_unix(dtime=dtime)