-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize.py
More file actions
56 lines (46 loc) · 1.49 KB
/
initialize.py
File metadata and controls
56 lines (46 loc) · 1.49 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
import sqlite3
import click
import faker
import random
import datetime
import sys
"""This module inizializes the database with fake data"""
connector = sqlite3.connect("contact_book.db")
fake = faker.Faker()
n_contacts = 30
def reset(connector):
"""Reset the database. Caution: all your contact will be lost!"""
connector.executescript(
"""
DROP TABLE IF EXISTS contacts;
CREATE TABLE contacts(
NAME TEXT NOT NULL,
SURNAME TEXT NOT NULL,
PHONE TEXT NOT NULL,
EMAIL TEXT,
BIRTHDAY TEXT
);
"""
)
print("The contact book has been reset")
def mock_database(connector, n_contacts):
for _ in range(n_contacts):
name = fake.first_name()
surname = fake.last_name()
email = f"{name.lower()}.{surname.lower()}@{fake.free_email_domain()}"
phone = fake.msisdn()
# birthday has to be cast into a string to mimick what click.DateTime
# does in contact new in the main script
birthday = str(fake.date_between(start_date="-80y", end_date="-15y"))
connector.execute(
"""
INSERT INTO contacts
VALUES (?, ?, ?, ?, ?)""",
(name, surname, phone, email, birthday),
)
connector.commit()
if __name__ == "__main__":
if len(sys.argv) > 1:
n_contacts = int(sys.argv[1])
reset(connector)
mock_database(connector, n_contacts)