提交 23b9c9f1 编写于 作者: HansBug's avatar HansBug 😆

add test for random_hex and random_hex_with_timestamp

上级 6a9e51cc
......@@ -8,4 +8,5 @@ treevalue.utils
enum
final
func
random
singleton
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
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))
......@@ -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
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`.
"""
random = Random()
random.seed(seed)
rnd = Random()
rnd.seed(seed)
try:
yield random
yield rnd
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)
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.
先完成此消息的编辑!
想要评论请 注册