Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from chinapnr/hujun_20181218
Hujun 20181218
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
``fish_random`` 基本函数包 | ||
============================= | ||
|
||
.. autosummary:: | ||
gen_string_by_range.get_date_range | ||
|
||
.. automodule:: fish_random | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# coding=utf-8 | ||
""" | ||
``fish_random`` 包含的是一些生成随机值的函数。 | ||
""" | ||
|
||
# 2018.12.26 v1.1.5 created | ||
import random | ||
from fishbase.fish_common import get_random_str | ||
|
||
|
||
# v1.0.15 edit by Hu Jun #163 | ||
def gen_string_by_range(min_length, max_length, prefix=None, suffix=None): | ||
""" | ||
指定一个前后缀以及字符串长度,返回随机生成带有前后缀及指定长度的字符串 | ||
:param: | ||
* min_length: (int) 字符串最小长度 | ||
* max_length: (int) 字符串最小长度 | ||
* prefix: (string) 字符串前缀 | ||
* suffix: (string) 字符串后缀 | ||
:return: | ||
* random_str: (string) 指定长度、前后缀的随机字符串 | ||
举例如下:: | ||
print('--- gen_string_by_range demo ---') | ||
print(gen_string_by_range(5, 7)) | ||
print(gen_string_by_range(5, 7, prefix='FISHBASE_')) | ||
print(gen_string_by_range(5, 7, prefix='FISHBASE_', suffix='.py')) | ||
print('---') | ||
执行结果:: | ||
--- gen_string_by_range demo --- | ||
q4uo6E8 | ||
FISHBASE_8uCBEUH | ||
FISHBASE_D4wRX2.py | ||
--- | ||
""" | ||
if not all([isinstance(min_length, int), isinstance(max_length, int)]): | ||
raise ValueError('min_length and max_length should be int, but we got {} and {}'. | ||
format(type(min_length), type(max_length))) | ||
|
||
if min_length > max_length: | ||
raise ValueError('min_length should less than or equal to max_length') | ||
|
||
random_str_len = random.randint(min_length, max_length) | ||
init_rand_str = get_random_str(random_str_len, digits=True) | ||
prefix = prefix if prefix else '' | ||
suffix = suffix if suffix else '' | ||
|
||
random_str = ''.join([prefix, init_rand_str, suffix]) | ||
|
||
return random_str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# coding=utf-8 | ||
# fish_date.py 单元测试 | ||
# 2018.6.11 create by Hu Jun | ||
|
||
import pytest | ||
|
||
from fishbase.fish_random import * | ||
|
||
|
||
# 2018.6.11 v1.1.5 #163 create by Hu Jun | ||
class TestFishRandom(object): | ||
# test gen_string_by_range() tc | ||
def test_gen_string_by_range_01(self): | ||
assert 1 <= len(gen_string_by_range(1, 5)) <= 5 | ||
assert gen_string_by_range(1, 5, prefix='fishbase_').startswith('fishbase_') | ||
assert gen_string_by_range(1, 5, suffix='.py')[-3:] == '.py' | ||
|
||
# test gen_string_by_range() tc | ||
def test_gen_string_by_range_02(self): | ||
with pytest.raises(ValueError): | ||
gen_string_by_range(1, '12', prefix='fishbase_') | ||
|
||
with pytest.raises(ValueError): | ||
gen_string_by_range(10, 4, prefix='fishbase_') |