Skip to content

Commit

Permalink
Merge pull request #125 from chinapnr/v1.1.2_develop
Browse files Browse the repository at this point in the history
V1.1.2 develop
  • Loading branch information
itaa authored Nov 8, 2018
2 parents 4cbd42c + bce3219 commit 52d13a5
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 26 deletions.
54 changes: 46 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
.. image:: https://readthedocs.org/projects/fishbase/badge/?version=latest
:target: https://fishbase.readthedocs.io/en/latest/?badge=latest



.. _header-n22:

fishbase 简介
=============
fishbase
========

fishbase 是我们自己开发和整理的一套 Python 基础函数库。 从这几年的
Python
Expand All @@ -25,9 +21,19 @@ Python
库的建设,包括完善文档和加入单元测试、示例代码、文档等。希望能够帮助到所有的
Python 爱好者和应用开发人员。

--------------
Installing
==========

Install and update using `pip`_:

.. code-block:: text
目前主要分为:
pip install -U fishbase
Modules
=======

目前主要分为以下模块:

- fish_common 基本函数包

Expand All @@ -41,4 +47,36 @@ Python 爱好者和应用开发人员。

- fish_project project 目录结构生成函数包

A Simple Example
================

.. code-block:: python
from fishbase.fish_common import GetMD5
# 获取字符换的MD5值
md5_string = GetMD5.string('hello world!')
print('md5_string : ', md5_string)
# 获取文件的MD5值
file_path = './test_conf.ini'
md5_file = GetMD5.file(file_path)
print('md5_file : ', md5_file)
# 获取hmac算法MD5值
hmac_md5_string = GetMD5.hmac_md5('hello world!', 'salt')
print('hmac_md5_string : ', hmac_md5_string)
.. code-block:: text
md5_string : fc3ff98e8c6a0d3087d515c0473f8677
md5_file : fb7528c9778b2377e30b0f7e4c26fef0
hmac_md5_string: 191f82804523bfdafe0188bbbddd6587
Links
=====

详细帮助文档:http://fishbase.readthedocs.io/

测试覆盖率:https://coveralls.io/github/chinapnr/fishbase?branch=master


.. _pip: https://pip.pypa.io/en/stable/quickstart/
8 changes: 7 additions & 1 deletion docs/change_log.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
更新记录
===========================
2018.10.27 v1.1.2
---------------------------
* `#80 <https://github.com/chinapnr/fishbase/issues/80>`_, common, add function :meth:`fish_common.find_common_between_dicts`, doc and unittest;
* `#99 <https://github.com/chinapnr/fishbase/issues/99>`_, common, add function :meth:`fish_common.GetMD5.hmac_md5`, doc and unittest;


2018.9.23 v1.1.1
---------------------------
* `#115 <https://github.com/chinapnr/fishbase/issues/115>`_, common, :meth:`fish_common.get_random_str`, optimize;
* `#115 <https://github.com/chinapnr/fishbase/issues/115>`_, common, add function :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;

Expand Down
10 changes: 8 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.1'
version = '1.1.2'
# The full version, including alpha/beta/rc tags.
release = '1.1.1'
release = '1.1.2'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -218,6 +218,12 @@

# Latex figure (float) alignment
#'figure_align': 'htbp',
'preamble': '''
\\hypersetup{unicode=true}
\\usepackage{CJKutf8}
\\AtBeginDocument{\\begin{CJK}{UTF8}{gbsn}}
\\AtEndDocument{\\end{CJK}}
'''
}

# Grouping the document tree into LaTeX files. List of tuples
Expand Down
2 changes: 2 additions & 0 deletions docs/fish_common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
fish_common.GetMD5.string
fish_common.GetMD5.file
fish_common.GetMD5.big_file
fish_common.GetMD5.hmac_md5
fish_common.if_json_contain
fish_common.SingleTon
fish_common.sorted_list_from_dict
Expand All @@ -25,6 +26,7 @@
fish_common.get_sub_dict
fish_common.get_group_list_data
fish_common.transform_hump_to_underline
fish_common.find_duplicated_between_dicts

.. automodule:: fish_common
:members:
2 changes: 1 addition & 1 deletion docs/fish_project.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
``fish_project`` project 函数包
===========================
===============================

.. automodule:: fish_project
: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.1' # type: str
__version__ = '1.1.2' # type: str

57 changes: 56 additions & 1 deletion fishbase/fish_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import base64
import string
import random
from collections import OrderedDict
from collections import OrderedDict, namedtuple
from operator import attrgetter
import functools

Expand Down Expand Up @@ -359,6 +359,7 @@ def get_cf_cache(self, cf, section, key):

