You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@JNmpi and I had a super short chat after the pyiron meeting tomorrow, and he was saying he'd still like to see syntax like:
# wf.min_phase1.calc = Macro.create.atomistics.CalcStatic # Currently possiblewf.min_phase1.calc.replace_with.static() # TODO, or similar
After introducing replacement and the Node.replace_with(self, other: Node) method in #26, this should actually be super easy to implement! A sketch looks like this: replace the replace_with method on Node with a property returning an instance of the following class:
classReplacer:
def__init__(self, node: Node):
self.node=nodedef__call__(self, other: Node):
# This is just the current replacement code:ifself.parentisnotNone:
self.node.parent.replace(self, other)
else:
warnings.warn(f"Could not replace {self.node.label}, as it has no parent.")
Then a savvy developer can "power-up" their node with replacements they know to be compatible by overriding the replace_with property with an instance of a child of that class like:
classCalculatorReplacer(Replacer):
@propertydefminimize(self):
self(Workflow.create.atomistics.CalcMinimize)
@propertydefstatic(self):
self(Workflow.create.atomistics.CalcStatic)
# Etc.
If the replacement is invalid this will just fail the way replacement normally fails -- e.g. if you have a minimize node with pressure connected to something, replacing with static should fail. I might need to double-check the tests and failure cases here to be sure this is working as intended, but the idea is perfectly straightforward.
As described above, this only works for nodes which are defined by actually making subclasses. We could probably stretch it to work using the decorators by extending the signature of Node to include replacer: Optional[type[Replacer]] and accept that as a decorator argument. Still requires the dev to be sophisticated enough to define a child-class of Replacer, but at least the decorators could still be used something like:
@JNmpi and I had a super short chat after the pyiron meeting tomorrow, and he was saying he'd still like to see syntax like:
After introducing replacement and the
Node.replace_with(self, other: Node)
method in #26, this should actually be super easy to implement! A sketch looks like this: replace thereplace_with
method onNode
with a property returning an instance of the following class:Then a savvy developer can "power-up" their node with replacements they know to be compatible by overriding the
replace_with
property with an instance of a child of that class like:If the replacement is invalid this will just fail the way replacement normally fails -- e.g. if you have a minimize node with
pressure
connected to something, replacing withstatic
should fail. I might need to double-check the tests and failure cases here to be sure this is working as intended, but the idea is perfectly straightforward.As described above, this only works for nodes which are defined by actually making subclasses. We could probably stretch it to work using the decorators by extending the signature of
Node
to includereplacer: Optional[type[Replacer]]
and accept that as a decorator argument. Still requires the dev to be sophisticated enough to define a child-class ofReplacer
, but at least the decorators could still be used something like:A bit of leg-work to get the examples and tests all set up, but I'm pretty happy with this direction.
The text was updated successfully, but these errors were encountered: