Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Robot Data Separator

This repository contains scripts to separate robots data from larger JSON files to reduce import size.

## Files

- **`separate_robots.py`** - Full-featured script with command line arguments
- **`simple_robot_filter.py`** - Simple script for basic usage
- **`select_robots.py`** - Script to select specific robots (configurable)
- **`interactive_robot_selector.py`** - Interactive script to choose robots at runtime
- **`README.md`** - This instruction file

## 🚀 Quick Start Options

### Option 1: Simple Filter (All Robots)
```bash
python simple_robot_filter.py
```
- Just change the filename at the top and run
- Creates `robots_only.json` with ALL robots

### Option 2: Select Specific Robots (Configurable)
```bash
python select_robots.py
```
- Edit the script to choose which robots to include/exclude
- More control over selection

### Option 3: Interactive Selection (Recommended)
```bash
python interactive_robot_selector.py
```
- **Best option for choosing robots!**
- Shows you all available robots
- Lets you pick exactly which ones you want
- No need to edit code

## 🎯 How to Choose Specific Robots

### Method 1: Interactive Script (Easiest)
1. Run: `python interactive_robot_selector.py`
2. Enter your JSON filename
3. See all available robots with counts
4. Choose from the menu:
- Pick individual robots
- Select "ALL robots"
- Use "Custom selection" for multiple robots
5. Choose your output filename
6. Get your filtered data!

### Method 2: Edit the Configurable Script
In `select_robots.py`, change these lines:

```python
# Include only specific robots
INCLUDE_ROBOTS = ["Term.", "Robot1", "Robot2"] # Add robot names you want

# OR exclude specific robots
EXCLUDE_ROBOTS = ["UnwantedRobot"] # Add robots to exclude

# Choose which method to use
USE_INCLUDE_LIST = True # True for include, False for exclude
```

### Method 3: Command Line (Advanced)
```bash
# Include only specific robots
python separate_robots.py your_file.json --include "Term.,Robot1"

# Exclude specific robots
python separate_robots.py your_file.json --exclude "UnwantedRobot"
```

## 📋 What Each Script Does

| Script | Purpose | Best For |
|--------|---------|----------|
| `simple_robot_filter.py` | Get ALL robots | Quick filtering |
| `select_robots.py` | Pre-configured selection | Repeatable filtering |
| `interactive_robot_selector.py` | Choose at runtime | **Selecting specific robots** |
| `separate_robots.py` | Command line control | Automation/scripts |

## 🔍 Example: Selecting Only "Term." Robot

### Using Interactive Script:
1. Run: `python interactive_robot_selector.py`
2. Enter your filename
3. Choose option 1 (if "Term." is first in the list)
4. Or use "Custom selection" and enter the number

### Using Configurable Script:
```python
# In select_robots.py
INCLUDE_ROBOTS = ["Term."] # Only include Term. robot
USE_INCLUDE_LIST = True
```

## 📊 Expected Output

The scripts will show you:
- Total records loaded
- Available robots and their counts
- Records found after filtering
- Final robot breakdown
- File size reduction percentage

## 🛠️ Requirements

- Python 3.6 or higher
- No additional packages required (uses only built-in libraries)

## 🚨 Troubleshooting

- **File not found**: Make sure you've updated the filename in the script
- **JSON errors**: Ensure your file contains valid JSON
- **No robots found**: Check that your "Robô" field contains data
- **Interactive script not working**: Make sure you're running it in a terminal that supports input

## 💡 Tips

1. **Start with the interactive script** - it's the easiest way to see what robots you have
2. **Use the configurable script** if you need to run the same filter repeatedly
3. **Check the file size reduction** to see how much data you're saving
4. **Backup your original file** before running any scripts
199 changes: 199 additions & 0 deletions interactive_robot_selector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#!/usr/bin/env python3
"""
Interactive script to select robots from JSON file at runtime.
This script will show you all available robots and let you choose which ones to include.
"""

import json
import sys
from pathlib import Path

def load_json_file(filename):
"""Load JSON data from file."""
try:
with open(filename, 'r', encoding='utf-8') as file:
return json.load(file)
except FileNotFoundError:
print(f"❌ Error: File '{filename}' not found.")
return None
except json.JSONDecodeError as e:
print(f"❌ Error: Invalid JSON in file '{filename}': {e}")
return None

def get_available_robots(data):
"""Get all available robots and their counts."""
robots = {}
for record in data:
if "Robô" in record and record["Robô"]:
robot_name = str(record["Robô"]).strip()
robots[robot_name] = robots.get(robot_name, 0) + 1
return robots

def display_robot_menu(robots):
"""Display robots in a numbered menu."""
print("\n🤖 Available robots in your data:")
print("=" * 50)

