Create data using scripted Python.
You can use this node to create data from a Python script when you want to perform an operation that does not require an input.
This example generates random age, sex and name data.
The new fields and the number of records to output are defined in the ConfigureFields property.
The CreateRecords property defines the details:
- The sex can be either 'Male' or 'Female'.
- The age can be between 0 and 100.
- The name consists of a capital letter, which can be followed by a vowel and then up to seven other random letters.
Enter the following script in the ConfigureFields property:
import random import stringnumRecords = 23out1.sex = strout1.age = intout1.name = strvowels = 'aeiou'
Enter the following script in the CreateRecords property:
for i in range(numRecords): myBool = random.randint(0,1) if myBool == 1: out1.sex = "Male" else: out1.sex = "Female" out1.age = random.randint(0,100) myName = "" nameLength = random.randint(0,9) for j in range(nameLength): if j == 0: letters = string.ascii_uppercase elif j ==1: letters = vowels else: letters = string.ascii_lowercase myName = myName + random.choice(letters) out1.name = myName node.write(0,out1)
StringIOobjects from the Python
StringIOmodule for improved performance. For more information, see the Python documentation at https://docs.python.org/2/library/stringio.html.
For information on importing pure Python modules to use in the Generate Data node, see importing pure Python modules.
Specify the Python script to configure the node outputs and output fields.
This script is executed once in order to configure the output metadata.
Specify the Python script to write output records.
Optionally specify whether or not the output records will be automatically written by the node after processing the CreateRecords script. Choose from:
- Always - Unless an output record has been explicitly written within the Python script, or has been explicitly set to None, it will always be written.
- Never - Output records will never be automatically written. They need to be explicitly written within the CreateRecords script.
- When Modified - Output records will be automatically written whenever something (including None) has been set on the output record, and the output has not been set to None, and the record has not been explicitly written.
The default value is When Modified.
Consider the following CreateRecords script:
if in1.Status != 'Inactive': out1 += in1
With ImplicitWriteMode set to When Modified, this record will only be written to the output if the 'Status' field value is not 'Inactive'.
If ImplicitWriteMode is set to Always, the record would be written to the output with the script above. To prevent this, you would use the following script:
if in1.Status != 'Inactive': out1 += in1else out1 += None
If ImplicitWriteMode is set to Never, then you would need to provide additional instructions to write the record to the output upon setting the value:
if in1.Status != 'Inactive': out1 += in1 node.write(0, out1)
Inputs and outputs