Skip to content

Commit be560cc

Browse files
authored
0.12.2 (#103)
* Add `helpers.exec_code` function to replace `exec` where source code available at runtime * Update README and playground for `exec_code` * Fix type hint for sourcefile parameter * Add future import for annotations * Fix linting * 0.12.2
1 parent 84320c6 commit be560cc

10 files changed

+255
-98
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ celerybeat-schedule
8282

8383
# virtualenv
8484
venv/
85+
.venv/
8586
ENV/
8687

8788
# Spyder project settings

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Note if you use `python < 3.8`, install `varname < 0.11`
3131
- A decorator to register `__varname__` to functions/classes, using `register`
3232
- A helper function to create dict without explicitly specifying the key-value pairs, using `jsobj`
3333
- A `debug` function to print variables with their names and values
34+
- `exec_code` to replace `exec` where source code is available at runtime
3435

3536
## Credits
3637

@@ -323,7 +324,7 @@ func4(y, x, c=z) # prints: ('x', 'z')
323324
# __getattr__/__getitem__/__setattr/__setitem__/__add__/__lt__, etc.
324325
class Foo:
325326
def __setattr__(self, name, value):
326-
print(argname("name", "value"))
327+
print(argname("name", "value", func=self.__setattr__))
327328

328329
Foo().a = 1 # prints: ("'a'", '1')
329330

@@ -383,6 +384,26 @@ debug(a+a)
383384
debug(a+a, vars_only=True) # ImproperUseError
384385
```
385386

387+
### Replacing `exec` with `exec_code`
388+
389+
```python
390+
from varname import argname
391+
from varname.helpers import exec_code
392+
393+
class Obj:
394+
def __init__(self):
395+
self.argnames = []
396+
397+
def receive(self, arg):
398+
self.argnames.append(argname('arg', func=self.receive))
399+
400+
obj = Obj()
401+
# exec('obj.receive(1)') # Error
402+
exec_code('obj.receive(1)')
403+
exec_code('obj.receive(2)')
404+
obj.argnames # ['1', '2']
405+
```
406+
386407
## Reliability and limitations
387408

388409
`varname` is all depending on `executing` package to look for the node.

README.raw.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Note if you use `python < 3.8`, install `varname < 0.11`
3131
- A decorator to register `__varname__` to functions/classes, using `register`
3232
- A helper function to create dict without explicitly specifying the key-value pairs, using `jsobj`
3333
- A `debug` function to print variables with their names and values
34+
- `exec_code` to replace `exec` where source code is available at runtime
3435

3536
## Credits
3637

@@ -319,7 +320,7 @@ func4(y, x, c=z) # prints: {_out}
319320
# __getattr__/__getitem__/__setattr/__setitem__/__add__/__lt__, etc.
320321
class Foo:
321322
def __setattr__(self, name, value):
322-
print(argname("name", "value"))
323+
print(argname("name", "value", func=self.__setattr__))
323324

324325
Foo().a = 1 # prints: {_out}
325326
```
@@ -378,6 +379,26 @@ debug(a+a)
378379
debug(a+a, vars_only=True) # {_exc}
379380
```
380381

382+
### Replacing `exec` with `exec_code`
383+
384+
```python
385+
from varname import argname
386+
from varname.helpers import exec_code
387+
388+
class Obj:
389+
def __init__(self):
390+
self.argnames = []
391+
392+
def receive(self, arg):
393+
self.argnames.append(argname('arg', func=self.receive))
394+
395+
obj = Obj()
396+
# exec('obj.receive(1)') # Error
397+
exec_code('obj.receive(1)')
398+
exec_code('obj.receive(2)')
399+
obj.argnames # ['1', '2']
400+
```
401+
381402
## Reliability and limitations
382403

383404
`varname` is all depending on `executing` package to look for the node.

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## 0.12.2
4+
5+
- Add `helpers.exec_code` function to replace `exec` so that source code available at runtime
6+
37
## 0.12.1
48

59
- Bump executing to 2.0.1

0 commit comments

Comments
 (0)