askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/services/calculate_shipping.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
calculate_shipping.py · 2.6 KB · 80 lines
1
2#!/usr/bin/env python3
3"""
4AI-Generated Service: calculate_shipping
5Description: Calculate shipping cost based on weight and destination. Express costs 50% more than standard. Add $5 for fragile items. Free shipping over $100.
6Generated: 2025-09-19T09:15:41.522895
7"""
8
9from typing import Dict, Any, Optional
10import json
11import asyncio
12from datetime import datetime
13
14# AI-Generated Implementation
15async 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
75def 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']