mirror_from¶
Utility functions for mirroring attributes and methods between objects.
This module provides :func:mirror_from as a class decorator to forward
attribute access to a member object, along with :func:mirror_attributes,
:func:mirror_all, and :func:unmirror for copying and removing attributes
between object instances.
mirror_from(origin_name, methods)
¶
Return a class decorator that mirrors methods from a named origin attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin_name
|
The name of the attribute on each instance that holds the origin object whose methods will be mirrored. |
required | |
methods
|
A list of method names to mirror onto the decorated class. |
required |
Returns:
| Type | Description |
|---|---|
|
A class decorator that sets each listed method as a property forwarding |
|
|
calls to the corresponding method on the origin attribute. |
Source code in py_misc_utils/mirror_from.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
mirror_attributes(src, dest, attributes)
¶
Copy named attributes from one object to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
The source object to read attributes from. |
required | |
dest
|
The destination object to set attributes on. |
required | |
attributes
|
A list of attribute names to copy. |
required |
Source code in py_misc_utils/mirror_from.py
72 73 74 75 76 77 78 79 80 81 | |
mirror_all(src, dest, excludes=(), includes=(), name=None)
¶
Mirror all public attributes from src to dest, optionally recording them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
The source object to read attributes from. |
required | |
dest
|
The destination object to set attributes on. |
required | |
excludes
|
Optional collection of attribute names to skip. |
()
|
|
includes
|
Optional collection of attribute names to include even if they start with an underscore. |
()
|
|
name
|
Optional identifier used to store the list of mirrored attributes on dest for later unmirroring. |
None
|
Returns:
| Type | Description |
|---|---|
|
A tuple of attribute names that were mirrored. |
Source code in py_misc_utils/mirror_from.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
unmirror(dest, attributes=None, name=None)
¶
Remove previously mirrored attributes from an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest
|
The object to remove attributes from. |
required | |
attributes
|
Optional collection of attribute names to remove. If None and name is provided, the stored mirrored list is used. |
None
|
|
name
|
Optional identifier used to retrieve the stored mirrored attribute list from dest. |
None
|
Source code in py_misc_utils/mirror_from.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |