"""Functions implementing boolean checks."""fromabcimportABCfromtypingimportAnyfromattrsimportcmp_usingfromtyping_extensionsimportis_protocol# Used for comparing pandas dataframes in attrs classeseq_dataframe=cmp_using(lambdax,y:x.equals(y))
[docs]defis_abstract(cls:Any)->bool:"""Determine if a given class is abstract. This check is more general sense than ``inspect.abstract``, which only verifies if a class has abstract methods. The latter can be problematic when the class has no abstract methods but is nevertheless not directly usable, for example, because it has uninitialized members, which are only covered in its non-"abstract" subclasses. By contrast, this method simply checks if the class derives from ``abc.ABC`` or is a protocol class. Args: cls: The class to be inspected. Returns: ``True`` if the class is "abstract" (see definition above), ``False`` else. """returnABCincls.__bases__oris_protocol(cls)
[docs]defstrtobool(val:str)->bool:"""Convert a string representation of truth to ``True`` or ``False``. Adapted from distutils. True values are ``y``, ``yes``, ``t``, ``true``, ``on``, and ``1``. False values are ``n``, ``no``, ``f``, ``false``, ``off``, and ``0``. Raises a ``ValueError`` if ``val`` is anything else. Args: val: String to be checked. Returns: The ``bool`` value of the corresponding string representation. Raises: ValueError: If ``val`` cannot be evaluated to a suitable boolean value. """ifval.lower()in("y","yes","t","true","on","1"):returnTrueifval.lower()in("n","no","f","false","off","0"):returnFalseraiseValueError(f"Invalid truth value: {val}")
[docs]defcheck_if_in(element:Any,allowed:list):"""Check if an element is in a given list of elements. Args: element: The element to be checked allowed: The corresponding list Raises: ValueError: If ``element`` is not in ``allowed``. """ifelementnotinallowed:raiseValueError(f"The value '{element}' is not allowed. Must be one of {allowed}.")