Misc Utilities¶
misc
¶
DeprecationWarningMeta
¶
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:
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.