Skip to content

Commit

Permalink
Merge pull request #68 from chinapnr/hujun_20180620
Browse files Browse the repository at this point in the history
Hujun 20180620
  • Loading branch information
itaa committed Jun 27, 2018
2 parents 18c6aed + 2667f2a commit 08d5629
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/change_log.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
更新记录
===========================

2018.6.6 v1.0.14
2018.6.25 v1.0.14
---------------------------

* 19046, setup, edit setup.py to add long description etc., the package detail;
Expand All @@ -11,6 +11,9 @@
* 37, date, edit function :meth:`fish_date.get_years()`, optimize, doc and unittest;
* 27, common, edit function :meth:`fish_common.hmac_sha256()`, optimize, doc and unittest;
* 61, date, edit function :meth:`fish_date.get_date_range()`, optimize, doc and unittest;
* 57, common, update function :meth:`fish_common.GetMD5.string()`, optimize;
* 59, common, edit function :meth:`fish_common.Base64`, optimize, doc and unittest;
* 51, common, edit function :meth:`fish_common.get_random_str`, optimize, doc and unittest;

2018.6.6 v1.0.13
---------------------------
Expand Down
4 changes: 4 additions & 0 deletions docs/fish_common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
fish_common.check_str
fish_common.find_files
fish_common.hmac_sha256
fish_common.Base64.string
fish_common.Base64.file
fish_common.Base64.decode
fish_common.get_random_str

.. automodule:: fish_common
:members:
108 changes: 108 additions & 0 deletions fishbase/fish_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import hashlib
import hmac
import os
import base64
import string
import random
from collections import OrderedDict
import functools

Expand Down Expand Up @@ -630,3 +633,108 @@ def hmac_sha256(secret, message):
message.encode('utf-8'),
digestmod=hashlib.sha256).hexdigest()
return hashed_str


# v1.0.14 edit by Hu Jun, #59
class Base64:
"""
计算返回文件和字符串的base64编码字符串
举例如下::
print('--- Base64 demo ---')
print('string base64:', Base64.string('hello world!'))
print('file base64:', Base64.file(get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1]))
print('decode base64:', Base64.decode(b'aGVsbG8gd29ybGQ='))
print('---')
执行结果::
--- Base64 demo ---
string base64: b'aGVsbG8gd29ybGQ='
file base64: b'IyEvYmluL2Jhc2gKCmNkIC9yb290L3d3dy9zaW5nbGVfcWEKCm5vaHVwIC9yb290L2FwcC9weXRob24zNjIvYmluL2d1bmljb3JuIC1jIGd1bmljb3JuLmNvbmYgc2luZ2xlX3NlcnZlcjphcHAK'
decode base64: b'hello world'
---
"""

@staticmethod
def string(s):
"""
获取一个字符串的base64值
:param:
* (string) s 需要进行 base64编码 的字符串
:return:
* (bytes) base64 编码结果
"""
return base64.b64encode(s.encode('utf-8'))

@staticmethod
def file(filename):
"""
获取一个文件的base64值
:param:
* (string) filename 需要进行 base64编码 文件路径
:return:
* (bytes) base64 编码结果
"""
with open(filename, 'rb') as f:
return base64.b64encode(f.read())

@staticmethod
def decode(s):
"""
获取base64 解码结果
:param:
* (string) filename 需要进行 base64编码 文件路径
:return:
* (bytes) base64 编码结果
"""
return base64.b64decode(s)


# v1.0.14 edit by Hu Jun, #51
def get_random_str(length, letters=True, digits=False, punctuation=False):
"""
获得指定长度,不同规则的随机字符串,可以包含数字,字母和标点符号
:param:
* length: (int) 随机字符串的长度
* letters: (bool) 随机字符串是否包含字母,默认包含
* digits: (bool) 随机字符串是否包含数字,默认不包含
* punctuation: (bool) 随机字符串是否包含特殊标点符号,默认不包含
:return:
* random_str: (string) 指定规则的随机字符串
举例如下::
print('--- get_random_str demo---')
print(get_random_str(6))
print(get_random_str(6, digits=True))
print(get_random_str(12, punctuation=True))
print(get_random_str(6, letters=False, digits=True))
print(get_random_str(12, letters=False, digits=True, punctuation=True))
print('---')
执行结果::
--- get_random_str demo---
nRBDHf
jXG5wR
)I;rz{ob&Clg
427681
*"4$0^`2}%9{
---
"""
random_source = ''
random_source += string.ascii_letters if letters else ''
random_source += string.digits if digits else ''
random_source += string.punctuation if punctuation else ''

random_str = ''.join(random.sample(random_source, length))
return random_str
49 changes: 49 additions & 0 deletions test/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,52 @@ def test_hmac_sha256_01(self):
message = 'Hello HMAC'
secret = '12345678'
assert hmac_sha256(secret, message) == '5eb8bdabdaa43f61fb220473028e49d40728444b4322f3093decd9a356afd18f'

# test Base64() tc
def test_base64_01(self):
assert Base64.string('hello world') == b'aGVsbG8gd29ybGQ='

assert len(Base64.file('./test/test_conf.ini')) != 0

assert Base64.decode(b'aGVsbG8gd29ybGQ=') == b'hello world'

# test Base64() tc
def test_base64_02(self):

assert GetMD5.string('hello world') != b'aGVsbG8gd29ybGQ=='
assert Base64.decode(b'aGVsbG8gd29ybGQ=') != b'hello'

if sys.version > '3':
with pytest.raises(FileNotFoundError):
GetMD5.file('./test/test_conf1.ini')
else:
with pytest.raises(IOError):
GetMD5.file('./test/test_conf1.ini')

assert GetMD5.file('./test/test_conf.ini') != b'bb7528c9778b2377e30b0f7e4c26fef0'

# test get_random_str()
def test_get_random_str_01(self):
assert len(get_random_str(6)) == 6

import re

digits_pattern = re.compile('[0-9]+')
letters_pattern = re.compile('[a-zA-Z]+')
letters_digits_pattern = re.compile('[0-9a-zA-Z]+')
punctuation_ord_list = [ord(item) for item in string.punctuation]

letter_str = get_random_str(6)
assert letters_pattern.match(letter_str)

letter_digits_str = get_random_str(6, digits=True)
assert letters_digits_pattern.match(letter_digits_str)

digits_str = get_random_str(6, letters=False, digits=True)
assert digits_pattern.match(digits_str)

punctuation_str = get_random_str(6, letters=False, punctuation=True)
for item in punctuation_str:
assert ord(item) in punctuation_ord_list

assert len(get_random_str(12, letters=False, digits=True, punctuation=True)) == 12

0 comments on commit 08d5629

Please sign in to comment.