8. Dynamic Data Transformation
By Bernd Klein. Last modified: 24 Mar 2024.
Definition
Dynamic data transformation refers to the process of altering or converting data in real-time or on-the-fly based on specific conditions, rules, or requirements. Unlike static data transformation, which applies fixed transformations to data regardless of context, dynamic data transformation adapts its transformations dynamically according to the data's characteristics or external factors.
This approach allows for flexibility and responsiveness in handling diverse datasets or changing requirements. Dynamic data transformation typically involves techniques such as conditional logic, parameterization, and automation to efficiently modify data as it flows through a system or process. It is commonly used in various domains including data integration, ETL (Extract, Transform, Load), data cleansing, and data analysis to ensure that data is appropriately formatted, structured, and enriched for downstream applications or analysis.
Live Python training
See our Python training courses
Product Class Example
Our example showcases a product class that could be utilized within a company or shop setting, e.g. a cheese shop.1

In the context of the provided Product class, when you change the currency using the set_currency method with the adapt_data parameter set to True, it dynamically adjusts the displayed prices and shipping costs based on the new currency without altering the original saved values. This process ensures that the user sees the values in the desired currency without permanently changing the underlying data. So this means that the process of dynamically changing the data also means that we are often seeing different values than are saved.
It is also more efficient to keep the data instead of permanently changing it. We just adapt the data "on demand":
class Product:
"""
A class representing a product with price and shipping cost.
Attributes:
conversion_rates (dict): A dictionary containing conversion rates from different currencies to USD.
"""
conversion_rates = {'USD': 1, 'EUR': 0.92, 'CHF': 0.90, 'GBP': 0.79}
def __init__(self, name, price, shipping_cost, currency='USD'):
"""
Initializes a Product object with the given parameters.
Args:
name (str): The name of the product.
price (float): The price of the product in the specified currency.
shipping_cost (float): The shipping cost of the product in the specified currency.
currency (str, optional): The currency code for price and shipping cost. Defaults to 'USD'.
"""
self.name = name
self._price = price
self._shipping_cost = shipping_cost
self.currency = currency
self._used_currency = currency
def set_currency(self, new_currency, adapt_data=False):
"""
Sets a new currency for the product and optionally adapts existing data.
Args:
new_currency (str): The new currency code.
adapt_data (bool, optional): Whether to adapt existing data to the new currency. Defaults to False.
"""
if self.currency != new_currency:
self.currency = new_currency
if adapt_data:
self._price = self.price
self._shipping_cost = self.shipping_cost
self._used_currency = new_currency
@property
def price(self):
"""
Property representing the price of the product in the specified currency.
Returns:
float: The price of the product.
"""
return self._convert_currency(self._price)
@property
def shipping_cost(self):
"""
Property representing the shipping cost of the product in the specified currency.
Returns:
float: The shipping cost of the product.
"""
return self._convert_currency(self._shipping_cost)
def _convert_currency(self, amount):
"""
Converts an amount from the internal currency to the specified currency.
Args:
amount (float): The amount to be converted.
Returns:
float: The converted amount.
"""
factor = Product.conversion_rates[self.currency] / Product.conversion_rates[self._used_currency]
return round(amount * factor, 2)
def __str__(self):
"""
Returns a string representation of the Product object.
Returns:
str: A string containing product details.
"""
return f"Product: {self.name}, Price: {self.price} {self.currency}, Shipping Cost: {self.shipping_cost} {self.currency}"
def show_saved_data(self):
