Skip to content

Misc Utilities

misc

DeprecationWarningMeta

Bases: type

Metaclass that issues a deprecation warning whenever a class using it is instantiated.

attr_equal(obj, attr, value)

Check if the attribute of the object is equal to the given value. Returns False if the attribute does not exist or is not equal to the value.

Parameters:

  • obj –

    The object to check.

  • attr (str) –

    The attribute name to check.

  • value –

    The value to compare against.

Returns:

  • bool –

    True if the attribute exists and is equal to the value, False otherwise.

first(iterable, default=None)

Return the first element of an iterable.

Parameters:

  • iterable (Iterable[T]) –

    The iterable to get the first element from.

  • default (Optional[T], default: None ) –

    The value to return if the iterable is empty. If None and the iterable is empty, raises StopIteration.

Returns:

  • Optional[T] –

    The first element of the iterable, or the default value if empty.

Raises:

  • StopIteration –

    If the iterable is empty and no default is provided.

  • TypeError –

    If the object is not iterable.

has_length(obj)

Check if an object has a length (implements len) and len() works correctly.

Parameters:

  • obj (Any) –

    The object to check for length support.

Returns:

  • bool ( bool ) –

    True if the object supports len() and doesn't raise an error, False otherwise.

join_lists(list_of_lists)

Flatten a collection of iterables into a single list.

Parameters:

  • list_of_lists (Iterable[Iterable[T]]) –

    An iterable containing iterables to be flattened.

Returns:

  • List[T] –

    List[T]: A new list containing all elements from the input iterables in order.

Raises:

  • TypeError –

    If any item in list_of_lists is not iterable.

Examples:

>>> join_lists([[1, 2], [3, 4], [5]])
[1, 2, 3, 4, 5]
>>> join_lists([])
[]
>>> join_lists([[], [1], [], [2, 3]])
[1, 2, 3]

validate_and_suggest_corrections(obj, values, *, max_suggestions=3, cutoff=0.6)

Return obj if it is contained in values. Otherwise raise a helpful ValueError that lists the closest matches.

Parameters:

  • obj –

    Any The value to validate.

  • values –

    Iterable[Any] The set of allowed values.

  • max_suggestions –

    int, optional How many typo-hints to include at most (default 3).

  • cutoff –

    float, optional Similarity threshold for suggestions (0.0-1.0, default 0.6).

Returns:

  • Any –

    The original obj if it is valid.

Raises:

  • ValueError –

    With a friendly message that points out possible typos.