Source code for schrodinger.utils.sea.common
"""
Copyright Schrodinger, LLC. All rights reserved.
"""
__CHECK_SEA_DEBUG = False
__debug_str_buf = ""
[docs]def debug_print(s, nl=True):
"""
Prints out some information for debugging purposes.
:param s: The string to print.
:param nl: Appends a new line to the string and flushes out the current
contents in the print buffer.
"""
global __debug_str_buf
if (__CHECK_SEA_DEBUG):
if (nl):
print(__debug_str_buf + s)
__debug_str_buf = ""
else:
__debug_str_buf += s
[docs]def is_equal(a, b, epsilon=1E-6):
"""
Compares two floating numbers and returns True if the absolute
difference is within epsilon (defaults to 1E-6), False if not.
"""
if (a == b):
return True
if (float("inf") in [a, b]):
return False
# This is a very reliable implementation for finite numbers.
a = float(a)
b = float(b)
a1 = abs(a)
b1 = abs(b)
c1 = a1 if (a1 > b1) else b1
eps = epsilon * c1 if (c1 > 1.0) else epsilon
return abs(a - b) <= eps
[docs]def boolean(s):
"""
Returns True if the string 's' is one of the following: 'true', 'yes', and 'on', or False if it is one of the following:
'false', 'no', and 'off'.
Case difference will be ignored.
Raises a ValueError exception if 's' is none of the above strings.
"""
s = str(s).lower()
if (s in [
"true",
"yes",
"on",
]):
return True
elif (s in [
"false",
"no",
"off",
]):
return False
raise ValueError("'%s' is not a boolean value" % s)