No project description provided
Project description
lupl
lupl - A personal collection of potentially generally useful Python utilities.
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
Chunk Iterator
The lupl.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
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 lupl-0.1.0.tar.gz.
File metadata
- Download URL: lupl-0.1.0.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
399958ce818cb368e57d4c3a88cb72cdec60826bdebf79374b1485fb65545345
|
|
| MD5 |
b6601776c4b2ce83ab781772bd8bdf59
|
|
| BLAKE2b-256 |
681aaffe48e07e6558e15c65e39834fafd230d4761b2365b13495b69b2a03729
|
File details
Details for the file lupl-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lupl-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17d94c1500e571bc152d8275dfc444078c7545ac5cb65356b21cd6b5c4c53f4a
|
|
| MD5 |
5537b4df9cfde3964683f6f891d8071c
|
|
| BLAKE2b-256 |
833918349a56efdf3d709b5e82d254063f5d0fb064aa678ad0a7b76f8f1df299
|