Skip to main content

Tiny Lens (AKA 'lentezinha' in pt-BR) for nested dicts in Python

Project description

Lentezinha

Tiny Lens for Python: a single 30 KiB module to improve readability. "Lentezinha" means "little lens" in Brazilian Portuguese.

Motivation

I don't like to do this: user.get("profile", {}).get("age")), where user is a nested dictionary. I'd rather say user.get("profile.age").

Usage

user = {"name": "Ana", "profile": {"age": 20}}

from lentezinha import read
read(user, "profile.age")  # == 20

from lentezinha import update
update(user, "profile.age", lambda a: a + 1)  # Updates age to 21

You can use a Lens instance if you plan to access nested attributes many times:

from lentezinha import Lens
age = Lens("profile.age")

print(age.get(user))  # == 20

age.set(user, 27)  # == 27

age.set(user, lambda a: a + 1)  # == 28

It also works with Pydantic:

from pydantic import BaseModel

class Profile(BaseModel):
    age: int

class User(BaseModel):
    profile: Profile

user = User(profile=Profile(age=20))

age.get(user)  # == 20
age.set(user, 27)  # == 27

Installation

pip install lentezinha

TODO

  • Compose lenses

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

lentezinha-0.1.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

lentezinha-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file lentezinha-0.1.0.tar.gz.

File metadata

  • Download URL: lentezinha-0.1.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for lentezinha-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d63d4017c6f779c6ac6df65364053bb525b7d10726dbbe2bc13a01d1ac56a8c3
MD5 1a7a322817f617c2d4a6981a5ae99cdb
BLAKE2b-256 c2da88ca6e7b005e915400fc606acf0dced68996cb312b700dfbf85f03d1894c

See more details on using hashes here.

File details

Details for the file lentezinha-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lentezinha-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for lentezinha-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 691ddb8c7b36d6fac67bcd520167aee05656b6c9ad66a1e8727f5a7556d600f9
MD5 6d029aa4b8b85a027616f4b2899292ab
BLAKE2b-256 6b70ea0fe68af07522fa18a4449eab931703d2806d0e36fed52ac8c06b1e6615

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