Skip to content

Commit

Permalink
Merge pull request #118 from chinapnr/v1.1.1_develop
Browse files Browse the repository at this point in the history
V1.1.1 develop
  • Loading branch information
itaa committed Oct 12, 2018
2 parents 86d5e2f + cd50435 commit 4cbd42c
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,6 @@ demo/log

# test cache data
test/.test_cache_data/*
test/.test_cache_data
test/.test_cache_data

.idea
24 changes: 0 additions & 24 deletions README.md

This file was deleted.

44 changes: 44 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -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/
10 changes: 8 additions & 2 deletions docs/change_log.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
更新记录
===========================
2018.9.23 v1.1.1
---------------------------
* `#115 <https://github.com/chinapnr/fishbase/issues/115>`_, common, :meth:`fish_common.get_random_str`, optimize;
* `#114 <https://github.com/chinapnr/fishbase/issues/114>`_, common, add function :meth:`fish_common.transform_hump_to_underline`, doc and unittest;
* `#101 <https://github.com/chinapnr/fishbase/issues/101>`_, date, add function :meth:`fish_date.transform_datetime_to_unix`, doc and unittest;


2018.9.3 v1.1.0
---------------------------
* `#74 <https://github.com/chinapnr/fishbase/issues/74>`_, common, add function :meth:`fish_common.get_group_list_data`, doc and unittest;
Expand All @@ -9,11 +16,10 @@
* `#82 <https://github.com/chinapnr/fishbase/issues/82>`_, project, add function :meth:`fish_project.init_project_by_yml`, doc and unittest;



2018.8.2 v1.0.16
---------------------------
* `#87 <https://github.com/chinapnr/fishbase/issues/87>`_, date, add function :meth:`fish_date.GetRandomTime`, doc and unittest;
* `#94 <https://github.com/chinapnr/fishbase/issues/94>`_, date, edit function :meth:`fish_csv.csv_file_to_list`, doc and unittest;
* `#94 <https://github.com/chinapnr/fishbase/issues/94>`_, csv, edit function :meth:`fish_csv.csv_file_to_list`, doc and unittest;
* `#94 <https://github.com/chinapnr/fishbase/issues/94>`_, common, edit function :meth:`fish_common.conf_as_dict`, doc and unittest;


Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions docs/fish_common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
1 change: 1 addition & 0 deletions docs/fish_date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
2 changes: 1 addition & 1 deletion fishbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
from .fish_system import *
from .fish_project import *

__version__ = '1.1.0' # type: str
__version__ = '1.1.1' # type: str

43 changes: 43 additions & 0 deletions fishbase/fish_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
获得指定长度,不同规则的随机字符串,可以包含数字,字母和标点符号
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
45 changes: 40 additions & 5 deletions fishbase/fish_date.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=utf-8

import time
from datetime import datetime, timedelta
import calendar
import random
Expand All @@ -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='-'):
"""
获取某个月的日期范围,返回该月第一天和最后一天的字符串表示
Expand Down Expand Up @@ -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):
"""
获取基准时月份增量的年月
Expand Down Expand Up @@ -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):
"""
获取随机时间
Expand Down Expand Up @@ -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时间戳之间的时间间隔
Expand Down Expand Up @@ -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类型
Expand Down Expand Up @@ -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())
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
10 changes: 9 additions & 1 deletion test/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
19 changes: 17 additions & 2 deletions test/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
transform_datetime_to_unix(dtime=dtime)

0 comments on commit 4cbd42c

Please sign in to comment.