Skip to main content

A lens library for python

Project description

[![Build Status](https://travis-ci.org/ingolemo/python-lenses.svg?branch=master)](https://travis-ci.org/ingolemo/python-lenses)
[![codecov](https://codecov.io/gh/ingolemo/python-lenses/branch/master/graph/badge.svg)](https://codecov.io/gh/ingolemo/python-lenses)

# Lenses

Lenses is a python library that helps you to manipulate large
data-structures without mutating them. It is inspired by the lenses in
Haskell, although it's much less principled and the api is more suitable
for python.


## Installation

You can install the latest github version using pip like so:

pip install lenses

You can uninstall similarly:

pip uninstall lenses


## Documentation

The lenses library makes liberal use of docstrings, which you can access
as normal with the `pydoc` shell command, the `help` function in the
repl, or by reading the source yourself.

Most users will only need the docs from `lenses.UnboundLens`. If you want
to add hooks to allow parts of the library to work with custom objects
then you should check out the `lenses.hooks` module. Most of the fancy
lens code is in the `lenses.optics` module for those who are curious
how everything works.

Some examples are given in the [`examples`](examples) folder and a tutorial
is available in the [`tutorial`](tutorial/index.md) folder.

Here's a simple example:

```python
>>> from pprint import pprint
>>> from lenses import lens
>>>
>>> data = [{'name': 'Jane', 'scores': ['a', 'a', 'b', 'a']},
... {'name': 'Richard', 'scores': ['c', None, 'd', 'c']},
... {'name': 'Zoe', 'scores': ['f', 'f', None, 'f']}]
...
>>> format_scores = lens.each_()['scores'].each_().instance_(str).call_upper()
>>> cheat = lens[2]['scores'].each_().set('a')
>>>
>>> corrected = format_scores(data)
>>> pprint(corrected)
[{'name': 'Jane', 'scores': ['A', 'A', 'B', 'A']},
{'name': 'Richard', 'scores': ['C', None, 'D', 'C']},
{'name': 'Zoe', 'scores': ['F', 'F', None, 'F']}]
>>>
>>> cheated = format_scores(cheat(data))
>>> pprint(cheated)
[{'name': 'Jane', 'scores': ['A', 'A', 'B', 'A']},
{'name': 'Richard', 'scores': ['C', None, 'D', 'C']},
{'name': 'Zoe', 'scores': ['A', 'A', 'A', 'A']}]

```


## License

python-lenses is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along
with this program. If not, see http://www.gnu.org/licenses/.


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

lenses-0.2.1.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lenses-0.2.1-py2.py3-none-any.whl (36.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file lenses-0.2.1.tar.gz.

File metadata

  • Download URL: lenses-0.2.1.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lenses-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2810bdb9835948b6c10df12fbc2b99a932205024a79381c7172a66597368e859
MD5 10a45ef1e37fe3094b52895e1e0a1e8b
BLAKE2b-256 6ae50aa9d933a238a1a74b6c72e64c9c10f38bb76d0883e0afa424ea9ab22962

See more details on using hashes here.

File details

Details for the file lenses-0.2.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for lenses-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0fe298ac1f51d406eaa69be424c7e263bc8bf99d30fe9fc22b4599af6a04e6a7
MD5 ac289c648a883995af9ec11db17ff8a8
BLAKE2b-256 5dc1dedb5960390918de4fc7aeda66796c9298babe6a5d4be851b992b0b9952c

See more details on using hashes here.

Supported by

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