more conversion

This commit is contained in:
aria1th
2023-12-11 03:52:17 +09:00
parent 60319133e6
commit fbcf2e8bba
3 changed files with 94 additions and 2 deletions
+24
View File
@@ -21,3 +21,27 @@ def validate(container):
for attr in ["FUNCTION", "INPUT_TYPES", "RETURN_TYPES", "CATEGORY"]:
if not hasattr(cls, attr):
raise Exception("Class {} doesn't have attribute {}".format(cls.__name__, attr))
class AllTrue(str):
def __init__(self, representation=None) -> None:
self.repr = representation
pass
def __ne__(self, __value: object) -> bool:
return False
# isinstance, jsonserializable hijack
def __instancecheck__(self, instance):
return True
def __subclasscheck__(self, subclass):
return True
def __bool__(self):
return True
def __str__(self):
return self.repr
# jsonserializable hijack
def __jsonencode__(self):
return self.repr
def __repr__(self) -> str:
return self.repr
def __eq__(self, __value: object) -> bool:
return True
anytype = AllTrue("variable") # when a != b is called, it will always return False
+44 -1
View File
@@ -2,7 +2,7 @@
Converts int / float / boolean
Note that it depends on the order of the conversion
"""
from .autonode import validate, node_wrapper, get_node_names_mappings
from .autonode import validate, node_wrapper, get_node_names_mappings, anytype
classes = []
node = node_wrapper(classes)
@@ -35,6 +35,49 @@ def create_class(type_from, type_to):
node(CustomClass)
return CustomClass
@node
class StringListToCombo:
"""
Converts raw string, separates with separator, then return as raw list
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"string": ("STRING", {"default": ""}),
"separator": ("STRING", {"default": "$"}),
}
}
FUNCTION = "stringListToCombo"
CATEGORY = "Logic Gates"
custom_name = "String List to Combo"
def stringListToCombo(self, string, separator):
return (string.split(separator),)
@node
class ConvertComboToString:
"""
Converts raw list to string, separated with separator
"""
RETURN_TYPES = ("STRING",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"combo": (anytype, {"default": []}),
"separator": ("STRING", {"default": "$"}),
}
}
FUNCTION = "convertComboToString"
CATEGORY = "Logic Gates"
custom_name = "Convert Combo to String"
def convertComboToString(self, combo, separator):
if isinstance(combo, (str, float, int, bool)):
return (combo,)
return (separator.join(combo),)
for type_from in conversion_operators:
for type_to in conversion_operators:
create_class(type_from, type_to)
+26 -1
View File
@@ -2,7 +2,7 @@
Implements logic gate nodes
"""
import re
from .autonode import node_wrapper, get_node_names_mappings, validate
from .autonode import node_wrapper, get_node_names_mappings, validate, anytype
classes = []
@@ -469,6 +469,31 @@ class MergeString:
custom_name = "Merge String"
def merge(self, input1, input2):
return (input1 + input2,)
@node
class MemoryNode:
"""
Stores a value in memory.
Flip-flop behaviour.
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype, {"default": ""}),
"flag": (anytype, {"default": 0}),
}
}
FUNCTION = "memory"
CATEGORY = "Logic Gates"
custom_name = "Memory String"
def memory(self, input1, flag):
if flag:
self.memory_value = input1
return (self.memory_value,)
CLASS_MAPPINGS, CLASS_NAMES = get_node_names_mappings(classes)
validate(classes)