Skip to content

Commit a886277

Browse files
authored
Pump to 0.3.0 (#21)
* Cleanup and add changelog * Add docs * Fix API style * Update build.yml * Update build.yml * Update build.yml * Update build.yml * Update mkdocs.yml * Give up github pages, use rtd instead. * Update requirements for mkdocs * Add compiled API * Update mkdocs config * Don't buid docs automatically for now. * Update README * Update README.md * 0.3.0
1 parent 529a90b commit a886277

File tree

12 files changed

+305
-166
lines changed

12 files changed

+305
-166
lines changed

.github/workflows/build.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ jobs:
1919
- name: Install dependencies
2020
run: |
2121
python -m pip install --upgrade pip
22-
pip install poetry
2322
pip install pylint
23+
pip install poetry
2424
poetry install -v
2525
- name: Run pylint
2626
run: pylint varname.py
27+
# - name: Update docs
28+
# run: |
29+
# pip install mkdocs
30+
# pip install mkdocs-material
31+
# pip install mkapi
32+
# pip install pymdown-extensions
33+
# cd docs; ln -s ../README.md index.md; ln -s ../logo.png; cd -
34+
# mkdocs gh-deploy
35+
# if: success() && matrix.python-version == 3.8 && github.event_name == 'push'
2736
- name: Test with pytest
2837
run: poetry run pytest tests/ --junitxml=junit/test-results-${{ matrix.python-version }}.xml
2938
- name: Upload pytest test results
@@ -40,6 +49,5 @@ jobs:
4049
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
4150
coverage-reports: .coverage.xml
4251
- name: Publish to PyPI
43-
if: success() && matrix.python-version == 3.8 && github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
52+
if: success() && matrix.python-version == 3.8 && github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
4453
run: poetry publish --build -u ${{ secrets.pypi_user }} -p ${{ secrets.pypi_password }}
45-

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ poetry.lock
105105

106106
# backup files
107107
*.bak
108+
109+
docs/index.md
110+
docs/logo.png

README.md

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
![python-varname][7]
22

3-
[![Pypi][3]][4] [![Github][5]][6] [![PythonVers][8]][4] [![Travis building][10]][11] [![Codacy][12]][13] [![Codacy coverage][14]][13]
3+
[![Pypi][3]][4] [![Github][5]][6] [![PythonVers][8]][4] [![Travis building][10]][11] [![Codacy][12]][13] [![Codacy coverage][14]][13] [![Chat on gitter][17]][18]
44

55
Dark magics about variable names in python
66

7+
[Change Log][16] | [API][15]
8+
79
## Installation
810
```shell
911
pip install python-varname
1012
```
1113

1214
## Features
1315

14-
- Fetching variable names from inside the function/class call
15-
- Fetching variable names directly (added in `v0.1.2`)
16-
- A value wrapper to store the variable name that a value is assigned to (added in `v0.1.1`)
17-
- Detecting next immediate attribute name (added in `v0.1.4`)
18-
- Shortcut for `collections.namedtuple` (added in `v0.1.6`)
19-
- Injecting `__varname__` to objects (added in `v0.1.7`)
16+
- Fetching variable names from inside the function/class call using `varname`
17+
- Fetching variable names directly using `nameof`
18+
- A value wrapper to store the variable name that a value is assigned to using `Wrapper`
19+
- Detecting next immediate attribute name using `will`
20+
- Shortcut for `collections.namedtuple`
21+
- Injecting `__varname__` to objects
2022

2123
## Credits
2224

2325
Thanks goes to these awesome people/projects:
2426

2527
<table>
2628
<tr>
27-
<td align="center">
29+
<td align="center" style="min-width: 75px">
2830
<a href="https://github.com/alexmojaki">
2931
<img src="https://avatars0.githubusercontent.com/u/3627481?s=400&v=4" width="50px;" alt=""/>
3032
<br /><sub><b>@alexmojaki</b></sub>
3133
</a>
3234
</td>
33-
<td align="center">
35+
<td align="center" style="min-width: 75px">
3436
<a href="https://github.com/alexmojaki/executing">
3537
<img src="https://via.placeholder.com/50?text=executing" width="50px;" alt=""/>
3638
<br /><sub><b>executing</b></sub>
@@ -54,6 +56,7 @@ Thanks goes to these awesome people/projects:
5456
```
5557

5658
- `varname` calls being buried deeply
59+
5760
```python
5861
def function():
5962
# I know that at which stack this will be called
@@ -70,6 +73,7 @@ Thanks goes to these awesome people/projects:
7073
```
7174

7275
- Retrieving instance name of a class
76+
7377
```python
7478
class Klass:
7579
def __init__(self):
@@ -87,6 +91,7 @@ Thanks goes to these awesome people/projects:
8791
```
8892

8993
- Some unusual use
94+
9095
```python
9196
func = [function()]
9297
# func == ['func']
@@ -114,6 +119,10 @@ Thanks goes to these awesome people/projects:
114119
func = function2()
115120
# func == 'func'
116121

122+
a = lambda: 0
123+
a.b = function()
124+
# a.b == 'b'
125+
117126
# Since v0.1.3
118127
# We can ask varname to raise exceptions
119128
# if it fails to detect the variable name
@@ -127,7 +136,8 @@ Thanks goes to these awesome people/projects:
127136
except VarnameRetrieveingError:
128137
return None
129138

130-
a.b = get_name() # None
139+
a = {}
140+
a['b'] = get_name() # None
131141
```
132142

133143
### Value wrapper
@@ -230,18 +240,21 @@ a == b
230240
```
231241

232242
## Limitations
233-
- Working in `ipython REPL` but not in standard `python console`
234-
- You have to know at which stack the function/class will be called (caller's depth)
235-
- Not working with `reticulate` from `R` since it cuts stacks to the most recent one.
236-
- ~~`nameof` cannot be used in statements in `pytest`~~ (supported in `v0.2.0`)
237-
```diff
238-
-a = 1
239-
+assert nameof(a) == 'a'
240-
-# Retrieving failure.
241-
-# The right way:
242-
-aname = nameof(a)
243-
-assert aname == 'a'
243+
`python-varname` is all depending on `executing` package to look for the node.
244+
It does not work with any environment where `executing` is not able to detect the node.
245+
For example:
246+
247+
- Environments where other AST magics apply. For example: `pytest`, `ipython`, `macropy`, or `birdseye`.
248+
This will not work with `pytest`:
249+
```python
250+
a = 1
251+
assert nameof(a) == 'a'
252+
253+
# do this instead
254+
name_a = nameof(a)
255+
assert name_a == 'a'
244256
```
257+
- `R` with `reticulate`.
245258

246259
[1]: https://github.com/pwwang/python-varname
247260
[3]: https://img.shields.io/pypi/v/python-varname?style=flat-square
@@ -255,3 +268,7 @@ a == b
255268
[12]: https://img.shields.io/codacy/grade/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
256269
[13]: https://app.codacy.com/manual/pwwang/python-varname/dashboard
257270
[14]: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
271+
[15]: https://pwwang.github.io/python-varname/api/varname/
272+
[16]: https://pwwang.github.io/python-varname/CHANGELOG/
273+
[17]: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
274+
[18]: https://gitter.im/python-varname/community

README.rst

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
.. role:: raw-html-m2r(raw)
2-
:format: html
3-
41

52

63
.. image:: logo.png
@@ -32,10 +29,16 @@
3229
.. image:: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
3330
:target: https://img.shields.io/codacy/coverage/ed851ff47b194e3e9389b2a44d6f21da?style=flat-square
3431
:alt: Codacy coverage
35-
<https://app.codacy.com/manual/pwwang/python-varname/dashboard>`_
32+
<https://app.codacy.com/manual/pwwang/python-varname/dashboard>`_ `
33+
.. image:: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
34+
:target: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
35+
:alt: Chat on gitter
36+
<https://gitter.im/python-varname/community>`_
3637

3738
Dark magics about variable names in python
3839

40+
`Change Log <https://pwwang.github.io/python-varname/CHANGELOG/>`_ | `API <https://pwwang.github.io/python-varname/api/varname/>`_
41+
3942
Installation
4043
------------
4144

@@ -47,12 +50,38 @@ Features
4750
--------
4851

4952

50-
* Fetching variable names from inside the function/class call
51-
* Fetching variable names directly (added in ``v0.1.2``\ )
52-
* A value wrapper to store the variable name that a value is assigned to (added in ``v0.1.1``\ )
53-
* Detecting next immediate attribute name (added in ``v0.1.4``\ )
54-
* Shortcut for ``collections.namedtuple`` (added in ``v0.1.6``\ )
55-
* Injecting ``__varname__`` to objects (added in ``v0.1.7``\ )
53+
* Fetching variable names from inside the function/class call using ``varname``
54+
* Fetching variable names directly using ``nameof``
55+
* A value wrapper to store the variable name that a value is assigned to using ``Wrapper``
56+
* Detecting next immediate attribute name using ``will``
57+
* Shortcut for ``collections.namedtuple``
58+
* Injecting ``__varname__`` to objects
59+
60+
Credits
61+
-------
62+
63+
Thanks goes to these awesome people/projects:
64+
65+
66+
.. raw:: html
67+
68+
<table>
69+
<tr>
70+
<td align="center" style="min-width: 75px">
71+
<a href="https://github.com/alexmojaki">
72+
<img src="https://avatars0.githubusercontent.com/u/3627481?s=400&v=4" width="50px;" alt=""/>
73+
<br /><sub><b>@alexmojaki</b></sub>
74+
</a>
75+
</td>
76+
<td align="center" style="min-width: 75px">
77+
<a href="https://github.com/alexmojaki/executing">
78+
<img src="https://via.placeholder.com/50?text=executing" width="50px;" alt=""/>
79+
<br /><sub><b>executing</b></sub>
80+
</a>
81+
</td>
82+
</tr>
83+
</table>
84+
5685

5786
Usage
5887
-----
@@ -141,6 +170,10 @@ Retrieving the variable names from inside a function call/class instantiation
141170
func = function2()
142171
# func == 'func'
143172
173+
a = lambda: 0
174+
a.b = function()
175+
# a.b == 'b'
176+
144177
# Since v0.1.3
145178
# We can ask varname to raise exceptions
146179
# if it fails to detect the variable name
@@ -154,7 +187,8 @@ Retrieving the variable names from inside a function call/class instantiation
154187
except VarnameRetrieveingError:
155188
return None
156189
157-
a.b = get_name() # None
190+
a = {}
191+
a['b'] = get_name() # None
158192
159193
Value wrapper
160194
^^^^^^^^^^^^^
@@ -265,16 +299,22 @@ Injecting ``__varname__``
265299
Limitations
266300
-----------
267301

302+
``python-varname`` is all depending on ``executing`` package to look for the node.
303+
It does not work with any environment where ``executing`` is not able to detect the node.
304+
For example:
305+
306+
307+
*
308+
Environments where other AST magics apply. For example: ``pytest``\ , ``ipython``\ , ``macropy``\ , or ``birdseye``.
309+
This will not work with ``pytest``\ :
310+
311+
.. code-block:: python
312+
313+
a = 1
314+
assert nameof(a) == 'a'
268315
269-
* Working in ``ipython REPL`` but not in standard ``python console``
270-
* You have to know at which stack the function/class will be called (caller's depth)
271-
* Not working with ``reticulate`` from ``R`` since it cuts stacks to the most recent one.
272-
* :raw-html-m2r:`<del>\ ``nameof`` cannot be used in statements in ``pytest``\ </del>` (supported in ``v0.2.0``\ )
273-
.. code-block:: diff
316+
# do this instead
317+
name_a = nameof(a)
318+
assert name_a == 'a'
274319
275-
-a = 1
276-
+assert nameof(a) == 'a'
277-
-# Retrieving failure.
278-
-# The right way:
279-
-aname = nameof(a)
280-
-assert aname == 'a'
320+
* ``R`` with ``reticulate``.

docs/CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## v0.1.0
2+
- Implement `varname` function
3+
4+
## v0.1.1
5+
- Add a value wrapper `Wrapper` class
6+
7+
## v0.1.2
8+
- Add function `nameof`
9+
10+
## v0.1.3
11+
- Add arugment `raise_exc` for `varname` to raise an exception instead of returning `var_<index>`
12+
13+
## v0.1.4
14+
- Add `will` to detect next immediate attribute name
15+
16+
## v0.1.5
17+
- Fix `will` from a property call
18+
19+
## v0.1.6
20+
- Fit situations when frames cannot be fetched
21+
- Add shortcut for `namedtuple`
22+
23+
## v0.1.7
24+
- Add `inject` function
25+
26+
## v0.2.0
27+
- Fix #5 and fit nameof in more cases
28+
29+
## v0.3.0
30+
- Use sys._getframe instead of inspect.stack for efficiency (#9)
31+
- Add alternative way of testing bytecode nameof (#10)
32+
- Drop support for pytest, don't try to find node when executing fails
33+
- Remodel `will` for better logic
34+
- Support attributes in varname and nameof (#14)

docs/api.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
.mkapi-docstring{
3+
line-height: 1;
4+
}
5+
.mkapi-node pre {
6+
line-height: 1.5;
7+
}
8+
.mkapi-node * {
9+
font-size: .65rem !important;
10+
}

docs/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mkdocs-material
2+
pymdown-extensions
3+
mkapi

mkdocs.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
site_name: python-varname
2+
theme:
3+
name: 'material'
4+
plugins:
5+
- search
6+
- mkapi
7+
extra_css:
8+
- api.css
9+
markdown_extensions:
10+
- markdown.extensions.admonition
11+
- pymdownx.superfences:
12+
preserve_tabs: true
13+
- toc:
14+
baselevel: 2
15+
nav:
16+
- "": mkapi/api/varname
17+
- index.md
18+
- API: api/varname.md
19+
- 'Change Log': CHANGELOG.md

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 = "python-varname"
7-
version = "0.2.0"
7+
version = "0.3.0"
88
description = "Retrieving variable names of function or class calls."
99
authors = [ "pwwang <pwwang@pwwang.com>",]
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='python-varname',
24-
version='0.2.0',
24+
version='0.3.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)