Skip to content

Commit

Permalink
0.12.2 (#103)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pwwang authored Nov 29, 2023
1 parent 84320c6 commit be560cc
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ celerybeat-schedule

# virtualenv
venv/
.venv/
ENV/

# Spyder project settings
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Note if you use `python < 3.8`, install `varname < 0.11`
- A decorator to register `__varname__` to functions/classes, using `register`
- A helper function to create dict without explicitly specifying the key-value pairs, using `jsobj`
- A `debug` function to print variables with their names and values
- `exec_code` to replace `exec` where source code is available at runtime

## Credits

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

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

Expand Down Expand Up @@ -383,6 +384,26 @@ debug(a+a)
debug(a+a, vars_only=True) # ImproperUseError
```

### Replacing `exec` with `exec_code`

```python
from varname import argname
from varname.helpers import exec_code

class Obj:
def __init__(self):
self.argnames = []

def receive(self, arg):
self.argnames.append(argname('arg', func=self.receive))

obj = Obj()
# exec('obj.receive(1)') # Error
exec_code('obj.receive(1)')
exec_code('obj.receive(2)')
obj.argnames # ['1', '2']
```

## Reliability and limitations

`varname` is all depending on `executing` package to look for the node.
Expand Down
23 changes: 22 additions & 1 deletion README.raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Note if you use `python < 3.8`, install `varname < 0.11`
- A decorator to register `__varname__` to functions/classes, using `register`
- A helper function to create dict without explicitly specifying the key-value pairs, using `jsobj`
- A `debug` function to print variables with their names and values
- `exec_code` to replace `exec` where source code is available at runtime

## Credits

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

Foo().a = 1 # prints: {_out}
```
Expand Down Expand Up @@ -378,6 +379,26 @@ debug(a+a)
debug(a+a, vars_only=True) # {_exc}
```

### Replacing `exec` with `exec_code`

```python
from varname import argname
from varname.helpers import exec_code

class Obj:
def __init__(self):
self.argnames = []

def receive(self, arg):
self.argnames.append(argname('arg', func=self.receive))

obj = Obj()
# exec('obj.receive(1)') # Error
exec_code('obj.receive(1)')
exec_code('obj.receive(2)')
obj.argnames # ['1', '2']
```

## Reliability and limitations

`varname` is all depending on `executing` package to look for the node.
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.12.2

- Add `helpers.exec_code` function to replace `exec` so that source code available at runtime

## 0.12.1

- Bump executing to 2.0.1
Expand Down
Loading

0 comments on commit be560cc

Please sign in to comment.