lab_geometry/geometry/validation.py

32 lines
1.0 KiB
Python

#
#
#
def is_numeric(value):
"Checks whether value is an integer or a float (decimal number)."
return isinstance(value, int) or isinstance(value, float)
def is_coordinates(value):
"Checks whether value is a tuple of two numeric values."
try:
x, y = value
return is_numeric(x) and is_numeric(y)
except (TypeError, ValueError):
return False
def has_method(obj, method_name):
"Checks whether the object has a method called `method_name`."
attr = getattr(obj, method_name, None)
return callable(attr)
def expect_type(obj, expected_type):
"""Checks whether obj is of the expected type.
If multiple types are provided, checks whether obj is one of the types.
"""
try:
if not any(isinstance(obj, t) for t in expected_type):
raise TypeError(f"Expected {obj} to be one of the following types: {expected_type}")
except TypeError:
if not isinstance(obj, expected_type):
raise TypeError(f"Expected {obj} to be a {expected_type}")