# 2018.5.8 edit by David Yi, edit from Jia Chunying,#19026
# 2018.6.12 edit by Hu Jun, edit from Jia Chunying,#37
# 2018.10.28 edit by Hu Jun #99
class GetMD5(object):
"""
计算普通字符串和一般的文件,对于大文件采取逐步读入的方式,也可以快速计算;基于 Python 的 hashlib.md5() 进行封装和扩展;
Expand All @@ -369,6 +370,7 @@ class GetMD5(object):
print('string md5:', GetMD5.string('hello world!'))
print('file md5:', GetMD5.file(get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1]))
print('big file md5:', GetMD5.big_file(get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1]))
print('string hmac_md5:', GetMD5.hmac_md5('hello world!', 'salt'))
print('---')
执行结果::
Expand All @@ -377,6 +379,7 @@ class GetMD5(object):
string md5: fc3ff98e8c6a0d3087d515c0473f8677
file md5: fb7528c9778b2377e30b0f7e4c26fef0
big file md5: fb7528c9778b2377e30b0f7e4c26fef0
string hmac_md5: 191f82804523bfdafe0188bbbddd6587
---
"""
Expand Down Expand Up @@ -433,6 +436,21 @@ def big_file(filename):
result = md5.hexdigest()
return result

@staticmethod
def hmac_md5(s, salt):
"""
获取一个字符串的 使用salt加密的 hmac MD5值
:param:
* (string) s 需要进行 hash 的字符串
* (string) salt 随机字符串
:return:
* (string) result 32位小写 MD5 值
"""
hmac_md5 = hmac.new(salt.encode('utf-8'), s.encode('utf-8'),
digestmod=hashlib.md5).hexdigest()
return hmac_md5


# 2018.5.15 v1.0.11 original by Lu Jie, edit by David Yi, #19029
def if_json_contain(left_json, right_json, op='strict'):
Expand Down Expand Up @@ -1045,3 +1063,40 @@ def transform_hump_to_underline(param_dict):
underline_sub = re.sub(hump_to_underline, r'\1_\2', key).lower()
temp_dict[underline_sub] = temp_dict.pop(key)
return temp_dict


# v1.1.2 edit by Hu Jun, #80
def find_duplicated_between_dicts(dict1, dict2):
"""
查找两个字典中的相同点,包括键、值、项,仅支持hashable对象
:param:
* dict1(dict): 比较的字典1
* dict2(dict): 比较的字典2
:return:
* dup_info(namedtuple): 返回两个字典中相同的信息组成的具名元组
举例如下::
print('--- find_duplicated_between_dicts demo---')
dict1 = {'x':1, 'y':2, 'z':3}
dict2 = {'w':10, 'x':1, 'y':2}
res = find_duplicated_between_dicts(dict1, dict2)
print(res.item)
print(res.key)
print(res.value)
print('---')
执行结果::
--- find_duplicated_between_dicts demo---
set([('x', 1)])
{'x', 'y'}
{1}
---
"""
Duplicated_info = namedtuple('Duplicated_info', ['item', 'key', 'value'])
duplicated_info = Duplicated_info(set(dict1.items()) & set(dict2.items()),
set(dict1.keys()) & set(dict2.keys()),
set(dict1.values()) & set(dict2.values()))
return duplicated_info
17 changes: 16 additions & 1 deletion test/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ def test_md5_02(self):
def test_md5_03(self):
salt = 'm4xV2yGFSn'
assert GetMD5.string('hello world!', salt) == '984d47991401fad7d920a30f715cfd22'


# 测试 GetMD5() tc
def test_md5_04(self):
salt = 'salt'
assert GetMD5.hmac_md5('hello world!', salt) == '191f82804523bfdafe0188bbbddd6587'

# 测试 if_json_contain() tc
def test_json_contain_01(self):

Expand Down Expand Up @@ -374,3 +379,13 @@ def test_transform_hump_to_underline(self):

assert 'firstName' not in underline_param_dict
assert 'first_name' in underline_param_dict

# test find_duplicated_between_dicts() tc
def test_find_duplicated_between_dicts_01(self):
dict1 = {'x': 1, 'y': 2, 'z': 3}
dict2 = {'w': 10, 'x': 1, 'y': 4}

info = find_duplicated_between_dicts(dict1, dict2)
assert dict(info.item) == {'x': 1}
assert info.key == {'x', 'y'}
assert info.value == {1}
17 changes: 6 additions & 11 deletions test/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,16 @@ class TestFishDate(object):
# test get_years() tc
def test_get_years_01(self):
this_month = datetime.date(day=1, month=6, year=2018)

assert get_years(7, this_month) == '201901'
assert get_years(-5, this_month) == '201801'
assert get_years(-6, this_month) == '201712'

this_month1 = datetime.datetime.now()

y = this_month1.year
m = this_month1.month + 1
if m == 12:
y += 1
m = 1

assert get_years(1) == ''.join(['%04d' % y, '%02d' % m])

with pytest.raises(TypeError):
get_years(-5, 8)

assert int(get_years(1)) >= 201811

def test_get_date_range_01(self):
this_month = datetime.date(day=1, month=2, year=2018)

Expand Down Expand Up @@ -104,6 +96,9 @@ def test_transform_datetime_to_unix_01(self):
assert isinstance(ans_time, float)
assert len(str(ans_time)) == 12

ans_time_1 = transform_datetime_to_unix()
assert ans_time_1 > 1541594338

# 测试 transform_datetime_to_unix() tc
def test_transform_datetime_to_unix_02(self):
dtime = '1534938627'
Expand Down

0 comments on commit 52d13a5

Please sign in to comment.