Accessing Method Wrappers¶
The get_log_calls_wrapper()
and get_own_log_calls_wrapper()
classmethods¶
log_calls decorates a callable by “wrapping” it in a function (the wrapper) which has
attributes containing data about the callable: log_calls_settings
, containing settings,
and stats
, containing statistics. Access to these attributes requires access to the callable’s
wrapper.
It’s straightforward to access the wrapper of a decorated global function f
: after decoration,
f
refers to the wrapper. For methods and properties, however, the various kinds of methods
and the two ways of defining properties require different navigation paths to the wrapper.
log_calls hides this complexity, providing uniform access to the wrappers of methods and properties.
-
classmethod
decorated_class.
get_log_calls_wrapper
(fname: str) Classmethod of a decorated class. Call this on a decorated class or an instance thereof to access the wrapper of the callable named
fname
, in order to access the log_calls-added attributes forfname
.Parameters: fname – name of a method (instance method, staticmethod or classmethod), or the name of a property (treated as denoting the getter), or the name of a property concatenated with ‘.getter‘, ‘.setter‘ or ‘.deleter‘.
Note
If a property is defined using the
property
function, as inpropx = property(getx, setx, delx)
,where
getx
,setx
,delx
are methods of a class (orNone
), then each individual property can be referred to in two ways:- via the name of the method, eg.
setx
, or - via
propx.
qualifier, where qualifier is one ofsetter
,getter
,deleter
, as appropriate (sopropx.setter
also refers tosetx
)
Thus you can use either
dc.log_calls_wrapper('setx')
ordc.log_calls_wrapper('propx.setter')
wheredc
is a decorated class or an instance thereof.Raises: TypeError
iffname
is not astr
; ValueError iffname
isn’t as described above or isn’t in the__dict__
of decorated_class.Returns: wrapper of fname
iffname
is decorated,None
otherwise.- via the name of the method, eg.
-
classmethod
decorated_class.
get_own_log_calls_wrapper
() Classmethod of a decorated class. Call from within a method or property of a decorated class. Typically called on
self
from within instance methods, oncls
from within classmethods, and on the explicitly named enclosing, decorated classdecorated_class
from within staticmethods.Raises: ValueError
if caller is not decorated.Returns: the wrapper of the caller (a function), so that the caller can access its own log_calls attributes.