A lens library which targets usability over rigor.
Project description
Scope
Scope is a Python library inspired by optics from functional programming. The aim of Scope is to make various flavors of optics natural tools for Python programmers without requiring knowledge of the underlying math. Put simply, optics provide an abstraction for focusing on ("getting") and manipulating ("setting") data within larger and potentially nested structures.
This library favors usability over rigor and makes some decisions which increase utility at the expense of overloading and extending established terminology. For example, traversals in the standard usage, provide a means of focusing on multiple parts of a datastructure but do not provide similar setting behavior, whereas instances of the scope.Traversal class do provide a way to set multiple parts of a larger datastructure.
Introduction
The Scope library targets manipulation of JSON-like nested structures, which in Python are built from dictionaries, and lists, but custom optics are definable. Scope has a few classes you should be aware of;
Lens: Base class providing get and put methods. Can be composed with other lenses.ParallelLens: Allows multiple lenses to be used in parallel.Traversal: Provides functionality for working with sequences of data.ItemLens: A lens focused on a specific item in an object which implements the__getitem__dunder method.CRUDLens: An extension of a Lens that includes create and delete methods.ParallelCRUDLens: Allows multiple CRUD lenses to be used in parallel.CRUDTraversal: Extension of the CRUDLens for working with sequences of data.
Importantly, a Lens focuses on one thing and a Traversals focuses on many things. A CRUDLens extends the get/put functionality of lenses with additional create/delete functionality.
Usage
Basic lens composition example:
import scope
# create a lens that focuses on item 'b' after item 'a'
lens = scope.id['a']['b']
# lens = scope.compose_lenses(scope.id['a'], scope.id['b']) <-- alt. syntax
data = {'a': {'b': 3}}
# note -- lens(data) is an alias for lens.get(data)
assert lens(data) == 3
assert lens.put(4)(data) == {'a': {'b': 4}}
CRUDTraversal example:
lens = CRUDTraversal(ItemCRUDLens('a'), ItemCRUDLens('c'))
# lens = ItemCRUDLens('a')[::]['c'] <-- alt. syntax
data = {'a': [{'b': 1}, {'b': 3}]}
# "{'a': [{'b': 1, 'c': 2}, {'b': 3, 'c': 4}]}"
print(lens.create([2, 4])(data))
Custom attribute lens example:
# create a lens that gets/sets the attribute "my_attr" on an object
my_attr_lens = scope.Lens(lambda data: getattr(data, 'my_attr'), lambda value: lambda data: setattr(data, 'my_attr', value))
Parallel lens example:
x_lens, y_lens = scope.id['x'], scope.id['y']
# create a lens which focuses on 'x' and 'y' in parallel
parallel_lens = x_lens | y_lens
# parallel_lens = scope.ParallelLens(x_lens, y_lens) <-- alt. syntax
data = {'x': 2, 'y': 3}
assert sum(parallel_lens(data)) == 5
License
This project is open-sourced under the MIT license. See LICENSE for more information.
Contact
For questions or issues, please open an issue on GitHub.
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 Distribution
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 scope_py-0.1.0.tar.gz.
File metadata
- Download URL: scope_py-0.1.0.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
406b9aad824784e6cb207aec2a37ff82467d092ae176ce1852609435ba01c609
|
|
| MD5 |
d958584e075f67ac5d20c712d06adf42
|
|
| BLAKE2b-256 |
9073ebbeea820ee0242ea30d57839ca66c9fcc23758f2c6f28788353c3725aaf
|
File details
Details for the file scope_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: scope_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d9ac518b7b2204981038b6987e2b6d6290cf6295672e8789850a9a25f42f6e9
|
|
| MD5 |
ca201c5fa7edb7b4ea727b1e7498da17
|
|
| BLAKE2b-256 |
af66fc46fef9c316a92520d5f824446ea1dd1f6a61ed288831c354fb85717058
|