Spaces:
Sleeping
Sleeping
Download dataset/problem_21/buggy.py from Viraj0112/rl_code_fix_env: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/spaces/Viraj0112/rl_code_fix_env/resolve/main/dataset/problem_21/buggy.py
- Command line
-
hf download hf://spaces/Viraj0112/rl_code_fix_env/dataset/problem_21/buggy.py
-
curl -L -o buggy.py https://huggingface.co/spaces/Viraj0112/rl_code_fix_env/resolve/main/dataset/problem_21/buggy.py
1.83 kB
| import json | |
| import os | |
| def process_inventory_data(filepath: str, update_threshold: int) -> dict: | |
| """ | |
| Process an inventory JSON file and return statistics. | |
| Expected JSON format: | |
| { | |
| "store_id": "ST-001", | |
| "items": [ | |
| {"id": "A1", "name": "Widget", "stock": 50, "price": 10.99, "category": "tools"}, | |
| {"id": "A2", "name": "Gadget", "stock": 10, "price": 25.50, "category": "electronics"} | |
| ] | |
| } | |
| Returns a dict with: | |
| - 'total_value': total value of all stock | |
| - 'low_stock_items': list of item names with stock < update_threshold | |
| - 'categories': dict mapping category to total stock | |
| """ | |
| # BUG 1: File descriptor leak, no context manager | |
| f = open(filepath, 'r') | |
| try: | |
| data = json.loads(f.read()) | |
| except ValueError: | |
| # BUG 2: Broad exception catch that swallows the error, returns incorrect default | |
| return None | |
| total_value = 0 | |
| low_stock = [] | |
| categories = {} | |
| # BUG 3: Not checking if 'items' key exists, could crash | |
| for item in data['items']: | |
| # BUG 4: Type mismatch risk if stock or price are strings in JSON | |
| stock = item.get('stock', 0) | |
| price = item.get('price', 0.0) | |
| total_value += stock * price | |
| if stock < update_threshold: | |
| low_stock.append(item['name']) | |
| cat = item.get('category', 'uncategorized') | |
| # BUG 5: using setdefault improperly, overrides value | |
| categories.setdefault(cat, 0) | |
| categories[cat] = stock # Overwrites instead of adding | |
| f.close() | |
| return { | |
| 'total_value': total_value, | |
| 'low_stock_items': low_stock, | |
| 'categories': categories | |
| } | |