https://github.com/fklc/namedassignments
Really small library to implement NamedExpressions in PEP 572 for Python <=3.7
https://github.com/fklc/namedassignments
pep pep572 python python-2 python-3 python-3-6 python2 python27 python3 python36
Last synced: 6 months ago
JSON representation
Really small library to implement NamedExpressions in PEP 572 for Python <=3.7
- Host: GitHub
- URL: https://github.com/fklc/namedassignments
- Owner: FKLC
- License: mit
- Archived: true
- Created: 2019-02-10T08:13:05.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-14T14:27:02.000Z (about 7 years ago)
- Last Synced: 2024-09-29T16:22:29.813Z (over 1 year ago)
- Topics: pep, pep572, python, python-2, python-3, python-3-6, python2, python27, python3, python36
- Language: Python
- Size: 2.93 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NamedAssignments
NamedAssignments is a library that I developed for myself to use named assignments in my Python <= 3.7 projects.
## What is named assignments?
Actual thing I don't even know how should I call them but this is the best name I found for them. If you've read [PEP 572](https://www.python.org/dev/peps/pep-0572/) there is a new operator coming in Python 3.8 called as ***Named Expressions*** (this is how it is going to look like in your code `:=`) which helps you to write more clean and shorter code.
## But how?
For original examples you can check [PEP 572 examples](https://www.python.org/dev/peps/pep-0572/#examples) directly but if you are looking this library you are probably not using Python 3.8 so just stay in here and continue reading.
* Caching computationally expensive computations (This is definitely a bad example of expensive computation but lets just act like it is)
###### Old way:
```python
# Makes two addition every time
strange_list = [[x + 1, x/(x + 1)] for x in range(5)]
```
###### Improved:
```python
# Makes only one addition every time
strange_list = [[N('y', lambda: x + 1), x/N.y] for x in range(5)]
```
* Usage in while loops
###### Old way:
```python
# conn is a accepted socket connection
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
```
###### Improved:
```python
# conn is a accepted socket connection
while N('data', conn.recv, 1024):
conn.sendall(N.data)
```
## Usage
Usage is not clean as original due to limitations but it is the cleanest way I found.
```python
from namedassignments import NamedAssignments as N
N(variable_name, function, *args, **kwargs)
```
## Installation
Library is avaible on PyPi so just run
```
pip install namedassignments
```
## API
Since the whole code is only 5 lines there is nothing too much. Library uses Python's magic method `__new__` to return what function returns this is all library does on the background.
* `__new__` Arguments:
* `variable_name`: Variable name you want to use while retrieving data. (String) (Required)
* `function`: Function you want to execute. (Function) (Required)
* `*args`: Arguments you want to pass function. (List) (Optional)
* `**kwargs`: Keyword arguments you want to pass function. (Dict) (Optional)