Module motor_odm.helpers
¶
This module contains various supporting functions that can be used independently of the Motor-ODM framework. Some of these utilities can be found in similar form in other packages or frameworks and are adapted here to reduce the number of dependencies.
-
motor_odm.helpers.
inherit_class
(name: str, self: Optional[T], parent: T, merge: Iterable[str] = None) → T¶ Performs a pseudo-inheritance by creating a new class that inherits from
self
andparents
. This is useful to support intuitive inheritance on inner classes (typically namedMeta
).Note that this method neither returns
self
nor any of theparents
but a new type that inherits from both.- Parameters
name – The name of the newly created type.
self – The primary base class (fields in this class take preference over the
parents
’ fields.parent – The secondary base class (a pseudo-parent of
self
).merge – A list of fields that should not be replaces during inheritance but merged. This only works for some types.
- Returns
A new type inheriting from
self
andparents
.
-
motor_odm.helpers.
merge_values
(value1: Any, value2: Any) → Any¶ Merges two values.
This method works only for specific collection types (namely lists, dicts and sets). For other values a
ValueError
is raised.The type of the resulting value is determined by the type of
value2
, howevervalue1
may override some of the contents invalue2
(e.g. replace values for dict keys).
-
motor_odm.helpers.
monkey_patch
(cls: Union[type, module], name: Optional[str] = None) → Callable[[C], C]¶ Monkey patches class or module by adding to it decorated function. Anything overwritten can be accessed via a
.original
attribute of the decorated object.- Parameters
cls – The class or module to be patched.
name – The name of the attribute to be patched.
- Returns
A decorator that monkey patches
cls.name
and returns the decorated function.