Write a Python script that uses the Cassandra database to store and retrieve user information (name, email, age) with appropriate data validation checks. The script should provide options for adding new users, updating existing user details, and deleting users.
PLAINFORMAT
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import re

# Connect to the Cassandra cluster
def connect_to_cassandra():
    auth_provider = PlainTextAuthProvider(username='cassandra', password='cassandra')
    cluster = Cluster(['127.0.0.1'], port=9042, auth_provider=auth_provider)
    session = cluster.connect()
    return session

# Create keyspace and table if they don't exist
def create_keyspace_and_table(session):
    session.execute("CREATE KEYSPACE IF NOT EXISTS user_info WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};")
    session.set_keyspace('user_info')
    session.execute("""
        CREATE TABLE IF NOT EXISTS users (
            name text PRIMARY KEY,
            email text,
            age int
        );
    """)

# Validate user data
def validate_user_data(name, email, age):
    if not isinstance(name, str) or len(name.strip()) == 0:
        raise ValueError("Name must be a non-empty string.")
    
    if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
        raise ValueError("Invalid email address.")
    
    if not isinstance(age, int) or age < 1:
        raise ValueError("Age must be a positive integer.")

# Add new user
def add_new_user(session, name, email, age):
    validate_user_data(name, email, age)
    session.execute(
        "INSERT INTO users (name, email, age) VALUES (%s, %s, %s);",
        (name, email, age)
    )

# Update existing user details
def update_user_details(session, name, email=None, age=None):
    if email is None and age is None:
        raise ValueError("At least one of email or age should be provided for update.")
    
    if email is not None:
        validate_user_data(name, email, 1)
        session.execute(
            "UPDATE users SET email = %s WHERE name = %s;",
            (email, name)
        )
    
    if age is not None:
        validate_user_data(name, "", age)
        session.execute(
            "UPDATE users SET age = %s WHERE name = %s;",
            (age, name)
        )

# Delete user
def delete_user(session, name):
    session.execute("DELETE FROM users WHERE name = %s;", (name,))

# Get all users
def get_all_users(session):
    rows = session.execute("SELECT * FROM users;")
    return list(rows)

if __name__ == "__main__":
    session = connect_to_cassandra()
    create_keyspace_and_table(session)

    # Add new users
    add_new_user(session, "John Doe", "john.doe@example.com", 30)
    add_new_user(session, "Jane Smith", "jane.smith@example.com", 25)

    # Update existing user details
    update_user_details(session, "John Doe", email="john.new@example.com")
    update_user_details(session, "Jane Smith", age=26)

    # Delete a user
    delete_user(session, "John Doe")

    # Print all users
    print(get_all_users(session))