Hello Rasa,
The following should explain what is going on with this Python snippet.
We first import the re (RegularExpression) module. This is one of only a couple that are allowed to be imported without pointing to the particular .dll file.
import re
We then create a named variable for our input port, at the toggled button of ‘IN[0]’. We do not need to unwrap if the inputs unless they are Revit Elements.
input = IN[0]
We want to check if the input is a single item, or a list. To do so, we can use the internal Python commands of ‘hassattr’ (Has Attribute) and the call of ‘_ iter _’, which is simply a true or false statement on whether or not the element in question (i.e our List) is iterable (Which means taking each item of something, one after another to subsequently do something to). It will then ask if it’s NOT iterable (Using logical operators of ‘if’ and ‘not’). This means, simply put, that if the input list is iterable - it’s a list, and if it’s not, it’s a single item. In the case of it being a single item, we simply want to put the item into a list (Of only itself) by calling a new return value for ‘input’. In python, to create a list, we use square brackets [ ] and here we wrap our input with them in the case of a single item.
if not hasattr(input, '__iter__'):
input = [input]
We then want run a list comprehension over the OUT port. Our comprehension uses the re module, and calls the ‘sub’ method - which allows us to replace sub-string elements. In this case, the information supplied to the RegularExpression is taking all instances of numbers and replacing them with empty spaces.
Our regular expression search key uses \d which stipulates match any data that is a number followed by a + key that means the regular expression will match one or more repetitions of the proceeding RE. It calls that for every single item in the input list, demarcate as ‘string’.
A list comprehension allows us to neatly wrap up loops into a single line by calling the following: [ item.doingSomething for item in inputList ], where item is our variable and doingSomething is our check - in this case our Regular Expression and it auto outputs a list (Meaning you don’t have to define an empty catchment list earlier).
Our list comprehension is as follows: [ callREModuleEnttitledSub(ReplaceAllNumbersWithAnEmptyStringOnVariableCalledString) for everySingleItem in ourList ]
OUT = [re.sub('\d+', ' ', string) for string in input]
Resources:
Has Attribute function
__ iter __ property
RegularExpression Python Module