-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.py
103 lines (89 loc) · 3.02 KB
/
insert.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from faker import Faker
from models import *
from sqlalchemy import inspect
from sqlalchemy.orm import sessionmaker
import random
from datetime import datetime
from sqlalchemy import text
# Instantiate a Faker object for generating fake data
fake = Faker(["en_US"])
# Set a seed for Faker to generate repeatable pseudorandom data.
Faker.seed(0)
random.seed(0)
# Drop all existing tables
Base.metadata.drop_all(bind=engine)
# Create all tables based on model definitions
Base.metadata.create_all(bind=engine)
# Create a sessionmaker bound to the engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# Function to create a fake user
def create_fake_user(fake):
user = User(
name=fake.name(),
email=fake.email(),
age=random.randint(18, 80),
gender=random.choice(['Male', 'Female', 'Other']),
height=random.uniform(1.5, 2.0),
weight=random.uniform(50, 100)
)
session.add(user)
return user
# Function to create fake data for other models
def create_fake_data(user,fake):
# Create a fake workout
workout = Workout(
user=user,
date=fake.date_between(start_date='-1y', end_date='today'),
type=random.choice(['Running', 'Cycling', 'Yoga', 'Weightlifting']),
duration=random.uniform(0.5, 2),
intensity=random.choice(['Low', 'Medium', 'High']),
calories_burned=random.randint(100, 800)
)
session.add(workout)
# Create fake nutrition data
nutrition = Nutrition(
user=user,
date=fake.date_between(start_date='-1y', end_date='today'),
meal_type=random.choice(['Breakfast', 'Lunch', 'Dinner', 'Snack']),
calories=random.randint(100, 500),
protein=random.uniform(0, 30),
carbohydrates=random.uniform(0, 50),
fat=random.uniform(0, 20)
)
session.add(nutrition)
# Create fake sleep data
sleep = Sleep(
user=user,
date=fake.date_between(start_date='-1y', end_date='today'),
start_time=datetime.now().time(),
end_time=datetime.now().time(),
quality=random.choice(['Poor', 'Average', 'Good', 'Excellent'])
)
session.add(sleep)
# Generate and insert fake data
for _ in range(10):
try:
# Begin a transaction
with session.begin():
fake_user = create_fake_user(fake)
session.add(fake_user)
create_fake_data(fake_user, fake)
except Exception as e:
# Rollback the transaction in case of an error
session.rollback()
print(f"An error occurred: {e}")
# Commit the session
session.commit()
# Creating an inspector object to inspect the database.
inspector = inspect(engine)
# Get list of table names
table_names = inspector.get_table_names()
# Iterating over each table and printing its contents.
for table_name in table_names:
print(f"Contents of table {table_name}:")
with engine.connect() as connection:
result = connection.execute(text(f"SELECT * FROM {table_name}"))
for row in result:
print(row)
print("\n")