Skip to content

Commit

Permalink
Add components property
Browse files Browse the repository at this point in the history
  • Loading branch information
frthjf committed Jun 30, 2024
1 parent 6e7baf5 commit ad20c11
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/machinable/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def executions() -> ExecutionCollection:

return Execution

@property
def components(self) -> "ComponentCollection":
return ComponentCollection([self])

@property
def execution(self) -> "Execution":
from machinable.execution import Execution
Expand Down
29 changes: 21 additions & 8 deletions src/machinable/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import dill as pickle
from machinable import errors, schema
from machinable.collection import Collection, InterfaceCollection
from machinable.collection import (
Collection,
ComponentCollection,
InterfaceCollection,
)
from machinable.element import _CONNECTIONS as connected_elements
from machinable.element import Element, get_dump, get_lineage
from machinable.types import VersionType
Expand Down Expand Up @@ -289,17 +293,19 @@ def push_related(self, key: str, value: "Interface") -> None:
self.__related__[key] = value
self._relation_cache[key] = True

def is_staged(self):
def is_staged(self) -> bool:
return self.__model__.uuid[-12:] != "0" * 12

def stage(self):
def stage(self) -> Self:
self.__model__.context = context = self.compute_context()
self.__model__.uuid = update_uuid_payload(self.__model__.uuid, context)

# ensure that configuration and predicate has been computed
assert self.config is not None
self.__model__.predicate = self.compute_predicate()

return self

def is_committed(self) -> bool:
from machinable.index import Index

Expand Down Expand Up @@ -740,12 +746,19 @@ def save_file(self, filepath: Union[str, List[str]], data: Any) -> str:
def launch(self) -> Self:
...

def cached(self):
from machinable.execution import Execution
@property
def components(self) -> "ComponentCollection":
if "components" not in self._cache:
from machinable.execution import Execution

with Execution().deferred() as e:
self.launch()
self._cache["components"] = e.executables

with Execution().deferred() as e:
self.launch()
return e.executables.reduce(
return self._cache["components"]

def cached(self):
return self.components.reduce(
lambda result, x: result and x.cached(), True
)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,16 @@ def test(self):
p.__exit__()


def test_interface_components(tmp_storage):
class T(Interface):
def launch(self):
get("machinable.component").launch()

assert len(T().components) == 1
t = get("machinable.component")
assert t == t.components[0]


def test_interface_cachable(tmp_storage):
counts = {
"test": 0,
Expand Down

0 comments on commit ad20c11

Please sign in to comment.