Skip to main content

Typed lambdas that are short and readable

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambdas-0.2.0.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

lambdas-0.2.0-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page