Skip to content

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
def mirror_from(origin_name, methods):
  """Return a class decorator that mirrors methods from a named origin attribute.

  Args:
    origin_name: The name of the attribute on each instance that holds the
      origin object whose methods will be mirrored.
    methods: A list of method names to mirror onto the decorated class.

  Returns:
    A class decorator that sets each listed method as a property forwarding
    calls to the corresponding method on the origin attribute.
  """

  def origin_getter(method, obj):
    """Get the mirrored method of the origin object of an instance.

    Args:
      method: The name of the method to retrieve.
      obj: The instance whose origin attribute holds the origin object.

    Returns:
      The bound method of the origin object.
    """
    origin = getattr(obj, origin_name)

    return getattr(origin, method)

  def wrapper(cls):
    """Decorate a class, mirroring the requested methods.

    Args:
      cls: The class to decorate.

    Returns:
      The class itself, with one read-only property per mirrored method.
    """
    for method in methods:
      wrapped_method = functools.partial(origin_getter, method)
      setattr(cls, method, property(wrapped_method))

    return cls

  return wrapper

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
def mirror_attributes(src, dest, attributes):
  """Copy named attributes from one object to another.

  Args:
    src: The source object to read attributes from.
    dest: The destination object to set attributes on.
    attributes: A list of attribute names to copy.
  """
  for attr in attributes:
    setattr(dest, attr, getattr(src, attr))

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
def mirror_all(src, dest, excludes=(), includes=(), name=None):
  """Mirror all public attributes from src to dest, optionally recording them.

  Args:
    src: The source object to read attributes from.
    dest: The destination object to set attributes on.
    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.

  Returns:
    A tuple of attribute names that were mirrored.
  """
  excludes = set(excludes)
  for attr in dir(dest):
    if not attr.startswith('_'):
      excludes.add(attr)

  includes = set(includes)
  mirrored = []
  for attr in dir(src):
    if (not attr.startswith('_') or attr in includes) and attr not in excludes:
      setattr(dest, attr, getattr(src, attr))
      mirrored.append(attr)

  if name is not None:
    setattr(dest, _mirrored_field(name), tuple(mirrored))

  return tuple(mirrored)

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
def unmirror(dest, attributes=None, name=None):
  """Remove previously mirrored attributes from an object.

  Args:
    dest: The object to remove attributes from.
    attributes: Optional collection of attribute names to remove. If None and
      name is provided, the stored mirrored list is used.
    name: Optional identifier used to retrieve the stored mirrored attribute
      list from dest.
  """
  if name is not None:
    mfield = _mirrored_field(name)
    attributes = getattr(dest, mfield, None)
    delattr(dest, mfield)

  for attr in attributes or ():
    delattr(dest, attr)