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
62 lines (52 loc) · 1.83 KB
/
utils.py
File metadata and controls
62 lines (52 loc) · 1.83 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
def shorten_str(text, to_chars=100):
return (text if len(text) <= 100 else (
text[:(to_chars // 2)] + "..." + text[-(to_chars // 2):]))
from types import ModuleType
import copy
def get_ord(num):
assert num != 0, "use strictly positive numbers in get_ord()"
nums = {1: "first", 2: "second", 3:"third", 4:"fourth",
5: "fifth", 6: "sixth", 7:"seventh", 8:"eight",
9: "nineth", 10: "tenth"}
if num in nums:
return(nums[num])
else:
return("%dth" % num)
def get_times(num):
nums = {1:"once", 2:"twice"}
if num in nums:
return(nums[num])
else:
return("%d times" % num)
def get_num(num):
nums = {0:"no", 1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight"}
if num in nums:
return(nums[num])
else:
return(str(num))
def copy_env(env, keep_objs=None):
if keep_objs is None:
keep_objs = []
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 ( key in keep_objs or 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 x.__class__.__name__ == "WorkerProcess"
return(x)