Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
23b9c9f1
T
treevalue
项目概览
OpenDILab开源决策智能平台
/
treevalue
大约 1 年 前同步成功
通知
3
Star
213
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
treevalue
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
23b9c9f1
编写于
7月 29, 2021
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add test for random_hex and random_hex_with_timestamp
上级
6a9e51cc
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
97 addition
and
6 deletion
+97
-6
docs/source/api_doc/utils/index.rst
docs/source/api_doc/utils/index.rst
+1
-0
docs/source/api_doc/utils/random.rst
docs/source/api_doc/utils/random.rst
+20
-0
test/utils/test_random.py
test/utils/test_random.py
+11
-1
treevalue/utils/__init__.py
treevalue/utils/__init__.py
+1
-1
treevalue/utils/random.py
treevalue/utils/random.py
+40
-4
treevalue/utils/tree.py
treevalue/utils/tree.py
+24
-0
未找到文件。
docs/source/api_doc/utils/index.rst
浏览文件 @
23b9c9f1
...
...
@@ -8,4 +8,5 @@ treevalue.utils
enum
final
func
random
singleton
docs/source/api_doc/utils/random.rst
0 → 100644
浏览文件 @
23b9c9f1
treevalue.utils.random
============================
seed_random
~~~~~~~~~~~~~~~~~~
.. autofunction:: treevalue.utils.random.seed_random
random_hex
~~~~~~~~~~~~~~~~~~~
.. autofunction:: treevalue.utils.random.random_hex
random_hex_with_timestamp
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. autofunction:: treevalue.utils.random.random_hex_with_timestamp
test/utils/test_random.py
浏览文件 @
23b9c9f1
import
re
import
pytest
from
treevalue.utils
import
seed_random
from
treevalue.utils
import
seed_random
,
random_hex
,
random_hex_with_timestamp
@
pytest
.
mark
.
unittest
...
...
@@ -13,3 +15,11 @@ class TestUtilsRandom:
with
seed_random
(
233
)
as
rnd
:
a
,
b
,
c
=
rnd
.
randint
(
0x00
,
0xff
),
rnd
.
randint
(
0x00
,
0xff
),
rnd
.
randint
(
0x00
,
0xff
)
assert
(
a
,
b
,
c
)
==
(
89
,
118
,
247
)
def
test_random_hex
(
self
):
assert
re
.
fullmatch
(
r
'^[a-f0-9]{32}$'
,
random_hex
())
assert
re
.
fullmatch
(
r
'^[a-f0-9]{48}$'
,
random_hex
(
48
))
def
test_random_hex_with_timestamp
(
self
):
assert
re
.
fullmatch
(
r
'^\d{8}_\d{12}_[a-f0-9]{12}$'
,
random_hex_with_timestamp
())
assert
re
.
fullmatch
(
r
'^\d{8}_\d{12}_[a-f0-9]{48}$'
,
random_hex_with_timestamp
(
48
))
treevalue/utils/__init__.py
浏览文件 @
23b9c9f1
...
...
@@ -2,6 +2,6 @@ from .clazz import init_magic, class_wraps, common_bases, common_direct_base
from
.enum
import
int_enum_loads
from
.final
import
FinalMeta
from
.func
import
args_iter
,
dynamic_call
from
.random
import
seed_random
from
.random
import
seed_random
,
random_hex
,
random_hex_with_timestamp
from
.singleton
import
SingletonMeta
,
ValueBasedSingletonMeta
,
SingletonMark
from
.tree
import
build_tree
treevalue/utils/random.py
浏览文件 @
23b9c9f1
import
random
from
contextlib
import
contextmanager
from
datetime
import
datetime
from
random
import
Random
...
...
@@ -11,9 +13,43 @@ def seed_random(seed):
Arguments:
- seed (:obj:`int`): Random seed, should be a `int`.
"""
r
andom
=
Random
()
r
andom
.
seed
(
seed
)
r
nd
=
Random
()
r
nd
.
seed
(
seed
)
try
:
yield
r
andom
yield
r
nd
finally
:
random
.
seed
()
rnd
.
seed
()
def
random_hex
(
length
:
int
=
32
)
->
str
:
"""
Overview:
Generate random hex string.
Arguments:
- length (:obj:`int`): Length of hex string, default is `32`.
Returns:
- string (:obj:`str`): Generated string.
Examples:
>>> random_hex() # 'ca7f14b25aa4498efdacb54e9ff72784'
"""
return
''
.
join
([
hex
(
random
.
randint
(
0
,
15
))[
2
:]
for
_
in
range
(
length
)])
def
random_hex_with_timestamp
(
length
:
int
=
12
)
->
str
:
"""
Overview:
Generate random hex string, with prefix of timestamp.
Arguments:
- length (:obj:`int`): Length of hex string, default is `12`.
Returns:
- string (:obj:`str`): Generated string.
Examples:
>>> random_hex_with_timestamp() # '20210729_202059576266_69603d64afad'
"""
return
datetime
.
now
().
strftime
(
"%Y%m%d_%H%M%S%f"
)
+
"_"
+
random_hex
(
length
)
treevalue/utils/tree.py
浏览文件 @
23b9c9f1
import
re
from
queue
import
Queue
from
graphviz
import
Digraph
from
treelib
import
Tree
as
LibTree
from
.random
import
random_hex_with_timestamp
_ROOT_ID
=
'_root'
_NODE_ID_TEMP
=
'_node_{id}'
...
...
@@ -31,3 +35,23 @@ def build_tree(root, represent=None, iterate=None, recurse=None) -> LibTree:
_queue
.
put
((
_current_id
,
value
))
return
_tree
_NAME_PATTERN
=
re
.
compile
(
'^[a-zA-Z_][a-zA-Z0-9_]*$'
)
def
_title_flatten
(
title
):
title
=
re
.
sub
(
r
'[^a-zA-Z0-9_]+'
,
'_'
,
str
(
title
))
title
=
re
.
sub
(
r
'_+'
,
'_'
,
title
)
title
=
title
.
strip
(
'_'
).
lower
()
return
title
def
build_graph
(
root
,
name
=
None
,
title
=
None
,
represent
=
None
,
iterate
=
None
,
recurse
=
None
):
represent
=
represent
or
repr
iterate
=
iterate
or
(
lambda
x
:
x
.
items
())
recurse
=
recurse
or
(
lambda
x
:
hasattr
(
x
,
'items'
))
title
=
title
or
'untitled_'
+
random_hex_with_timestamp
()
name
=
name
or
_title_flatten
(
title
)
graph
=
Digraph
(
name
=
name
,
comment
=
title
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录