Skip to content

Commit 071bd56

Browse files
committed
Allow nameof to retrieve full name of chained attribute calls
1 parent eb99338 commit 071bd56

File tree

9 files changed

+210
-133
lines changed

9 files changed

+210
-133
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,6 @@ poetry.lock
109109
docs/index.md
110110
docs/logo.png
111111
docs/api/
112+
113+
# vscode's local history extension
114+
.history/

.pylintrc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,14 @@ disable=print-statement,
145145
no-name-in-module,
146146
no-member,
147147
broad-except,
148-
cyclic-import
148+
cyclic-import,
149+
too-few-public-methods,
150+
unbalanced-tuple-unpacking,
151+
no-self-use,
152+
too-few-public-methods,
153+
protected-access,
154+
too-many-instance-attributes,
155+
too-many-arguments
149156

150157
# Enable the message, report, category or checker with the given id(s). You can
151158
# either give multiple identifier separated by comma (,) or put this option
@@ -312,6 +319,7 @@ good-names=i,
312319
do,
313320
by,
314321
tf,
322+
on,
315323
op,
316324
Run,
317325
_

README.md

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ Thanks goes to these awesome people/projects:
5454
def function():
5555
return varname()
5656

57-
func = function()
58-
# func == 'func'
57+
func = function() # func == 'func'
5958
```
6059

6160
- `varname` calls being buried deeply
@@ -71,60 +70,51 @@ Thanks goes to these awesome people/projects:
7170
def function2():
7271
return function1()
7372

74-
func = function2()
75-
# func == 'func'
73+
func = function2() # func == 'func'
7674
```
7775

7876
- Retrieving instance name of a class
7977

8078
```python
81-
class Klass:
79+
class Foo:
8280
def __init__(self):
8381
self.id = varname()
8482

8583
def copy(self):
86-
# also able to fetch inside a member call
87-
return varname()
84+
# also able to fetch inside a method call
85+
copied = Foo() # copied.id == 'copied'
86+
copied.id = varname() # assign id to whatever variable name
87+
return copied
8888

89-
k = Klass()
90-
# k.id == 'k'
89+
k = Foo() # k.id == 'k'
9190

92-
k2 = k.copy()
93-
# k2 == 'k2'
91+
k2 = k.copy() # k2.id == 'k2'
9492
```
9593

9694
- Some unusual use
9795

9896
```python
99-
func = [function()]
100-
# func == ['func']
97+
func = [function()] # func == ['func']
10198

102-
func = [function(), function()]
103-
# func == ['func', 'func']
99+
func = [function(), function()] # func == ['func', 'func']
104100

105-
func = function(), function()
106-
# func = ('func', 'func')
101+
func = function(), function() # func = ('func', 'func')
107102

108-
func = func1 = function()
109-
# func == func1 == 'func'
103+
func = func1 = function() # func == func1 == 'func'
110104
# a warning will be printed
111105
# since you may not want func1 to be 'func'
112106

113-
x = func(y = func())
114-
# x == 'x'
107+
x = func(y = func()) # x == 'x'
115108

116109
# get part of the name
117-
func_abc = function()[-3:]
118-
# func_abc == 'abc'
110+
func_abc = function()[-3:] # func_abc == 'abc'
119111

120112
# function alias supported now
121113
function2 = function
122-
func = function2()
123-
# func == 'func'
114+
func = function2() # func == 'func'
124115

125116
a = lambda: 0
126-
a.b = function()
127-
# a.b == 'b'
117+
a.b = function() # a.b == 'b'
128118

129119
# Since v0.1.3
130120
# We can ask varname to raise exceptions
@@ -162,20 +152,23 @@ mydict = values_to_dict(foo, bar)
162152
from varname import varname, nameof
163153

164154
a = 1
165-
aname = nameof(a)
166-
# aname == 'a
155+
nameof(a) # 'a'
167156

168157
b = 2
169-
aname, bname = nameof(a, b)
170-
# aname == 'a', bname == 'b'
158+
nameof(a, b) # ('a', 'b')
171159

172160
def func():
173161
return varname() + '_suffix'
174162

175-
f = func()
176-
# f == 'f_suffix'
177-
fname = nameof(f)
178-
# fname == 'f'
163+
f = func() # f == 'f_suffix'
164+
nameof(f) # 'f'
165+
166+
# get full names of (chained) attribute calls
167+
func.a = func
168+
nameof(func.a, full=True) # 'func.a'
169+
170+
func.a.b = 1
171+
nameof(func.a.b, full=True) # 'func.a.b'
179172
```
180173

