forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
52 lines (44 loc) · 2.02 KB
/
utils.py
File metadata and controls
52 lines (44 loc) · 2.02 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
from numbers import Number as Num
def compare_dict(dict1, dict2, equivalent=True, msg='', tol=10e-8):
for key in dict1:
if key not in dict2:
return (False,
"{0} should be {1}".format(
list(dict1.keys()), list(dict2.keys())))
for key in dict1:
if isinstance(dict1[key], dict):
equivalent, msg = compare_dict(dict1[key],
dict2[key],
tol=tol)
elif isinstance(dict1[key], Num) and isinstance(dict2[key], Num):
if not comp_nums(dict1[key], dict2[key], tol):
return False, "['{0}'] = {1} should be {2}".format(key,
dict1[key],
dict2[key])
elif is_num_list(dict1[key]) and is_num_list(dict2[key]):
if not comp_num_list(dict1[key], dict2[key], tol):
return False, "['{0}'] = {1} should be {2}".format(key,
dict1[key],
dict2[key])
elif not (dict1[key] == dict2[key]):
return False, "['{0}'] = {1} should be {2}".format(key,
dict1[key],
dict2[key])
if not equivalent:
return False, "['{0}']".format(key) + msg
return equivalent, msg
def comp_nums(num1, num2, tol=10e-8):
return abs(num1-num2) < tol
def comp_num_list(list1, list2, tol=10e-8):
for item1, item2 in zip(list1, list2):
if not comp_nums(item1, item2, tol):
return False
return True
def is_num_list(item):
try:
for thing in item:
if not isinstance(thing, Num):
raise TypeError
except TypeError:
return False
return True