Skip to main content

lambdas logo


Build Status codecov Documentation Status Python Version wemake-python-styleguide Telegram chat


Write short and fully-typed lambdas where you need them.

Features

  • Allows to write lambdas as _
  • Fully typed with annotations and checked with mypy, PEP561 compatible
  • Has a bunch of helpers for better composition
  • Easy to start: has lots of docs, tests, and tutorials

Installation

pip install lambdas

You also need to configure mypy correctly and install our plugin:

# In setup.cfg or mypy.ini:
[mypy]
plugins =
  lambdas.contrib.mypy.lambdas_plugin

We recommend to use the same mypy settings we use.

Examples

Imagine that you need to sort an array of dictionaries like so:

>>> scores = [
...     {'name': 'Nikita', 'score': 2},
...     {'name': 'Oleg', 'score': 1},
...     {'name': 'Pavel', 'score': 4},
... ]

>>> print(sorted(scores, key=lambda item: item['score']))
[{'name': 'Oleg', 'score': 1}, {'name': 'Nikita', 'score': 2}, {'name': 'Pavel', 'score': 4}]

And it works perfectly fine. Except, that you have to do a lot of typing for such a simple operation.

That's where lambdas helper steps in:

>>> from lambdas import _

>>> scores = [
...     {'name': 'Nikita', 'score': 2},
...     {'name': 'Oleg', 'score': 1},
...     {'name': 'Pavel', 'score': 4},
... ]

>>> print(sorted(scores, key=_['score']))
[{'name': 'Oleg', 'score': 1}, {'name': 'Nikita', 'score': 2}, {'name': 'Pavel', 'score': 4}]

It might really save you a lot of effort, when you use a lot of lambda functions. Like when using returns library.

We can easily create math expressions:

>>> from lambdas import _

>>> math_expression = _ * 2 + 1
>>> print(math_expression(10))
21
>>> complex_math_expression = 50 / (_ ** 2) * 2
>>> print(complex_math_expression(5))
100.0

Work in progress:

  • _.method() is not supported yet for the same reason
  • TypedDicts are not tested with __getitem__
  • __getitem__ does not work with list and tuples (collections), only dicts (mappings)

For now you will have to use regular lamdbas in these cases.

Release files for lambdas 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lambdas 0.2.0
File Size Uploaded
lambdas-0.2.0.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lambdas 0.2.0
File Interpreter ABI Platform
lambdas-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size:12.3 kB

Release files / lambdas-0.2.0.tar.gz

Download URL lambdas-0.2.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b9ddeb7d10f6f6abe665fe4a21e7441890696a42b406b9c3ef3de8fac256ebe8
BLAKE2b-256 checksum
How to use checksums
db13dcc0a750047115dafdcd6f40f5af339567b985d99f6a14a91f6884ae2b37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.3 Linux/6.1.46-1-lts

Release files / lambdas-0.2.0-py3-none-any.whl

Download URL lambdas-0.2.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4cd3e6f10966951c5810b019d1693a0c0881d7646b2bbad28f87e4f43f8a053e
BLAKE2b-256 checksum
How to use checksums
daea52296bff57c194bc74d3367e88e26dc253991d50a54de8f48b0dbb2293db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.3 Linux/6.1.46-1-lts

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page