@@ -24,6 +24,7 @@ class VarnameRetrievingError(Exception):
2424
2525
2626def _get_frame (caller ):
27+ """Get the frame at `caller` depth"""
2728 try :
2829 return sys ._getframe (caller + 1 )
2930 except Exception as exc :
@@ -130,17 +131,30 @@ def will(caller=1, raise_exc=False):
130131 """Detect the attribute name right immediately after a function call.
131132
132133 Examples:
133- ```python
134- def i_will():
135- will = varname.will()
136- func = lambda: 0
137- func.will = will
138- return func
139-
140- func = i_will().abc
141-
142- # func.will == 'abc'
143- ```
134+ >>> class AwesomeClass:
135+ >>> def __init__(self):
136+ >>> self.will = None
137+
138+ >>> def permit(self):
139+ >>> self.will = will()
140+ >>> if self.will == 'do':
141+ >>> # let self handle do
142+ >>> return self
143+ >>> raise AttributeError(
144+ >>> 'Should do something with AwesomeClass object'
145+ >>> )
146+
147+ >>> def do(self):
148+ >>> if self.will != 'do':
149+ >>> raise AttributeError("You don't have permission to do")
150+ >>> return 'I am doing!'
151+
152+ >>> awesome = AwesomeClass()
153+ >>> # AttributeError: You don't have permission to do
154+ >>> awesome.do()
155+ >>> # AttributeError: Should do something with AwesomeClass object
156+ >>> awesome.permit()
157+ >>> awesome.permit().do() == 'I am doing!'
144158
145159 Args:
146160 caller (int): At which stack this function is called.
@@ -186,6 +200,23 @@ def i_will():
186200def inject (obj ):
187201 """Inject attribute `__varname__` to an object
188202
203+ Examples:
204+ >>> class MyList(list):
205+ >>> pass
206+
207+ >>> a = varname.inject(MyList())
208+ >>> b = varname.inject(MyList())
209+
210+ >>> a.__varname__ == 'a'
211+ >>> b.__varname__ == 'b'
212+
213+ >>> a == b
214+
215+ >>> # other methods not affected
216+ >>> a.append(1)
217+ >>> b.append(1)
218+ >>> a == b
219+
189220 Args:
190221 obj: An object that can be injected
191222
@@ -210,6 +241,15 @@ def inject(obj):
210241def nameof (* args , caller = 1 ):
211242 """Get the names of the variables passed in
212243
244+ Examples:
245+ >>> a = 1
246+ >>> aname = nameof(a)
247+ >>> # aname == 'a
248+
249+ >>> b = 2
250+ >>> aname, bname = nameof(a, b)
251+ >>> # aname == 'a', bname == 'b'
252+
213253 Args:
214254 *args: A couple of variables passed in
215255
@@ -273,18 +313,38 @@ def namedtuple(*args, **kwargs):
273313 the variable name.
274314
275315 So instead of:
276- >>> from collections import namedtuple
277- >>> Name = namedtuple('Name', ['first', 'last'])
316+ >>> from collections import namedtuple
317+ >>> Name = namedtuple('Name', ['first', 'last'])
278318
279319 You can do:
280- >>> from varname import namedtuple
281- >>> Name = namedtuple(['first', 'last'])
320+ >>> from varname import namedtuple
321+ >>> Name = namedtuple(['first', 'last'])
322+
323+ Args:
324+ *args: arguments for `collections.namedtuple` except `typename`
325+ **kwargs: keyword arguments for `collections.namedtuple`
326+ except `typename`
282327 """
283328 typename = varname (raise_exc = True )
284329 return standard_namedtuple (typename , * args , ** kwargs )
285330
286331class Wrapper :
287- """A wrapper with ability to retrieve the variable name"""
332+ """A wrapper with ability to retrieve the variable name
333+
334+ Examples:
335+ >>> foo = Wrapper(True)
336+ >>> # foo.name == 'foo'
337+ >>> # foo.value == True
338+
339+ >>> val = {}
340+ >>> bar = Wrapper(val)
341+ >>> # bar.name == 'bar'
342+ >>> # bar.value is val
343+
344+ Attributes:
345+ name (str): The variable name to which the instance is assigned
346+ value (any): The value this wrapper wraps
347+ """
288348
289349 def __init__ (self , value ):
290350 self .name = varname ()
0 commit comments