You can download this code by clicking the button below.
This code is now available for download.
The function uses Pydantic's BaseModel to create a user model and generates a user object with a random name and age.
Technology Stack : Pydantic, BaseModel
Code Type : Function
Code Difficulty : Intermediate
from pydantic import BaseModel, Field
import random
class User(BaseModel):
name: str
age: int = Field(default=18, ge=0, lt=100)
email: str
def generate_random_user():
user = User(
name=random.choice(['Alice', 'Bob', 'Charlie', 'David', 'Eva']),
age=random.randint(18, 99)
)
return user