File size: 2,305 Bytes
90d2159 871a880 90d2159 eada831 871a880 90d2159 eada831 90d2159 eada831 871a880 eada831 871a880 eada831 871a880 eada831 871a880 eada831 871a880 eada831 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import time
import logging
from typing import Optional
class VirtualRobot:
"""Enhanced virtual robot with state management and logging."""
def __init__(self):
"""Initialize the virtual robot with default state."""
self.state = "IDLE"
self._setup_logging()
logging.info("π€ Virtual Robot initialized in %s state", self.state)
def _setup_logging(self):
"""Configure logging settings."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
def perform_action(self, action: str) -> str:
"""
Process robot actions with validation and error handling.
Args:
action: The action command (e.g., "wave" or "say hello")
Returns:
Response string from the robot
"""
if not action or not isinstance(action, str):
logging.warning("Invalid action command received")
return "β Invalid command"
action = action.lower().strip()
try:
if action == "wave":
return self._wave()
elif action.startswith("say"):
return self._speak(action[3:].strip())
else:
logging.warning("Unknown action: %s", action)
return "β Unknown action - try 'wave' or 'say [message]'"
except Exception as e:
logging.error("Action failed: %s", str(e))
return f"β Error: {str(e)}"
def _wave(self) -> str:
"""Perform waving animation."""
self.state = "WAVING"
logging.info("Performing wave action")
time.sleep(0.8) # Simulate animation time
self.state = "IDLE"
return "π *waves enthusiastically*"
def _speak(self, message: str) -> str:
"""Generate speech response."""
if not message:
return "β Please provide something to say"
self.state = "SPEAKING"
logging.info("Speaking: '%s'", message)
time.sleep(max(0.5, len(message)*0.07)) # Dynamic delay
self.state = "IDLE"
return f"π£οΈ {message.capitalize()}" |