Feat: move to models and add consume_flux
This commit is contained in:
parent
07fb92e2fa
commit
9d45625a5e
0
plesna/compute/__init__.py
Normal file
0
plesna/compute/__init__.py
Normal file
8
plesna/compute/consume_flux.py
Normal file
8
plesna/compute/consume_flux.py
Normal 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)
|
0
plesna/models/__init__.py
Normal file
0
plesna/models/__init__.py
Normal file
14
plesna/models/flux.py
Normal file
14
plesna/models/flux.py
Normal 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
|
15
plesna/models/transformation.py
Normal file
15
plesna/models/transformation.py
Normal 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 = {}
|
0
tests/compute/__init__.py
Normal file
0
tests/compute/__init__.py
Normal file
35
tests/compute/test_consume_flux.py
Normal file
35
tests/compute/test_consume_flux.py
Normal 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,
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user