You can download this code by clicking the button below.
This code is now available for download.
This function takes a username as an argument, generates a random email address, and saves it to the database.
Technology Stack : Peewee, SqliteDatabase, Model, CharField, random, string
Code Type : Function
Code Difficulty : Intermediate
def generate_random_user_email(username):
import random
import string
from peewee import *
# Create a database connection
database = SqliteDatabase('example.db')
# Define a model for the User table
class User(Model):
username = CharField()
email = CharField()
class Meta:
database = database
# Create the table
database.connect()
database.create_tables([User])
# Generate a random email for the user
domain = "@example.com"
random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))
email = f"{username}{random_string}{domain}"
# Save the user to the database
new_user = User(username=username, email=email)
new_user.save()
# Close the database connection
database.close()
return email