From 15469d928fd7cd64ee6dff622a443d0f3c81aaed Mon Sep 17 00:00:00 2001 From: "Lex Darlog (DRL)" <3897975+Lex-DRL@users.noreply.github.com> Date: Sat, 2 Aug 2025 15:29:08 +0500 Subject: [PATCH] `StringConstructorValidateKeys` --- CHANGELOG.md | 1 + README.md | 1 + __init__.py | 3 ++ node_dict_add_any.py | 2 +- node_dict_add_string.py | 2 +- node_dict_key_extract.py | 74 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 node_dict_key_extract.py diff --git a/CHANGELOG.md b/CHANGELOG.md index baea32d..200d1fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ No other features planned. To do some fancy stuff with dictionaries, specialized - All the secondary dict-related nodes are moved to a subcategory. - Default node name shortened: `Format-Dict` → just `Dict`. +- New node: `Extract String from Dict` - to be actually self-sufficient for the common use cases. # v1.0.3 diff --git a/README.md b/README.md index cbf7c43..eecdf92 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ The pack provides some utility nodes to build such dict: - `Add String to Dict` - similar, but adds only one entry. Useful when you need a value or a key of the dictionary entry to come as input connection from somewhere else. - `Add ANY to Dict` similar, but for advanced formatting. It allows you to add not only a string, but literally anything (float, int, etc). The key still must follow the same restrictions. - Any of these nodes can take another dictionary as input - then they extend/update it. +- `Extract String from Dict` - the opposite: extract a single string from the dict. With it, you can extract a single element to modify it - and put an updated version back to the dict. Technically, the same could be accomplished with the main `String Formatter` node, but this one is more compact. - `Validate Dict` - a node that ensures that all the keys in the dictionary are named properly. Useful if you build the dictionary with nodes from other packs (see below) and want to ensure that everything is fine - before passing the dictionary down the line. - You can design your dict to be intended for updating down the line. Change some keys → get a different prompt with the same template (for example, a more detailed description of a character). - Don't forget that with recursive formatting, template itself could be a part of the dictionary, too! diff --git a/__init__.py b/__init__.py index 0f78e1b..5fb9844 100644 --- a/__init__.py +++ b/__init__.py @@ -6,6 +6,7 @@ import typing as _t from .node_dict_add_any import StringConstructorDictAddAny from .node_dict_add_string import StringConstructorDictAddString +from .node_dict_key_extract import StringConstructorDictExtractString from .node_dict_from_text import StringConstructorDictFromText from .node_dict_preview import StringConstructorDictPreview from .node_formatter import StringConstructorFormatter @@ -14,6 +15,7 @@ from .node_validate_keys import StringConstructorValidateKeys NODE_CLASS_MAPPINGS: _t.Dict[str, type] = { 'StringConstructorDictAddAny': StringConstructorDictAddAny, 'StringConstructorDictAddString': StringConstructorDictAddString, + 'StringConstructorDictExtractString': StringConstructorDictExtractString, 'StringConstructorDictFromText': StringConstructorDictFromText, 'StringConstructorDictPreview': StringConstructorDictPreview, 'StringConstructorFormatter': StringConstructorFormatter, @@ -22,6 +24,7 @@ NODE_CLASS_MAPPINGS: _t.Dict[str, type] = { NODE_DISPLAY_NAME_MAPPINGS: _t.Dict[str, str] = { 'StringConstructorDictAddAny': "Add ANY to Dict", 'StringConstructorDictAddString': "Add String to Dict", + 'StringConstructorDictExtractString': "Extract String from Dict", 'StringConstructorDictFromText': "Dict from Text", 'StringConstructorDictPreview': "Preview Dict", 'StringConstructorFormatter': "String Formatter", diff --git a/node_dict_add_any.py b/node_dict_add_any.py index 68494a7..61fbd2f 100644 --- a/node_dict_add_any.py +++ b/node_dict_add_any.py @@ -23,7 +23,7 @@ _dict = dict _input_types = _deepfreeze({ 'required': { 'name': (_IO.STRING, {'tooltip': ( - "Name of the non-string item inserted into the dict. " + "Name (key) of the non-string item inserted into the dict. " "It must comprise only of latin letters, digits and underscores + it can't start with a digit." )}), }, diff --git a/node_dict_add_string.py b/node_dict_add_string.py index 6779a84..0cf2fbd 100644 --- a/node_dict_add_string.py +++ b/node_dict_add_string.py @@ -20,7 +20,7 @@ from .funcs_common import _new_dict_with_updated_key, _T _input_types = _deepfreeze({ 'required': { 'name': (_IO.STRING, {'tooltip': ( - "Name of the substring inserted into the dict. " + "Name (key) of the substring inserted into the dict. " "It must comprise only of latin letters, digits and underscores + it can't start with a digit." )}), 'cleanup': (_IO.BOOLEAN, {'default': True, 'label_on': 'leading/trailing spaces', 'label_off': 'no', 'tooltip': ( diff --git a/node_dict_key_extract.py b/node_dict_key_extract.py new file mode 100644 index 0000000..2e099c9 --- /dev/null +++ b/node_dict_key_extract.py @@ -0,0 +1,74 @@ +# encoding: utf-8 +""" +""" + +import typing as _t + +from inspect import cleandoc as _cleandoc + +from frozendict import deepfreeze as _deepfreeze + +from comfy.comfy_types.node_typing import IO as _IO + +from . import _meta +from .docstring_formatter import format_docstring as _format_docstring +from .enums import DataTypes as _DataTypes +from .funcs_common import _show_text_on_node, _new_dict_with_updated_key, _T + + +_input_types = _deepfreeze({ + 'required': { + 'name': (_IO.STRING, {'tooltip': ( + "Name (key) of a string to extract from dictionary.\n" + "If the element with such key isn't a string, it will be turned to one.\n" + "If no such key exists in the dict, an empty string returned." + )}), + 'show_status': (_IO.BOOLEAN, {'default': False, 'label_on': 'value', 'label_off': 'no', 'tooltip': ( + "Show the extracted string on the node itself?" + )}), + }, + 'optional': { + 'dict': _DataTypes.input_dict(tooltip="The dictionary to extract the element from."), + }, + 'hidden': { + 'unique_id': 'UNIQUE_ID', # used for text display at the bottom of the node + }, +}) + + +class StringConstructorDictExtractString: + """Extract a single string from the Format-Dict.""" + NODE_NAME = 'StringConstructorDictExtractString' + CATEGORY = _meta.category_dict + DESCRIPTION = _format_docstring(_cleandoc(__doc__)) + + FUNCTION = 'main' + RETURN_TYPES = (_IO.STRING, ) + RETURN_NAMES = ('string', ) + # OUTPUT_TOOLTIPS = tuple() + + @classmethod + def INPUT_TYPES(cls): + return _input_types + + @staticmethod + def main( + name: str, show_status: bool = False, dict: _t.Dict[str, _T] = None, + unique_id: str = None, + ) -> _t.Tuple[str]: + """Extract a single string from the Format-Dict.""" + # noinspection PyBroadException + try: + string: str = dict.get(name, '') + except Exception: + string = '' + if string is None: + string = '' + + if not isinstance(string, str): + string = repr(string) + + if show_status and unique_id: + _show_text_on_node(string, unique_id) + + return (string, )