Initial commit
This commit is contained in:
31
geometry/validation.py
Normal file
31
geometry/validation.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
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}")
|
||||
Reference in New Issue
Block a user