Composable Python via syntactic sugar
Project description
Why pysweet?
Consider the following variants of the same logic:
acc = []
for x in range(10):
y = x + 1
if y % 2 == 0:
acc.extend([y, y * 2])
acc = [
z for y in (x + 1 for x in range(10))
for z in [y, y * 2] if y % 2 == 0
]
from itertools import chain
acc = list(chain.from_iterable(map(
lambda x: [x, x * 2],
filter(
lambda x: x % 2 == 0,
map(lambda x: x + 1, range(10)),
),
)))
The imperative style can grow complex as requirements evolve;
The comprehension style can get complicated when nested;
The functional style is not very readable in Python.
In JavaScript, the same logic can be written:
acc = [...Array(10).keys()]
.map(x => x + 1)
.filter(x => x % 2 === 0)
.flatMap(x => [x, x * 2])
Can we write analogous code in Python?
Now you can with pysweet!
from pysweet import Iterable_
acc = (
Iterable_(range(10))
.map(lambda x: x + 1)
.filter(lambda x: x % 2 == 0)
.flat_map(lambda x: [x, x * 2])
.to_list()
)
pysweet also offers many other similar features.
pysweet is:
lightweight, with around 100 lines of code;
mostly syntactic sugar, so it is performant, easy to debug, and easy to learn;
successfully used in production.
Sweeten Python with pysweet!
Sample features
Iterable with method chaining, in the spirit of JavaScript and Scala:
from pysweet import Iterable_
(
Iterable_([1, 2])
.map(lambda x: x + 1)
.to_list()
)
# [2, 3]
Multi-expression lambda, common in modern languages:
from pysweet import block_
val = lambda: block_(
x := 1,
x + 1,
)
# val() == 2
Statement as expression, in the spirit of Scala and Haskell (if_ is also the ternary operator):
from pysweet import if_, try_, raise_
if_(
True,
lambda: 1,
lambda: 2,
)
# 1
try_(
lambda: raise_(Exception('test')),
catch=lambda e: str(e),
)
# 'test'
Next steps
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysweet_func-1.1.9.tar.gz.
File metadata
- Download URL: pysweet_func-1.1.9.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ab6aa2b36087b2fd604a22f040110537904e483eae2cc54c7e46d8549a108ab
|
|
| MD5 |
9ab41301ca285f475af07a052f8e4d61
|
|
| BLAKE2b-256 |
041d3fa2bc3712d89ba9fd0d26070c2e9a0db9c279f14d195ce40273a6304cf0
|
File details
Details for the file pysweet_func-1.1.9-py3-none-any.whl.
File metadata
- Download URL: pysweet_func-1.1.9-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4b0ff2e2d6925a38c46a10ff7510eb4ea852f5da11a3b7d64cd9452d66a3899
|
|
| MD5 |
f8f6d5d5490008683cc83c26b37a2d07
|
|
| BLAKE2b-256 |
702984b03aab692226d30087c75e7f8c5c6234c81fcd6c7f8c0df6380fe73dc8
|