Feat: move to models and add consume_flux

This commit is contained in:
Bertrand Benjamin 2024-11-06 07:00:15 +01:00
parent 07fb92e2fa
commit 9d45625a5e
7 changed files with 72 additions and 0 deletions

View File

View File

@ -0,0 +1,8 @@
from plesna.models.flux import Flux, FluxMetaData
def consume_flux(flux: Flux) -> FluxMetaData:
metadata = flux.transformation.function(
sources=flux.sources, targets=flux.targets, **flux.transformation.extra_kwrds
)
return FluxMetaData(data=metadata)

View File

14
plesna/models/flux.py Normal file
View File

@ -0,0 +1,14 @@
from pydantic import BaseModel
from plesna.models.storage import Table
from plesna.models.transformation import Transformation
class Flux(BaseModel):
sources: dict[str, Table]
targets: dict[str, Table]
transformation: Transformation
class FluxMetaData(BaseModel):
data: dict

View File

@ -0,0 +1,15 @@
from collections.abc import Callable
from pydantic import BaseModel
class Transformation(BaseModel):
"""
The function have to have at least 2 arguments: sources and targets
Other arguments will came throught extra_kwrds
The function will have to return metadata as dict
"""
function: Callable
extra_kwrds: dict = {}

View File

View File

@ -0,0 +1,35 @@
from plesna.compute.consume_flux import consume_flux
from plesna.models.flux import Flux
from plesna.models.storage import Table
from plesna.models.transformation import Transformation
def test_consume_flux():
sources = {
"src1": Table(id="src1", value="here"),
"src2": Table(id="src2", value="here"),
}
targets = {
"tgt1": Table(id="tgt1", value="this"),
"tgt2": Table(id="tgt2", value="that"),
}
def func(sources, targets, **kwrds):
return {
"sources": len(sources),
"targets": len(targets),
"kwrds": len(kwrds),
}
flux = Flux(
sources=sources,
targets=targets,
transformation=Transformation(function=func, extra_kwrds={"extra": "super"}),
)
meta = consume_flux(flux)
assert meta.data == {
"sources": 2,
"targets": 2,
"kwrds": 1,
}