181174
### Detecting next immediate attribute name

README.rst

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
.. image:: logo.png
44
:target: logo.png
5-
:alt: python-varname
5+
:alt: varname
66

77

88
`
@@ -17,27 +17,33 @@
1717
.. image:: https://img.shields.io/pypi/pyversions/python-varname?style=flat-square
1818
:target: https://img.shields.io/pypi/pyversions/python-varname?style=flat-square
1919
:alt: PythonVers
20-
<https://pypi.org/project/python-varname/>`_ `
21-
.. image:: https://img.shields.io/travis/pwwang/python-varname?style=flat-square
22-
:target: https://img.shields.io/travis/pwwang/python-varname?style=flat-square
23-
:alt: Travis building
24-
<https://travis-ci.org/pwwang/python-varname>`_ `
20+
<https://pypi.org/project/python-varname/>`_
21+
.. image:: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20and%20Deploy?style=flat-square
22+
:target: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20and%20Deploy?style=flat-square
23+
:alt: Building
24+
25+
`
26+
.. image:: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20Docs?label=docs&style=flat-square
27+
:target: https://img.shields.io/github/workflow/status/pwwang/python-varname/Build%20Docs?label=docs&style=flat-square
28+
:alt: Docs and API
29+
<https://pwwang.github.io/python-varname/api/varname>`_ `
2530
.. image:: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
2631
:target: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
2732
:alt: Codacy
2833
<https://app.codacy.com/manual/pwwang/python-varname/dashboard>`_ `
2934
.. image:: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
3035
:target: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
3136
:alt: Codacy coverage
32-
<https://app.codacy.com/manual/pwwang/python-varname/dashboard>`_ `
37+
<https://app.codacy.com/manual/pwwang/python-varname/dashboard>`_
38+
`
3339
.. image:: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
3440
:target: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
3541
:alt: Chat on gitter
3642
<https://gitter.im/python-varname/community>`_
3743

3844
Dark magics about variable names in python
3945

40-
`Change Log <https://pwwang.github.io/python-varname/CHANGELOG/>`_ | `API <https://pwwang.github.io/python-varname/api/varname/>`_
46+
`Change Log <https://pwwang.github.io/python-varname/CHANGELOG/>`_ | `API <https://pwwang.github.io/python-varname/api/varname>`_
4147

4248
Installation
4349
------------
@@ -100,8 +106,7 @@ Retrieving the variable names from inside a function call/class instantiation
100106
def function():
101107
return varname()
102108
103-
func = function()
104-
# func == 'func'
109+
func = function() # func == 'func'
105110
106111
*
107112
``varname`` calls being buried deeply
@@ -118,62 +123,53 @@ Retrieving the variable names from inside a function call/class instantiation
118123
def function2():
119124
return function1()
120125
121-
func = function2()
122-
# func == 'func'
126+
func = function2() # func == 'func'
123127
124128
*
125129
Retrieving instance name of a class
126130

127131
.. code-block:: python
128132
129-
class Klass:
133+
class Foo:
130134
def __init__(self):
131135
self.id = varname()
132136
133137
def copy(self):
134-
# also able to fetch inside a member call
135-
return varname()
138+
# also able to fetch inside a method call
139+
copied = Foo() # copied.id == 'copied'
140+
copied.id = varname() # assign id to whatever variable name
141+
return copied
136142
137-
k = Klass()
138-
# k.id == 'k'
143+
k = Foo() # k.id == 'k'
139144
140-
k2 = k.copy()
141-
# k2 == 'k2'
145+
k2 = k.copy() # k2.id == 'k2'
142146
143147
*
144148
Some unusual use
145149

