forked from learn-co-curriculum/python-p3-freebie-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
66 lines (51 loc) · 1.54 KB
/
seed.py
File metadata and controls
66 lines (51 loc) · 1.54 KB
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
#!/usr/bin/env python3
# Script goes here!
from faker import Faker
import random
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Company , Freebie , Dev
if __name__ == '__main__':
engine = create_engine('sqlite:///freebies.db')
Session = sessionmaker(bind = engine)
session = Session()
session.query(Company).delete()
session.query(Dev).delete()
session.query(Freebie).delete()
fake = Faker()
companies = []
for i in range(30):
company = Company(
name=fake.unique.name(),
founding_year=random.randint(1980, 2023)
)
# add and commit individually to get IDs back
session.add(company)
session.commit()
companies.append(company)
devs = []
for i in range(25):
dev = Dev(
name=fake.name(),
)
session.add(dev)
session.commit()
devs.append(dev)
freebies= []
for company in companies:
for i in range (random.randint(1,5)):
dev = random.choice(devs)
if company not in dev.companies:
dev.companies.append(company)
session.add(dev)
session.commit()
freebie= Freebie(
item_name = fake.name(),
value= random.randint(0,20),
company_id= company.id,
dev_id= dev.id,
)
freebies.append(freebie)
session.bulk_save_objects(freebies)
session.commit()
session.close()