Skip to content

lu-pl/upto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UPTo

tests coverage PyPI version License: GPL v3 Ruff

UPTo - A personal collection of potentially generally Useful Python Tools.

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

Pydantic Tools

CurryModel

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

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages