-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_data.py
75 lines (58 loc) · 1.64 KB
/
fake_data.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
from faker import Faker
from werkzeug.security import generate_password_hash
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import os
import random
from app.models import User,Post,Comment
basedir = os.path.abspath(os.path.dirname(__file__))
# print(fake.company_email())
# print(fake.name())
# print(fake.license_plate())
# print(fake.locale())
# print(generate_password_hash('1'))
def create_session():
engine = create_engine('sqlite:///' + os.path.join(basedir,'app.db'))
DBSession = sessionmaker(bind=engine)
session = DBSession()
return session
def add_user(n,locale='zh_CN'):
session = create_session()
fake = Faker(locale)
for _ in range(n):
user = User(username=fake.name(),email=fake.company_email(),password_hash=generate_password_hash('1'))
session.add(user)
try:
session.commit()
except Exception as e:
pass
session.close()
def add_post(n,locale='zh_CN'):
session = create_session()
fake = Faker(locale)
user_num = session.query(User.id).count()
for _ in range(n):
post = Post(user_id=random.randint(1,user_num),body=fake.text())
session.add(post)
try:
session.commit()
except Exception as e:
pass
session.close()
def add_comment(n,locale='zh_CN'):
session = create_session()
fake = Faker(locale)
user_num = session.query(User.id).count()
post_num = session.query(Post.id).count()
for _ in range(n):
comment = Comment(user_id=random.randint(1,user_num),post_id=random.randint(1,post_num),body=fake.text())
session.add(comment)
try:
session.commit()
except Exception as e:
pass
session.close()
if __name__ == '__main__':
# add_user(100)
# add_post(1000)
add_comment(10000)