robot_list = sorted(robots.items())
for i, (robot, count) in enumerate(robot_list, 1):
print(f" {i:2d}. {robot:<20} ({count:4d} records)")

print(f" {len(robot_list) + 1:2d}. ALL robots")
print(f" {len(robot_list) + 2:2d}. Custom selection")
print(f" {len(robot_list) + 3:2d}. Exit")

return robot_list

def get_user_selection(robot_list):
"""Get user's robot selection."""
while True:
try:
choice = input(f"\n👉 Enter your choice (1-{len(robot_list) + 3}): ").strip()
choice_num = int(choice)

if choice_num == len(robot_list) + 1: # ALL robots
return "ALL"
elif choice_num == len(robot_list) + 2: # Custom selection
return get_custom_selection(robot_list)
elif choice_num == len(robot_list) + 3: # Exit
return "EXIT"
elif 1 <= choice_num <= len(robot_list):
selected_robot = robot_list[choice_num - 1][0]
return [selected_robot]
else:
print("❌ Invalid choice. Please try again.")
except ValueError:
print("❌ Please enter a valid number.")

def get_custom_selection(robot_list):
"""Get custom robot selection from user."""
print(f"\n📝 Enter robot numbers separated by commas (e.g., 1,3,5)")
print(" Or enter 'all' for all robots")

while True:
choice = input("👉 Your selection: ").strip().lower()

if choice == "all":
return "ALL"

try:
numbers = [int(x.strip()) for x in choice.split(",")]
selected_robots = []

for num in numbers:
if 1 <= num <= len(robot_list):
selected_robots.append(robot_list[num - 1][0])
else:
print(f"❌ Invalid number: {num}")
return None

if selected_robots:
return selected_robots
else:
print("❌ No valid robots selected. Please try again.")
except ValueError:
print("❌ Invalid input. Please enter numbers separated by commas.")

def filter_data_by_robots(data, selected_robots):
"""Filter data based on selected robots."""
if selected_robots == "ALL":
return [record for record in data if "Robô" in record and record["Robô"]]

filtered_data = []
for record in data:
if "Robô" in record and record["Robô"]:
robot_name = str(record["Robô"]).strip()
if robot_name in selected_robots:
filtered_data.append(record)

return filtered_data

def save_filtered_data(data, output_filename):
"""Save filtered data to file."""
try:
with open(output_filename, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=2, ensure_ascii=False)
return True
except Exception as e:
print(f"❌ Error saving file: {e}")
return False

def main():
"""Main interactive function."""
print("🚀 Interactive Robot Selector")
print("=" * 40)

# Get input filename
while True:
filename = input("📁 Enter your JSON filename: ").strip()
if filename:
break
print("❌ Please enter a filename.")

# Load data
print(f"\n📂 Loading {filename}...")
data = load_json_file(filename)
if data is None:
return

print(f"✅ Loaded {len(data)} total records")

# Get available robots
robots = get_available_robots(data)
if not robots:
print("❌ No robots found in the data!")
return

# Display menu and get selection
robot_list = display_robot_menu(robots)
selection = get_user_selection(robot_list)

if selection == "EXIT":
print("👋 Goodbye!")
return

# Filter data
print(f"\n🔍 Filtering data...")
if selection == "ALL":
print("📋 Including ALL robots")
else:
print(f"📋 Including: {', '.join(selection)}")

filtered_data = filter_data_by_robots(data, selection)
print(f"✅ Found {len(filtered_data)} matching records")

# Get output filename
output_filename = input(f"\n💾 Enter output filename (default: selected_robots.json): ").strip()
if not output_filename:
output_filename = "selected_robots.json"

# Save data
print(f"\n💾 Saving to {output_filename}...")
if save_filtered_data(filtered_data, output_filename):
print("✅ Data saved successfully!")

# Show final statistics
final_robots = {}
for record in filtered_data:
robot_name = str(record["Robô"]).strip()
final_robots[robot_name] = final_robots.get(robot_name, 0) + 1

print(f"\n📈 Final robot breakdown:")
print("-" * 30)
for robot, count in sorted(final_robots.items()):
print(f" {robot}: {count} records")

# Calculate size reduction
original_size = Path(filename).stat().st_size
filtered_size = Path(output_filename).stat().st_size
reduction = ((original_size - filtered_size) / original_size) * 100

print(f"\n📊 File size reduction:")
print(f" Original: {original_size:,} bytes")
print(f" Filtered: {filtered_size:,} bytes")
print(f" Reduction: {reduction:.1f}%")

print("\n🎉 Done!")

if __name__ == "__main__":
main()
Loading