From e4a3b2eec40b09a970d912def6cf0836750cfccf Mon Sep 17 00:00:00 2001 From: Michael Wooten Date: Fri, 22 Dec 2023 22:31:37 -0500 Subject: [PATCH] Renamed preserve to overwrite Renamed the `preserve` property of `VariableAssignmentCommand` to overwrite, changed its default to True, and reversed how it was used that existing values are now preserved when it is not set. --- .../commands/variable_commands.py | 2 +- src/dynamicprompts/parser/parse.py | 4 ++-- src/dynamicprompts/sampling_context.py | 2 +- tests/parser/test_parser.py | 17 +++++++---------- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/dynamicprompts/commands/variable_commands.py b/src/dynamicprompts/commands/variable_commands.py index 96498dc..d1440f2 100644 --- a/src/dynamicprompts/commands/variable_commands.py +++ b/src/dynamicprompts/commands/variable_commands.py @@ -10,7 +10,7 @@ class VariableAssignmentCommand(Command): name: str value: Command immediate: bool - preserve: bool = False + overwrite: bool = True sampling_method = None diff --git a/src/dynamicprompts/parser/parse.py b/src/dynamicprompts/parser/parse.py index 75af6cb..7e6a064 100644 --- a/src/dynamicprompts/parser/parse.py +++ b/src/dynamicprompts/parser/parse.py @@ -240,7 +240,7 @@ def _configure_variable_assignment( + OPT_WS + var_name("name") + OPT_WS - + pp.Opt(pp.Literal("?"))("preserve") + + pp.Opt(pp.Literal("?"))("preserve_existing_value") + pp.Literal("=") + pp.Opt(pp.Literal("!"))("immediate") + OPT_WS @@ -402,7 +402,7 @@ def _parse_variable_assignment_command( return VariableAssignmentCommand( name=parts["name"], value=parts["value"], - preserve=("preserve" in parts), + overwrite=("preserve_existing_value" not in parts), immediate=("immediate" in parts), ) diff --git a/src/dynamicprompts/sampling_context.py b/src/dynamicprompts/sampling_context.py index a062464..9503e08 100644 --- a/src/dynamicprompts/sampling_context.py +++ b/src/dynamicprompts/sampling_context.py @@ -162,7 +162,7 @@ def process_variable_assignment( self, command: VariableAssignmentCommand, ) -> Command: - if command.preserve and command.name in self.variables: + if not command.overwrite and command.name in self.variables: return self.variables[command.name] if command.immediate: if isinstance(command.value, LiteralCommand): diff --git a/tests/parser/test_parser.py b/tests/parser/test_parser.py index ff684f9..dbf6ebc 100644 --- a/tests/parser/test_parser.py +++ b/tests/parser/test_parser.py @@ -390,17 +390,14 @@ def test_alternative_wildcard_wrap(self, wildcard_wrap: str, template: str): assert variant.values[1].literal == "B" assert variant.values[2].wildcard == "some/wildcard" + @pytest.mark.parametrize("immediate", (False, True), ids=("delayed", "immediate")) @pytest.mark.parametrize( - ("immediate", "preserve"), - [ - (False, False), - (False, True), - (True, False), - (True, True), - ], + "overwrite", + (False, True), + ids=("preserve existing value", "overwrite existing value"), ) - def test_variable_commands(self, immediate: bool, preserve: bool): - op = "?" if preserve else "" + def test_variable_commands(self, immediate: bool, overwrite: bool): + op = "?" if not overwrite else "" op += "=!" if immediate else "=" sequence = cast( SequenceCommand, @@ -411,7 +408,7 @@ def test_variable_commands(self, immediate: bool, preserve: bool): assert isinstance(ass, VariableAssignmentCommand) assert ass.name == "animal" assert ass.value == LiteralCommand("cat") - assert ass.preserve == preserve + assert ass.overwrite == overwrite assert ass.immediate == immediate acc = sequence[2] assert isinstance(acc, VariableAccessCommand)