forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (40 loc) · 1.21 KB
/
utils.py
File metadata and controls
56 lines (40 loc) · 1.21 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
from types import ModuleType
import copy
import os
def include_v1():
return os.environ.get("PYTHONWHAT_V2_ONLY", "") != "1"
def v2_only():
return not include_v1()
def shorten_string(text):
if len(text) > 50:
text = text[0:45] + "..."
return text
def has_newline(text):
return "\n" in text
def copy_env(env):
mutableTypes = (tuple, list, dict)
# One list comprehension to filter list. Might need some cleaning, but it
# works
ipy_ignore = ["In", "Out", "get_ipython", "quit", "exit"]
update_env = {
key: copy.deepcopy(value)
for key, value in env.items()
if not any(
(key.startswith("_"), isinstance(value, ModuleType), key in ipy_ignore)
)
and isinstance(value, mutableTypes)
}
updated_env = dict(env)
updated_env.update(update_env)
return updated_env
def first_lower(s):
return s[:1].lower() + s[1:] if s else ""
def check_str(x):
assert isinstance(x, str), "object isn't string where string expected"
return x
def check_dict(x):
assert isinstance(x, dict), "object isn't dict where dict expected"
return x
def check_process(x):
assert "Process" in x.__class__.__name__
return x