Jump to content
Developer Wiki and Function Reference Links ×

variable casting for inputs


har

Recommended Posts

Hi all,

not sure if this belongs here or on the Vectorscript board...

I'm pretty new to Marionette and Vectorscript and have only limited experience with Python in general - Please be very slow with me ;)

My task:

Inputs are an array of strings and a string. I want to output an array of all indices in the array that match the input string.

My code:

@Marionette.NodeDefinition
class Params(metaclass = Marionette.OrderedClass):
this = Marionette.Node( 'Indices' )
this.SetDescription ('Find all Indices in List that equal Item')
this.SetListAbsorb()
list = Marionette.PortIn( [] )
list.SetDescription( "The input list" )
item = Marionette.PortIn( None )
item.SetDescription( "The item to be searched" )
outlist = Marionette.PortOut()   
outlist.SetDescription( "The result index list" )

def RunNode(self):
item = self.Params.item.value
lst = self.Params.list.value

outlist = [i for i, x in enumerate(lst) if x == item]

self.Params.outlist.value = outlist

My problem:

the item value seems to be casted to a "wrong" type, resulting in no output. converting it with "str()" won't help either.

As soon as I hardcode item to some string, the function will work as expected.

What am I doing wrong here?

Thanks a lot in advance for any answer!

Link to comment

Hi Patrick,

thanks a lot, that works for me.

Also, greetings from Austria :)

I deleted the GetItem node as I couldn't see any purpose and the network still works.

Was there a specific reason for it?

Aside from that, I'd still be interested why my script wouldn't work.

Can anyone give some insight here?

Link to comment

You are right, GetItem is needless. I just didn't notice in the hurry...

Finding the bug in your code was a real brainteaser:

SetListAbsorb() will convert item 'A' for example to a list: ['A']

You can solve this by declaring list as PortInLits:

@Marionette.NodeDefinition
class Params(metaclass = Marionette.OrderedClass):
this = Marionette.Node( 'Indices' )
this.SetDescription ('Find all Indices in List that equal Item')
#this.SetListAbsorb()
list = Marionette.PortInLists ( [] )
list.SetDescription( "The input list" )
item = Marionette.PortIn( None )
item.SetDescription( "The item to be searched" )
outlist = Marionette.PortOut()   
outlist.SetDescription( "The result index list" )

def RunNode(self):
item = self.Params.item.value
lst = self.Params.list.value

print (type (item))

outlist = [i for i, x in enumerate(lst) if x == item ]

self.Params.outlist.value = outlist

Edited by Patrick Winkler
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...