146150
.. code-block:: python
147151
148-
func = [function()]
149-
# func == ['func']
152+
func = [function()] # func == ['func']
150153
151-
func = [function(), function()]
152-
# func == ['func', 'func']
154+
func = [function(), function()] # func == ['func', 'func']
153155
154-
func = function(), function()
155-
# func = ('func', 'func')
156+
func = function(), function() # func = ('func', 'func')
156157
157-
func = func1 = function()
158-
# func == func1 == 'func'
158+
func = func1 = function() # func == func1 == 'func'
159159
# a warning will be printed
160160
# since you may not want func1 to be 'func'
161161
162-
x = func(y = func())
163-
# x == 'x'
162+
x = func(y = func()) # x == 'x'
164163
165164
# get part of the name
166-
func_abc = function()[-3:]
167-
# func_abc == 'abc'
165+
func_abc = function()[-3:] # func_abc == 'abc'
168166
169167
# function alias supported now
170168
function2 = function
171-
func = function2()
172-
# func == 'func'
169+
func = function2() # func == 'func'
173170
174171
a = lambda: 0
175-
a.b = function()
176-
# a.b == 'b'
172+
a.b = function() # a.b == 'b'
177173
178174
# Since v0.1.3
179175
# We can ask varname to raise exceptions
@@ -213,20 +209,23 @@ Getting variable names directly
213209
from varname import varname, nameof
214210
215211
a = 1
216-
aname = nameof(a)
217-
# aname == 'a
212+
nameof(a) # 'a'
218213
219214
b = 2
220-
aname, bname = nameof(a, b)
221-
# aname == 'a', bname == 'b'
215+
nameof(a, b) # ('a', 'b')
222216
223217
def func():
224218
return varname() + '_suffix'
225219
226-
f = func()
227-
# f == 'f_suffix'
228-
fname = nameof(f)
229-
# fname == 'f'
220+
f = func() # f == 'f_suffix'
221+
nameof(f) # 'f'
222+
223+
# get full names of (chained) attribute calls
224+
func.a = func
225+
nameof(func.a, full=True) # 'func.a'
226+
227+
func.a.b = 1
228+
nameof(func.a.b, full=True) # 'func.a.b'
230229
231230
Detecting next immediate attribute name
232231
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -294,12 +293,12 @@ Injecting ``__varname__``
294293
Reliability and limitations
295294
---------------------------
296295

297-
``python-varname`` is all depending on ``executing`` package to look for the node.
296+
``varname`` is all depending on ``executing`` package to look for the node.
298297
The node ``executing`` detects is ensured to be the correct one (see `this <https://github.com/alexmojaki/executing#is-it-reliable>`_\ ).
299298

300299
It partially works with environments where other AST magics apply, including
301300
``pytest``\ , ``ipython``\ , ``macropy``\ , ``birdseye``\ , ``reticulate`` with ``R``\ , etc. Neither
302-
``executing`` nor ``python-varname`` is 100% working with those environments. Use
301+
``executing`` nor ``varname`` is 100% working with those environments. Use
303302
it at your own risk.
304303

305304
For example:

docs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@
3737
- Change default of `raise_exc` to `True` for all related APIs
3838
- Deprecate `var_0`
3939
- Get rid of `VarnameRetrievingWarning`.
40+
41+
## v0.5.0
42+
- Allow `nameof` to retrieve full name of chained attribute calls
43+
- Add `__all__` to the module so that only desired APIs are exposed when `from varname import *`
44+
- Give more hints on `nameof` being called in a weird way when no soucecode available.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
44

55
[tool.poetry]
66
name = "varname"
7-
version = "0.4.0"
7+
version = "0.5.0"
88
description = "Retrieving variable names of function or class calls."
99
authors = [ "pwwang <[email protected]>",]
1010
license = "MIT"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
setup(
2222
long_description=readme,
2323
name='varname',
24-
version='0.4.0',
24+
version='0.5.0',
2525
description='Retrieving variable names of function or class calls.',
2626
python_requires='==3.*,>=3.6.0',
2727
project_urls={

0 commit comments

Comments
 (0)