The Python generator creates data using an IronPython script. Under Fill settings, you can set up basic and custom settings.
IronPython script text box: Specify your script in the text box.
You can use an IronPython script to generate data. The script must define a main() function that accepts a single config argument.
config is a dictionary that contains keys that you can use in your scripts.
You can specify values for the following keys:
config["column_type"] – The column data type.config["column_size"] – The column size.config["n_rows"] – The number of rows.config["seed"] – The current random seed.config["config_path"] – The path to the Meaningful Generators foldercolumn_name – The column name. Specifying the config argument before column_name is optional. You can reference column_name directly.The Python generator supports the usage of the RegexGenerator inner class. Therefore, you can use regular expressions within the Python generator.
You can create the RegexGenerator in three ways:
mygen = RegexGenerator(regular expression)
mygen = RegexGenerator(regular expression, is unique data, data length)
mygen = RegexGenerator(regular expression, is unique data, data length, seed)
Where:
regular expression: Define an actual regular expression, for example, "[0-9A-Z]+”.is unique data: Define whether to generate unique data. Available parameters are False and True.data length: Specify the length of the longest value to generate. You can enter either a numeric value or an expression in the following format: config["column_size"].seed: Specify a seed value. You can enter either a numeric value or an expression in the following format: config["seed"].mygen = RegexGenerator("[0-9A-Z]+",False, 30)
def main():
i = 0
while i <= config["n_rows"]:
i = i + 1
varData = mygen.Generate();
yield varData
main()
Sequential read of rows from a text file:
# Read a txt file sequentially
def getColors():
fileName = config["config_path"] +'\\'+ r"Colors.txt"
with open(fileName) as f:
content = f.readlines()
for row in content:
yield row
def main(config):
return getColors()
Random read from a text file:
# Read a txt file randomly
import random
# Uncomment the line below to use a seed if needed
#random.seed(config["seed"])
def getColors():
fileName = config["config_path"] +'\\'+ r"Colors.txt"
with open(fileName) as f:
content = f.readlines()
for row in content:
yield row
def main(config):
colors = list(getColors())
while True:
yield colors[random.randint(0, len(colors)-1)]
Sequential read of data for a column from a CSV file:
# Read a csv file sequentially
import csv
def getCountryCodes():
columnName = 'ISO3166-1-Alpha-2'
fileName = config["config_path"] +'\\'+ r"CountryCodes.csv"
with open(fileName,"rb") as file:
reader = csv.DictReader(file, delimiter=';', quotechar='"')
for row in reader:
yield str(row[columnName]).upper()
def main(config):
return getCountryCodes()
Random read of data for a column from a CSV file:
# Read a csv file sequentially
import csv
import random
# Uncomment the line below to use a seed if needed
#random.seed(config["seed"])
def getCountryCodes():
columnName = 'ISO3166-1-Alpha-2'
fileName = config["config_path"] +'\\'+ r"CountryCodes.csv"
with open(fileName,"rb") as file:
reader = csv.DictReader(file, delimiter=';', quotechar='"')
for row in reader:
yield str(row[columnName]).upper()
def main(config):
codes = list(getCountryCodes())
while True:
yield codes[random.randint(0, len(codes)-1)]
Sequential read of rows from a XML file:
# Read an XML file
# Use the CLR XML libraries
import clr
clr.AddReference("System.Xml")
from System.Xml.XPath import XPathDocument, XPathNavigator
def getTitles(column_size=50):
filename = r"D:\books.xml"
doc = XPathDocument(filename)
nav = doc.CreateNavigator()
expr = nav.Compile("/catalog/book/title")
titles = nav.Select(expr)
for title in titles:
yield str(title)[:column_size]
def main(config):
# Truncate titles to the column size
return list(getTitles(column_size=config["column_size"]))
Generation of male and female names from files depending on a flag:
# Select a name depending on a flag
import random
# Uncomment the line below to use a seed if needed
#random.seed(config["seed"])
def getPersonNames(fileName):
fileName = config["config_path"] +'\\'+ fileName
with open(fileName) as f:
content = f.readlines()
for row in content:
yield row
def main(config):
males = list(getPersonNames(r"FirstNamesMale.txt"))
females= list(getPersonNames(r"FirstNamesFemale.txt" ))
while True:
# is_male - is a flag field
if is_male:
yield males[random.randint(0, len(males)-1)]
else:
yield females[random.randint(0, len(females)-1)]
Running a certain generator depending on a flag:
# Select a generator depending on a flag
def main(config):
maleGen = RegexGenerator("Automotive|Computers|Crafts|Tools")
femaleGen = RegexGenerator("Furniture|Pharmacy|Garden|Gifts")
while True:
# is_male - is a flag field
if is_male:
yield maleGen.Generate()
else:
yield femaleGen.Generate()
Calculating a new date based on a date value from other field:
# Calculate a date based on another date
import random
# Uncomment the line below to use a seed if needed
#random.seed(config["seed"])
def main(config):
# StartDate - is a column name
if str(StartDate) == '':
return DBNull.Value
# Add number of days to a StartDate
return StartDate.AddDays(random.randint(1, 1000));
The table provides key details about the generator, including its short name, examples of generated data, supported data types, and country-specific data.
| Short name | Example of generated data | Data type matching | Country-specific data |
|---|---|---|---|
| Python | EMP-1004, 3, 5 … | bigint, binary, bit, char, date, datetime, datetime2, datetimeoffset, decimal, float, hierarchyid, image, int, money, nchar, ntext, numeric, nvarchar(max), nvarchar, real, smalldatetime, smallint, smallmoney, sql_variant, text, time, tinyint, uniqueidentifier, varbinary(max), varbinary, varchar(max), varchar, xml | Default (United States) |
The generator can be assigned to a column that can have any name, regardless of the table name.
