Skip to main content

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.

Install

Install by running pip install scope-py or by cloning this repo and running pip install . in the project root directory.

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:

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 usage:

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))

Create a custom attribute lens:

# 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 usage:

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

Todo‘s

  • [] Clean up unused files leftover from cloned template repo
  • [] Tests to achieve 100% code coverage
  • [] Add a filter method to all classes which accepts a predicate function and applies it to the focus
  • [] Add tests passing and code coverage badges using Github Pages

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


Download files

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

Source Distribution

scope_py-0.1.1.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

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

scope_py-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file scope_py-0.1.1.tar.gz.

File metadata

  • Download URL: scope_py-0.1.1.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for scope_py-0.1.1.tar.gz
Algorithm Hash digest
SHA256 df0e35189bd8ba70b3c4e71ffa6c2e3870900f10ca2ecf3238b069017b1c631a
MD5 44065ba63be9989e1dc0b113ef5b904a
BLAKE2b-256 07d85d29a75f1442939dd03f7d3dee6b644f10e8e4e1ff791c73db50a0d0c3c7

See more details on using hashes here.

File details

Details for the file scope_py-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: scope_py-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for scope_py-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d7692c8b9708de1917cc104901b7bda149d406e64a259bcf5e65b054dc96de25
MD5 3205d43c5184d7139d1681160dc1d2cf
BLAKE2b-256 23013df2f0d0ce5ccddc4432bae4d9915cbe13c170320500b152e70c2758ed1a

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