Show practical real-world use cases for range queries.
()
| 65 | |
| 66 | |
| 67 | def demo_practical_use_cases(): |
| 68 | """Show practical real-world use cases for range queries.""" |
| 69 | print("\n=== Practical Use Cases ===\n") |
| 70 | |
| 71 | # Scenario 1: Time-series data |
| 72 | print("1. Time-series data (last 7 days)") |
| 73 | tree = BPlusTreeMap(capacity=16) |
| 74 | |
| 75 | # Simulate daily metrics |
| 76 | base_date = datetime.now() |
| 77 | for i in range(30): # 30 days of data |
| 78 | date_key = int((base_date - timedelta(days=i)).timestamp()) |
| 79 | tree[date_key] = { |
| 80 | "date": (base_date - timedelta(days=i)).strftime("%Y-%m-%d"), |
| 81 | "users": random.randint(100, 1000), |
| 82 | "revenue": random.randint(1000, 10000), |
| 83 | } |
| 84 | |
| 85 | # Get last 7 days (most recent timestamps) |
| 86 | cutoff = int((base_date - timedelta(days=7)).timestamp()) |
| 87 | print(" Last 7 days of metrics:") |
| 88 | count = 0 |
| 89 | for timestamp, metrics in tree.range(cutoff, None): |
| 90 | print( |
| 91 | f" {metrics['date']}: {metrics['users']} users, ${metrics['revenue']} revenue" |
| 92 | ) |
| 93 | count += 1 |
| 94 | if count >= 7: |
| 95 | break |
| 96 | |
| 97 | # Scenario 2: Score ranges |
| 98 | print("\n2. Student grade analysis") |
| 99 | grades_tree = BPlusTreeMap(capacity=8) |
| 100 | |
| 101 | students = [ |
| 102 | ("Alice", 95), |
| 103 | ("Bob", 67), |
| 104 | ("Charlie", 89), |
| 105 | ("Diana", 76), |
| 106 | ("Eve", 93), |
| 107 | ("Frank", 54), |
| 108 | ("Grace", 88), |
| 109 | ("Henry", 72), |
| 110 | ("Iris", 91), |
| 111 | ("Jack", 63), |
| 112 | ("Kate", 85), |
| 113 | ("Leo", 79), |
| 114 | ] |
| 115 | |
| 116 | for name, score in students: |
| 117 | grades_tree[score] = name |
| 118 | |
| 119 | print(" A grades (90-100):") |
| 120 | for score, name in grades_tree.range(90, 101): |
| 121 | print(f" {name}: {score}") |
| 122 | |
| 123 | print(" B grades (80-89):") |
| 124 | for score, name in grades_tree.range(80, 90): |
no test coverage detected