List of Transformation Scripts
The section below shows script samples for various business use cases.
Requirement: Replace all occurrences of “Crimson” for the attribute “Color” with “Red”
Script:
def transformer(product):
resultant = None
if “Color” in product:
resultant = product[“Colour”].replace(“Crimson”, “Red”)return resultant
Requirement: Join two attributes “ Brand” and “Product Name” using the joining value “-”
Script:
def transformer(product):
resultant = None
if {“Brand”, “Product Name”} <= set(product):
resultant = product[“Brand”] + “-” +product[“Product Name”]return resultant
Requirement: Prefix (add something at the beginning) US currency “$” for the attribute “Price”
Script:
def transformer(product):
resultant = None
if “Price” in product:
resultant = “$” + product[“Price”]return resultant
Requirement: Suffix (add something at the end) registered trademark to the values of the attribute “Product Name”
Script:
def transformer(product):
resultant = None
if “Product Name” in product:
resultant = product[“Product Name”] + “®”return resultant
Requirement: Add two numerical attribute values for attributes“ Base Price” and “Delivery Charges”
That is,
Actual price = Base price + Delivery charges
Script:
def transformer(product):
resultant = None
if {“Base Price”, “Delivery Charges”} <= set(product):
resultant = float(product[“Base Price”]) + float(product[“Delivery Charges”])return resultant
Requirement: Subtract two numerical attribute values for attributes “ Actual Price” and “Discounted Price”
That is,
Offer Price= Actual Price- Discount Price
Script:
def transformer(product):
resultant = None
if {“Actual Price”, “Discount Price”} <= set(product):
resultant = float(product[“Actual Price”]) – float(product[“Discount Price”])return resultant
Requirement: Multiply two numerical attribute values for attributes “Number of Units” and “Price per Unit”
That is,
Total Price = “Number of Units” * “Price per Unit”
Script:
def transformer(product):
resultant = None
if {“Number of Units”, “Price per Unit”} <= set(product):
resultant = float(product[“Number of Units”]) * float(product[“Price per Unit”])return resultant
Requirement: Divide two numerical attribute values for attributes “Total Weight of the Products” and “Total Weight of the Carton”
That is,
Per Unit Weight = Total Weight of the Products / Total Weight of the Carton
Script:
def transformer(product):
resultant = None
if {“Total Weight of the Products”, “Total Weight of the Carton”} <= set(product):
resultant = float(product[“Total Weight of the Products”]) * float(product[“Total Weight of the Carton”])return resultant
Requirement: Get % portion of a particular attribute value
That is,
Offer Price= 80% of Actual Price
Script:
def transformer(product):
resultant = None
if “Actual Price” in product:
resultant = float(product[“Actual Price”]) * 80/100return resultant
Requirement: Round off the attributes values of a attribute “Weight” upto two decimal places
That is, the value 114.7261 will become 114.73
Script:
def transformer(product):
resultant = None
if {“Weight”} <= set(product):
resultant = round(float(product[“Weight”]), 2)return resultant
Requirement: Split the attribute “Image URL” and pick the second part of the “image URL”
For example,
https://images-cdn.azureedge.net/azure/in-resources/bd5c1517-8d80-48d7-8e8e-365433ad124f/Images/ProductImages/Source/noimage.png”>https://images-cdn.azureedge.net/azure/in-resources/bd5c1517-8d80-48d7-8e8e-365433ad124f/Images/ProductImages/Source/noimage.png
In this case, if you want to split by “.net”, the output will be the following:
/azure/in-resources/bd5c1517-8d80-48d7-8e8e-365433ad124f/Images/ProductImages/Source/noimage.png
Script:
def transformer(product):
resultant = None
if “imageUrl” in product:
splitArr = product[“imageUrl”].split(“.net”)
resultant = splitArr[int(1)]return resultant