Python difflib Module - HTML Diff Demo

← PyNotes Home difflib Article

This demonstrates the HTML side-by-side diff format from Python's difflib.HtmlDiff.make_file() method.


Original Code
Modified Code
n1def calculate_price(quantity, unit_price):n1def calculate_price(quantity, unit_price, tax_rate=0.0):
2    """Calculate total price for items."""2    """Calculate total price for items including tax."""
3    total = quantity * unit_price3    subtotal = quantity * unit_price
4    tax_amount = subtotal * tax_rate
5    total = subtotal + tax_amount
4    return total6    return total
57
7def apply_discount(price, discount_rate):9def apply_discount(price, discount_rate):
8    """Apply percentage discount to price."""10    """Apply percentage discount to price."""
nn11    if discount_rate < 0 or discount_rate > 1:
12        raise ValueError("Discount rate must be between 0 and 1")
9    discount_amount = price * discount_rate13    discount_amount = price * discount_rate
10    final_price = price - discount_amount14    final_price = price - discount_amount
1216
1317
n14def process_order(items):n18def process_order(items, tax_rate=0.1):
15    """Process an order with multiple items."""19    """Process an order with multiple items and apply tax."""
16    total = 020    total = 0
17    for item in items:21    for item in items:
n18        price = calculate_price(item['qty'], item['price'])n22        price = calculate_price(item['qty'], item['price'], tax_rate)
19        total += price23        total += price
tt24 
25    print(f"Order total: ${total:.2f}")
20    return total26    return total
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

Part of Brad's Python Notes