forked from VertaAI/modeldb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
127 lines (92 loc) · 3.43 KB
/
Copy pathutils.py
File metadata and controls
127 lines (92 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import contextlib
import copy
import random
import os
import sys
from string import printable
import requests
from verta._internal_utils import _utils
from verta._protos.public.uac import Organization_pb2 as _OrganizationService
from hypothesis import strategies as st
def gen_none():
return None
def gen_bool():
return random.random() > .5
def gen_float(start=1, stop=None):
if stop is None:
return random.random()*start
else:
return random.uniform(start, stop)
def gen_int(start=10, stop=None):
return random.randrange(start, stop)
def gen_str(length=8):
return ''.join([chr(random.randrange(97, 123))
for _
in range(length)])
def gen_list(length=8):
"""Generates a list with mixed-type elements."""
gen_el = lambda fns=(gen_none, gen_bool, gen_float, gen_int, gen_str): random.choice(fns)()
return [gen_el() for _ in range(length)]
def gen_dict(length=8):
"""Generates a single-level dict with string keys and mixed-type values."""
gen_val = lambda fns=(gen_none, gen_bool, gen_float, gen_int, gen_str): random.choice(fns)()
res = {}
while len(res) < length:
res[gen_str()] = gen_val()
return res
@st.composite
def st_scalars(draw):
# pylint: disable=bad-continuation
return draw(st.none()
| st.booleans()
| st.integers()
| st.floats(allow_nan=False, allow_infinity=False)
| st.text(printable))
@st.composite
def st_json(draw, max_size=6):
# pylint: disable=bad-continuation
return draw(st.recursive(st_scalars(),
lambda children: st.lists(children,
min_size=1,
max_size=max_size)
| st.dictionaries(st.text(printable),
children,
min_size=1,
max_size=max_size)))
@st.composite
def st_keys(draw):
return draw(st.text(sorted(_utils._VALID_FLAT_KEY_CHARS), min_size=1))
@st.composite
def st_key_values(draw, min_size=1, max_size=12, scalars_only=False):
return draw(st.dictionaries(st_keys(),
st_scalars() if scalars_only else st_json(),
min_size=min_size,
max_size=max_size))
@contextlib.contextmanager
def chdir(new_dir):
"""
Context manager for safely changing current working directory.
Without this, if a test involving a directory change fails then subsequent tests would likely
fail as well due to a bad execution state.
"""
old_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(old_dir)
@contextlib.contextmanager
def sys_path_manager():
"""
Context manager for safely modifying `sys.path`.
Without this, if a test involving a `sys.path` modification fails then subsequent tests could
possibly fail as well due to a bad execution state.
"""
old_sys_path = copy.copy(sys.path)
try:
yield sys.path
finally:
sys.path = old_sys_path
def get_build_ids(status):
# get the set of build_ids in the status of the stage:
return set(map(lambda comp: comp["build_id"], status["components"]))