Skip to main content

Typed lambdas that are short and readable

Project description

lambdas logo


Build Status Coverage Status Documentation Status Python Version wemake-python-styleguide Checked with mypy


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

We also 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']))

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:

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

print(sorted(scores, key=_['score']))

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

Work in progress:

  • _.some_attribute is not supported yet, because we need a complex mypy plugin for this
  • _.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


Release history Release notifications

Download files

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

Files for lambdas, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size lambdas-0.1.0-py3-none-any.whl (4.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size lambdas-0.1.0.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page