Operator overloading for fast anonymous functions.
Project description
A placeholder uses operator overloading to create partially bound functions on-the-fly. When used in a binary expression, it will return a callable object with the other argument bound. It's useful for replacing lambda in functional programming, and resembles Scala's placeholders.
Usage
from placeholder import _ # single underscore
_.age < 18 # lambda obj: obj.age < 18
_[key] ** 2 # lambda obj: obj[key] ** 2
Note _ has special meaning in other contexts, such as the previous output in interactive shells. Assign to a different name as needed. Kotlin uses it, but in Python it is a common short name for an iterator.
_ is a singleton of an F class, and F expressions can also be used with functions.
from placeholder import F
-F(len) # lambda obj: -len(obj)
All applicable double underscore methods are supported.
Performance
Every effort is made to optimize the placeholder instance. It's 20-40x faster than similar libraries on PyPI.
Placeholders are also iterable, allowing direct access to the underlying functions.
(func,) = _.age # operator.attrgetter('age')
Performance should generally be comparable to inlined expressions, and faster than lambda. Below are some example benchmarks.
min(data, key=operator.itemgetter(-1)) # 1x
min(data, key=_[-1]) # 1.3x
min(data, key=lambda x: x[-1]) # 1.6x
Installation
% pip install placeholder
Tests
100% branch coverage.
% pytest [--cov]
Changes
1.3
- Python >=3.7 required
- Deprecated accessing
funcattribute of partial object
1.2.1
- Setup fix
1.2
- Python >=3.6 required
- Optimized
partialimplementation
1.1
- Additional unary functions
1.0
- Removed
__(double underscore) - Variable arguments of first function
- Method callers and multi-valued getters
0.7
- Deprecated
__(double underscore)
0.6
- Optimized composite functions
- Renamed to
_(single underscore) for consistency
0.5
- Unary operators
__call__implementsmethodcaller__getitem__supports only single argument- Improved error handling
composerobject deprecated in favor of optimizedFexpression
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 Distributions
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 placeholder-1.3.tar.gz.
File metadata
- Download URL: placeholder-1.3.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.5.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6393d87df58b79ed27169b564e62049481ab94fb28eb791f11758b575b250d90
|
|
| MD5 |
54e3b04cdab943bffc6bb8badf582be5
|
|
| BLAKE2b-256 |
a487e4a7ff3030f1b62e0bdfb08fbbb1b8ef919a6f9651c2ca39b65f10ad1f54
|
File details
Details for the file placeholder-1.3-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: placeholder-1.3-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 10.5 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.10.0rc2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c5a6b6665012a57c3be6ca2fd093d60069d3ddaae85234ebc499e8a3010f6b9
|
|
| MD5 |
bf475c7280370b0fe1fb1abaf5d55f6d
|
|
| BLAKE2b-256 |
bd4b8058698f3f63b6f7d10f25f500801f44320413bd0330f077a2aa2ada6801
|
File details
Details for the file placeholder-1.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
- Upload date:
- Size: 20.0 kB
- Tags: CPython 3.10, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07cab015f6dffd9334d0e19be7b8575947a971164dc07f9f98cfca85bda18bcb
|
|
| MD5 |
db25a1ee97e4edb61ff2f6e42b5a6dcb
|
|
| BLAKE2b-256 |
b39d20fd4073d13ebe32dea7faff75bb4f3c84d427073871b375d735cd92a255
|
File details
Details for the file placeholder-1.3-cp310-cp310-macosx_10_14_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp310-cp310-macosx_10_14_x86_64.whl
- Upload date:
- Size: 7.1 kB
- Tags: CPython 3.10, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.10.0rc2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc4fdecbb62cb2505888822076640794e461ef4c17bcb21525f7b0e8b1c60ae7
|
|
| MD5 |
e3c8be6ab322ae2853ccc91232cdb928
|
|
| BLAKE2b-256 |
54768e989d9941830ad05e75fb3c67d9db7e1960ea36375a5b2dd0d3545b329d
|
File details
Details for the file placeholder-1.3-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: placeholder-1.3-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 10.5 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3af3ed8b176f1ea783ee3442d423af8aada5741d9867bb33565583cf8888e2d
|
|
| MD5 |
8ca0cdfc1fb126ff481a90c1853c87ff
|
|
| BLAKE2b-256 |
0ae5cfcf09f9fd118a0a18ad60fdea24e4e65d0119b3f640fa6c3e76b2078e5e
|
File details
Details for the file placeholder-1.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
- Upload date:
- Size: 19.8 kB
- Tags: CPython 3.9, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3728a3e303616e7457e774cca4a93a973fa84282dcb71b825198966d41fddce7
|
|
| MD5 |
3bbe2704db279336ae916203f47fdf78
|
|
| BLAKE2b-256 |
57f539983314486ced2a3cb72104621df1300cc12965c008e4e7e536cabf28d8
|
File details
Details for the file placeholder-1.3-cp39-cp39-macosx_10_14_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp39-cp39-macosx_10_14_x86_64.whl
- Upload date:
- Size: 7.1 kB
- Tags: CPython 3.9, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a6bb334458a92c3f0bc93983c934b1d7cb627b6d2f383f8ad0233976391e847
|
|
| MD5 |
215060e46649898cec8b303a8b9a86bb
|
|
| BLAKE2b-256 |
781c610ad5b4d1b8be7fdfeacf051e37e32ededc7d9db03b8efbe6373070f9d5
|
File details
Details for the file placeholder-1.3-cp38-cp38-win_amd64.whl.
File metadata
- Download URL: placeholder-1.3-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 10.3 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
602a5195c23a5148d7c741c8288b6f699cee3a5d7189413ace373af2d50a2ebf
|
|
| MD5 |
6bafd0986077802e7869035803aa27ff
|
|
| BLAKE2b-256 |
40441a1d823729bbc2ec81169340d81542d90a2c7533c3fa44e9cbc4ed43d7b6
|
File details
Details for the file placeholder-1.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
- Upload date:
- Size: 19.9 kB
- Tags: CPython 3.8, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ef7811368fa56593814385dd4f7620e40e42e298d0ec2f23e82d90b58622b5a
|
|
| MD5 |
7bab54d8c9bb03ae33de3739c1ca19d4
|
|
| BLAKE2b-256 |
a730700fd05fe6a02c6f28c0a15edf328b4aef152d9e9c48d7a4b17fa81b1f7b
|
File details
Details for the file placeholder-1.3-cp38-cp38-macosx_10_14_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp38-cp38-macosx_10_14_x86_64.whl
- Upload date:
- Size: 7.0 kB
- Tags: CPython 3.8, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91192f7664f47f293203103f3997e4a125c5a89d1d4a35b991fedea3503cb8aa
|
|
| MD5 |
f31ed2027885a62f0eeaab02d5f87249
|
|
| BLAKE2b-256 |
cef0eda98f6b052de351fbceb9e6ded0e1fec20b233446b09f1bdaf18a66f615
|
File details
Details for the file placeholder-1.3-cp37-cp37m-win_amd64.whl.
File metadata
- Download URL: placeholder-1.3-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 10.1 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11dd75bd36a1ff260b8e315a7389fce57c2a7c70f81a2669702c65a380635872
|
|
| MD5 |
ff8b063d5ff44265194d6eb6b33cea79
|
|
| BLAKE2b-256 |
df730aaaecb4034141de01094e4da5e7a4953786d08e3af0acbedf24a1681ad3
|
File details
Details for the file placeholder-1.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
- Upload date:
- Size: 19.6 kB
- Tags: CPython 3.7m, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3892a419277c7cfea1070bb31761d543fff59a1d2706a039528cfa298990d0c
|
|
| MD5 |
ca99ce367ea1a49a39e5cf2b047d2b33
|
|
| BLAKE2b-256 |
e763fb2bd28bfe84ca36782de30972ba0552a9b343fcb7b96f60c50dbd1b0df8
|
File details
Details for the file placeholder-1.3-cp37-cp37m-macosx_10_14_x86_64.whl.
File metadata
- Download URL: placeholder-1.3-cp37-cp37m-macosx_10_14_x86_64.whl
- Upload date:
- Size: 6.9 kB
- Tags: CPython 3.7m, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c84d3ec2c4a6345abaa73e1bb163a49ca8c22a864500cb4e9fe76aa9021a11c
|
|
| MD5 |
85901bf517af937f4548e20b090430a9
|
|
| BLAKE2b-256 |
adbf3e25a3a1f0f0e04c71ebb2d00f72bcbaa378b88cfaff7bc2649099798fbb
|