Skip to main content

No project description provided

Project description

lupl 👾😺

tests Coverage Status PyPI version License: GPL v3 Ruff uv

A collection of potentially generally useful Python utilities.

Installation

lupl is a PEP-621-compliant package and available on PyPI.

Usage

ComposeRouter

The ComposeRouter class allows to route attributes access for registered methods through a functional pipeline constructed from components. The pipeline is only triggered if a registered method is accessed via the ComposeRouter namespace.

from lupl import ComposeRouter

class Foo:
	route = ComposeRouter(lambda x: x + 1, lambda y: y * 2)

	@route.register
	def method(self, x, y):
		return x * y

	foo = Foo()

print(foo.method(2, 3))           # 6
print(foo.route.method(2, 3))     # 13

By default, composition in ComposeRouter is right-associative.

Associativity can be controlled by setting the left_associative: bool kwarg either when creating the ComposeRouter instance or when calling it.

class Bar:
	route = ComposeRouter(lambda x: x + 1, lambda y: y * 2, left_associative=True)

	@route.register
	def method(self, x, y):
		return x * y

bar = Bar()

print(bar.method(2, 3))  # 6
print(bar.route.method(2, 3))  # 14
print(bar.route(left_associative=False).method(2, 3))  # 13

Chunk Iterator

The ichunk generator implements a simple chunk iterator that allows to lazily slice an Iterator into sub-iterators.

from collections.abc import Iterator
from lupl import ichunk

iterator: Iterator[int] = iter(range(10))
chunks: Iterator[Iterator[int]] = ichunk(iterator, size=3)

materialized = [tuple(chunk) for chunk in chunks]
print(materialized)  # [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]

Pydantic Tools

CurryModel

The CurryModel constructor allows to sequentially initialize (curry) a Pydantic model.

from lupl import CurryModel

class MyModel(BaseModel):
	x: str
	y: int
	z: tuple[str, int]


curried_model = CurryModel(MyModel)

curried_model(x="1")
curried_model(y=2)

model_instance = curried_model(z=("3", 4))
print(model_instance)

CurryModel instances are recursive so it is also possible to do this:

curried_model_2 = CurryModel(MyModel)
model_instance_2 = curried_model_2(x="1")(y=2)(z=("3", 4))
print(model_instance_2)

Currying turns a function of arity n into at most n functions of arity 1 and at least 1 function of arity n (and everything in between), so you can also do e.g. this:

curried_model_3 = CurryModel(MyModel)
model_instance_3 = curried_model_3(x="1", y=2)(z=("3", 4))
print(model_instance_3)

init_model_from_kwargs

The init_model_from_kwargs constructor allows to initialize (potentially nested) models from (flat) kwargs.

class SimpleModel(BaseModel):
	x: int
	y: int = 3


class NestedModel(BaseModel):
	a: str
	b: SimpleModel


class ComplexModel(BaseModel):
	p: str
	q: NestedModel


# p='p value' q=NestedModel(a='a value', b=SimpleModel(x=1, y=2))
model_instance_1 = init_model_from_kwargs(
	ComplexModel, x=1, y=2, a="a value", p="p value"
)

# p='p value' q=NestedModel(a='a value', b=SimpleModel(x=1, y=3))
model_instance_2 = init_model_from_kwargs(
	ComplexModel, p="p value", q=NestedModel(a="a value", b=SimpleModel(x=1))
)

# p='p value' q=NestedModel(a='a value', b=SimpleModel(x=1, y=3))
model_instance_3 = init_model_from_kwargs(
	ComplexModel, p="p value", q=init_model_from_kwargs(NestedModel, a="a value", x=1)
)

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

lupl-0.1.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

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

lupl-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lupl-0.1.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.22

File hashes

Hashes for lupl-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ecdf0ba8a42c96409d880c6dc87bd58bc3346ce0a10177d95881ecc5f5a41b97
MD5 b8a89faad79e8ee7c4fda8ad7ba22199
BLAKE2b-256 958f811ec66337bc6589306636bcf9935b0a879876e3f74369a8c46863912e4f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lupl-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.22

File hashes

Hashes for lupl-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e85c78e9032b97f793e89fea35b36c410d92bbe3803e02de20770291b4cdd37f
MD5 f39b8932e50c529e2027da4cac274854
BLAKE2b-256 94ddd8294e3ed85099ad9bfe3edbf07d0dab300e0e47eddffc24d3f039d8865e

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