Utilities¶
default_filter |
Attribute filter. |
create_patches |
Create a patch for each member of a module or a class. |
find_patches |
Find all the patches created through decorators. |
get_attribute |
Retrieve an attribute while bypassing the descriptor protocol. |
get_original_attribute |
Retrieve an overriden attribute that has been stored. |
DecoratorData |
Decorator data. |
get_decorator_data |
Retrieve any decorator data from an object. |
-
gorilla.default_filter(name, obj)[source]¶ Attribute filter.
It filters out module attributes, and also methods starting with an underscore
_.This is used as the default filter for the
create_patches()function and thepatches()decorator.Parameters: - name (str) – Attribute name.
- obj (object) – Attribute value.
Returns: Whether the attribute should be returned.
Return type: bool
-
gorilla.create_patches(destination, root, settings=None, traverse_bases=True, filter=<function default_filter>, recursive=True, use_decorators=True)[source]¶ Create a patch for each member of a module or a class.
Parameters: - destination (object) – Patch destination.
- root (object) – Root object, either a module or a class.
- settings (gorilla.Settings) – Settings.
- traverse_bases (bool) – If the object is a class, the base classes are also traversed.
- filter (function) – Attributes for which the function returns
Falseare skipped. The function needs to define two parameters:name, the attribute name, andobj, the attribute value. IfNone, no attribute is skipped. - recursive (bool) – If
True, and a hit occurs due to an attribute at the destination already existing with the given name, and both the member and the target attributes are classes, then instead of creating a patch directly with the member attribute value as is, a patch for each of its own members is created with the target as new destination. - use_decorators (bool) –
Trueto take any modifier decorator into consideration to allow for more granular customizations.
Returns: The patches.
Return type: list of gorilla.Patch
Note
A ‘target’ differs from a ‘destination’ in that a target represents an existing attribute at the destination about to be hit by a patch.
See also
-
gorilla.find_patches(modules, recursive=True)[source]¶ Find all the patches created through decorators.
Parameters: - modules (list of module) – Modules and/or packages to search the patches in.
- recursive (bool) –
Trueto search recursively in subpackages.
Returns: Patches found.
Return type: list of gorilla.Patch
Raises: TypeError– The input is not a valid package or module.
-
gorilla.get_attribute(obj, name)[source]¶ Retrieve an attribute while bypassing the descriptor protocol.
As per the built-in
getattr()function, if the input object is a class then its base classes might also be searched until the attribute is found.Parameters: - obj (object) – Object to search the attribute in.
- name (str) – Name of the attribute.
Returns: The attribute found.
Return type: object
Raises: AttributeError– The attribute couldn’t be found.
-
gorilla.get_original_attribute(obj, name, id='default')[source]¶ Retrieve an overriden attribute that has been stored.
Parameters: - obj (object) – Object to search the attribute in.
- name (str) – Name of the attribute.
- id (str) – Identifier of the original attribute to retrieve from the stack.
Returns: The attribute found.
Return type: object
Raises: AttributeError– The attribute couldn’t be found.See also
-
class
gorilla.DecoratorData[source]¶ Decorator data.
-
patches¶ Patches created through the decorators.
Type: list of gorilla.Patch
-
override¶ Any overriding value defined by the
destination(),name(), andsettings()decorators.Type: dict
-
-
gorilla.get_decorator_data(obj, set_default=False)[source]¶ Retrieve any decorator data from an object.
Parameters: - obj (object) – Object.
- set_default (bool) – If no data is found, a default one is set on the object and returned,
otherwise
Noneis returned.
Returns: The decorator data or
None.Return type: