StringConstructorValidateKeys

This commit is contained in:
Lex Darlog (DRL)
2025-08-02 15:41:02 +05:00
parent 475a2e2ca7
commit 15469d928f
6 changed files with 81 additions and 2 deletions
+1
View File
@@ -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
+1
View File
@@ -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!
+3
View File
@@ -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",
+1 -1
View File
@@ -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."
)}),
},
+1 -1
View File
@@ -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': (
+74
View File
@@ -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, )