music21.common.weakrefTools

Functions

music21.common.weakrefTools.unwrapWeakref(referent: ReferenceType | Any) Any

Utility function that gets an object that might be an object itself or a weak reference to an object. It returns obj() if it’s a weakref. and obj if it’s not.

>>> class Mock:
...     pass
>>> a1 = Mock()
>>> a2 = Mock()
>>> a2.strong = a1
>>> a2.weak = common.wrapWeakref(a1)
>>> common.unwrapWeakref(a2.strong) is a1
True
>>> common.unwrapWeakref(a2.weak) is a1
True
>>> common.unwrapWeakref(a2.strong) is common.unwrapWeakref(a2.weak)
True
music21.common.weakrefTools.wrapWeakref(referent: _T) ReferenceType | _T

utility function that wraps objects as weakrefs but does not wrap already wrapped objects; also prevents wrapping the unwrappable “None” type, etc.

>>> import weakref
>>> class Mock:
...     pass
>>> a1 = Mock()
>>> ref1 = common.wrapWeakref(a1)
>>> ref1
<weakref at 0x101f29ae8; to ...Mock' at 0x101e45358>
>>> ref2 = common.wrapWeakref(ref1)
>>> ref2
<weakref at 0x101f299af; to ...Mock' at 0x101e45358>
>>> ref3 = common.wrapWeakref(5)
>>> ref3
5