Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
calculate_shipping.py · 2.6 KB · 80 lines
| 1 | |
| 2 | #!/usr/bin/env python3 |
| 3 | """ |
| 4 | AI-Generated Service: calculate_shipping |
| 5 | Description: Calculate shipping cost based on weight and destination. Express costs 50% more than standard. Add $5 for fragile items. Free shipping over $100. |
| 6 | Generated: 2025-09-19T09:15:41.522895 |
| 7 | """ |
| 8 | |
| 9 | from typing import Dict, Any, Optional |
| 10 | import json |
| 11 | import asyncio |
| 12 | from datetime import datetime |
| 13 | |
| 14 | # AI-Generated Implementation |
| 15 | async def calculate_shipping(data: Dict[str, Any]) -> Dict[str, Any]: |
| 16 | """ |
| 17 | Calculate shipping cost based on weight and destination. Express costs 50% more than standard. Add $5 for fragile items. Free shipping over $100. |
| 18 | |
| 19 | Inputs: weight, shipping_speed, is_fragile, order_total |
| 20 | Outputs: shipping_cost, carrier, free_shipping |
| 21 | """ |
| 22 | |
| 23 | try: |
| 24 | # Extract inputs |
| 25 | weight = data.get('weight', None) |
| 26 | shipping_speed = data.get('shipping_speed', None) |
| 27 | is_fragile = data.get('is_fragile', False) |
| 28 | order_total = data.get('order_total', None) |
| 29 | |
| 30 | # AI-Generated Business Logic |
| 31 | # Calculate shipping cost |
| 32 | base_cost = weight * 2.5 # $2.50 per pound |
| 33 | |
| 34 | # Apply shipping speed multiplier |
| 35 | speed_multipliers = { |
| 36 | 'standard': 1.0, |
| 37 | 'express': 1.5, |
| 38 | 'overnight': 2.5 |
| 39 | } |
| 40 | multiplier = speed_multipliers.get(shipping_speed, 1.0) |
| 41 | cost = base_cost * multiplier |
| 42 | |
| 43 | # Add handling fees |
| 44 | if is_fragile: |
| 45 | cost += 5.0 # Fragile handling fee |
| 46 | |
| 47 | # Check for free shipping |
| 48 | free_shipping = False |
| 49 | if 'order_total' in locals() and order_total >= 100: |
| 50 | cost = 0 |
| 51 | free_shipping = True |
| 52 | |
| 53 | result = { |
| 54 | 'shipping_cost': round(cost, 2), |
| 55 | 'free_shipping_applied': free_shipping, |
| 56 | 'carrier': 'UPS' if shipping_speed == 'express' else 'USPS', |
| 57 | 'estimated_days': 1 if shipping_speed == 'overnight' else (2 if shipping_speed == 'express' else 5) |
| 58 | } |
| 59 | |
| 60 | # Return results |
| 61 | return { |
| 62 | "success": True, |
| 63 | "timestamp": datetime.now().isoformat(), |
| 64 | "data": result |
| 65 | } |
| 66 | |
| 67 | except Exception as e: |
| 68 | return { |
| 69 | "success": False, |
| 70 | "error": str(e), |
| 71 | "timestamp": datetime.now().isoformat() |
| 72 | } |
| 73 | |
| 74 | # Synchronous wrapper for compatibility |
| 75 | def calculate_shipping_sync(data: Dict[str, Any]) -> Dict[str, Any]: |
| 76 | """Synchronous wrapper for the async function""" |
| 77 | return asyncio.run(calculate_shipping(data)) |
| 78 | |
| 79 | # Export the service |
| 80 | __all__ = ['calculate_shipping', 'calculate_shipping_sync'] |