implement first nodes.

This commit is contained in:
Wutipong Wongsakuldej
2023-11-21 02:25:34 +07:00
parent 086003035d
commit 00c0d30f58
4 changed files with 47 additions and 32 deletions
+5 -4
View File
@@ -1,5 +1,6 @@
from create_n_token_node import CreateNTokenStringNode
from .create_n_token_node import CreateNTokenStringNode
NODE_CLASS_MAPPINGS = { "wutipong/comfyui-textutils/create-n-token-string" : CreateNTokenStringNode }
NODE_DISPLAY_NAME_MAPPINGS = { "wutipong/comfyui-textutils/create-n-token-string" : "Create N-Token String" }
NODE_CLASS_MAPPINGS = {
"wutipong/comfyui-textutils/create-n-token-string": CreateNTokenStringNode}
NODE_DISPLAY_NAME_MAPPINGS = {
"wutipong/comfyui-textutils/create-n-token-string": "Create N-Token String"}
+10 -10
View File
@@ -1,21 +1,21 @@
import textutils
from .textutils import create_n_token_string
class CreateNTokenStringNode:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text" : ("TEXT", ""),
"delim": ("TEXT", ""),
"n": ("INT", 0)
"required": {
"text": ("STRING", {"default": ""}),
"separator": ("STRING", {"default": "/"}),
"n": ("INT", {"default": 0, "min": -100, "max": 100, "step": 1})
},
}
RETURN_TYPES = ("Text",)
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("TEXT",)
FUNCTION = "create_n_token_string"
CATEGORY = "wutipong"
FUNCTION = "perform_create_n_token_string"
CATEGORY = "text utility"
def create_n_token_string(self, text, delim, n):
return (textutils.create_n_token_string(text, delim, n),)
def perform_create_n_token_string(self, text, delim, n):
return (create_n_token_string(text, delim, n),)
+27 -14
View File
@@ -1,29 +1,42 @@
import textutils
import unittest
class TestTextUtils(unittest.TestCase):
def test_positive_tokens(self):
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", 0), "")
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", 1), "hello")
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", 2), "hello/world")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", 0), "")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", 1), "hello")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", 2), "hello/world")
def test_negative_tokens(self):
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", -1), "doing")
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", -2), "you/doing")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", -1), "doing")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", -2), "you/doing")
def test_n_exceed_length(self):
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", 8), "hello/world/how/are/you/doing")
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing", "/", -6), "hello/world/how/are/you/doing")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", 8), "hello/world/how/are/you/doing")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing", "/", -6), "hello/world/how/are/you/doing")
def test_delim_at_the_begining(self):
self.assertEqual(textutils.create_n_token_string("/hello/world/how/are/you/doing/", "/", 0), "")
self.assertEqual(textutils.create_n_token_string("/hello/world/how/are/you/doing/", "/", 1), "")
self.assertEqual(textutils.create_n_token_string("/hello/world/how/are/you/doing/", "/", 2), "/hello")
self.assertEqual(textutils.create_n_token_string(
"/hello/world/how/are/you/doing/", "/", 0), "")
self.assertEqual(textutils.create_n_token_string(
"/hello/world/how/are/you/doing/", "/", 1), "")
self.assertEqual(textutils.create_n_token_string(
"/hello/world/how/are/you/doing/", "/", 2), "/hello")
def test_delim_at_the_end(self):
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing/", "/", -1), "")
self.assertEqual(textutils.create_n_token_string("hello/world/how/are/you/doing/", "/", -2), "doing/")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing/", "/", -1), "")
self.assertEqual(textutils.create_n_token_string(
"hello/world/how/are/you/doing/", "/", -2), "doing/")
if __name__ == '__main__':
unittest.main()
unittest.main()
+5 -4
View File
@@ -1,11 +1,12 @@
def create_n_token_string(str, delim, n):
parts = str.split(delim)
if n > 0 :
if n > 0:
return delim.join(parts[0: n])
else :
else:
l = len(parts)
start = l + n
if start < 0: start = 0
if start < 0:
start = 0
return delim.join(parts[start: l])
return delim.join(parts[start: l])