-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
data.py
429 lines (406 loc) · 17.7 KB
/
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import datetime
import random
import typing as t
import click
from faker import Faker
from schema import (
Author,
Book,
BookAuthor,
BookLanguage,
BookShelf,
BookSubject,
City,
Continent,
Country,
Language,
Publisher,
Rating,
Shelf,
Subject,
)
from sqlalchemy.orm import sessionmaker
from pgsync.base import pg_engine, subtransactions
from pgsync.constants import DEFAULT_SCHEMA
from pgsync.helper import teardown
from pgsync.utils import config_loader, get_config
@click.command()
@click.option(
"--config",
"-c",
help="Schema config",
type=click.Path(exists=True),
)
@click.option("--nsize", "-n", default=1, help="Number of dummy data samples")
def main(config, nsize):
config: str = get_config(config)
teardown(drop_db=False, config=config)
for doc in config_loader(config):
database: str = doc.get("database", doc["index"])
with pg_engine(database) as engine:
schema: str = doc.get("schema", DEFAULT_SCHEMA)
connection = engine.connect().execution_options(
schema_translate_map={None: schema}
)
Session = sessionmaker(bind=connection, autoflush=True)
session = Session()
# Bootstrap
continents: t.Dict[str, Continent] = {
"Europe": Continent(name="Europe"),
"North America": Continent(name="North America"),
}
with subtransactions(session):
session.add_all(continents.values())
countries: t.Dict[str, Country] = {
"United Kingdom": Country(
name="United Kingdom",
continent=continents["Europe"],
),
"France": Country(
name="France",
continent=continents["Europe"],
),
"United States": Country(
name="United States",
continent=continents["North America"],
),
}
with subtransactions(session):
session.add_all(countries.values())
cities: t.Dict[str, City] = {
"London": City(
name="London", country=countries["United Kingdom"]
),
"Paris": City(name="Paris", country=countries["France"]),
"New York": City(
name="New York", country=countries["United States"]
),
}
with subtransactions(session):
session.add_all(cities.values())
publishers: t.Dict[str, Publisher] = {
"Oxford Press": Publisher(name="Oxford Press", is_active=True),
"Penguin Books": Publisher(
name="Penguin Books", is_active=False
),
"Pearson Press": Publisher(
name="Pearson Press", is_active=True
),
"Reutgers Press": Publisher(
name="Reutgers Press", is_active=False
),
}
with subtransactions(session):
session.add_all(publishers.values())
authors: t.Dict[str, Author] = {
"Stephen King": Author(
name="Stephen King",
date_of_birth=datetime.datetime(1947, 9, 21),
city=cities["London"],
),
"J. K. Rowling": Author(
name="J. K. Rowling",
date_of_birth=datetime.datetime(1965, 7, 31),
city=cities["Paris"],
),
"James Patterson": Author(
name="James Patterson",
date_of_birth=datetime.datetime(1947, 3, 22),
city=cities["New York"],
),
"Melinda Leigh": Author(
name="Melinda Leigh",
date_of_birth=datetime.datetime(1980, 1, 1),
city=cities["Paris"],
),
"Tolu Aina": Author(
name="Tolu Aina",
date_of_birth=datetime.datetime(1980, 5, 21),
city=cities["London"],
),
}
with subtransactions(session):
session.add_all(authors.values())
subjects: t.Dict[str, Subject] = {
"Literature": Subject(name="Literature"),
"Poetry": Subject(name="Poetry"),
"Romance": Subject(name="Romance"),
"Science Fiction & Fantasy": Subject(
name="Science Fiction & Fantasy"
),
"Westerns": Subject(name="Westerns"),
}
with subtransactions(session):
session.add_all(subjects.values())
languages: t.Dict[str, Language] = {
"en-GB": Language(code="en-GB"),
"en-US": Language(code="en-US"),
"de-DE": Language(code="de-DE"),
"af-ZA": Language(code="af-ZA"),
"es-ES": Language(code="es-ES"),
"fr-FR": Language(code="fr-FR"),
"it-IT": Language(code="it-IT"),
"ja-JP": Language(code="ja-JP"),
}
with subtransactions(session):
session.add_all(languages.values())
shelves: t.Dict[str, Shelf] = {
"Shelf A": Shelf(shelf="Shelf A"),
"Shelf B": Shelf(shelf="Shelf B"),
}
with subtransactions(session):
session.add_all(shelves.values())
books: t.Dict[str, Book] = {
"001": Book(
isbn="001",
title="It",
description="Stephens Kings It",
publisher=publishers["Oxford Press"],
tags=["a", "b", "c"],
doc={
"i": 73,
"bool": True,
"firstname": "Glenda",
"lastname": "Judye",
"nick_names": [
"Beatriz",
"Jean",
"Carilyn",
"Carol-Jean",
"Sara-Ann",
],
"coordinates": {"lat": 21.1, "lon": 32.9},
"a": {"b": {"c": [0, 1, 2, 3, 4]}},
"x": [{"y": 0, "z": 5}, {"y": 1, "z": 6}],
"generation": {"name": "X"},
},
publish_date=datetime.datetime(1980, 1, 1),
),
"002": Book(
isbn="002",
title="The Body",
description="Lodsdcsdrem ipsum dodscdslor sit amet",
publisher=publishers["Oxford Press"],
tags=["d", "e", "f"],
doc={
"i": 99,
"bool": False,
"firstname": "Jack",
"lastname": "Jones",
"nick_names": [
"Jack",
"Jones",
"Jay",
"Jay-Jay",
"Jackie",
],
"coordinates": {"lat": 25.1, "lon": 52.2},
"a": {"b": {"c": [2, 3, 4, 5, 6]}},
"x": [{"y": 2, "z": 3}, {"y": 7, "z": 2}],
"generation": {"name": "X"},
},
publish_date="infinity",
),
"003": Book(
isbn="003",
title="Harry Potter and the Sorcerer's Stone",
description="Harry Potter has never been",
publisher=publishers["Penguin Books"],
tags=["g", "h", "i"],
doc={
"i": 13,
"bool": True,
"firstname": "Mary",
"lastname": "Jane",
"nick_names": [
"Mariane",
"May",
"Maey",
"M-Jane",
"Jane",
],
"coordinates": {"lat": 24.9, "lon": 93.2},
"a": {"b": {"c": [9, 8, 7, 6, 5]}},
"x": [{"y": 3, "z": 5}, {"y": 8, "z": 2}],
"generation": {"name": "X"},
},
publish_date="-infinity",
),
"004": Book(
isbn="004",
title="Harry Potter and the Chamber of Secrets",
description="The Dursleys were so mean and hideous that summer "
"that all Harry Potter wanted was to get back to the "
"Hogwarts School for Witchcraft and Wizardry",
publisher=publishers["Penguin Books"],
tags=["j", "k", "l"],
doc={
"i": 43,
"bool": False,
"firstname": "Kermit",
"lastname": "Frog",
"nick_names": ["Kermit", "Frog", "Ker", "Boss", "K"],
"coordinates": {"lat": 22.7, "lon": 35.2},
"a": {"b": {"c": [9, 1, 2, 8, 4]}},
"x": [{"y": 3, "z": 2}, {"y": 9, "z": 2}],
"generation": {"name": "Z"},
},
),
"005": Book(
isbn="005",
title="The 17th Suspect",
description="A series of shootings exposes San Francisco to a "
"methodical yet unpredictable killer, and a reluctant "
"woman decides to put her trust in Sergeant Lindsay "
"Boxer",
publisher=publishers["Pearson Press"],
tags=["m", "n", "o"],
),
"006": Book(
isbn="006",
title="The President Is Missing",
description="The publishing event of 2018: Bill Clinton and James "
"Patterson's The President Is Missing is a "
"superlative thriller",
publisher=publishers["Pearson Press"],
),
"007": Book(
isbn="007",
title="Say You're Sorry",
description="deserunt mollit anim id est laborum",
publisher=publishers["Reutgers Press"],
),
"008": Book(
isbn="008",
title="Bones Don't Lie",
description="Lorem ipsum",
publisher=publishers["Reutgers Press"],
),
}
with subtransactions(session):
session.add_all(books.values())
ratings: t.List[Rating] = [
Rating(value=1.1, book=books["001"]),
Rating(value=2.1, book=books["002"]),
Rating(value=3.1, book=books["003"]),
Rating(value=4.1, book=books["004"]),
Rating(value=5.1, book=books["005"]),
Rating(value=6.1, book=books["006"]),
Rating(value=7.1, book=books["007"]),
Rating(value=8.1, book=books["008"]),
]
with subtransactions(session):
session.add_all(ratings)
book_authors: t.List[BookAuthor] = [
BookAuthor(book=books["001"], author=authors["Stephen King"]),
BookAuthor(book=books["002"], author=authors["Stephen King"]),
BookAuthor(book=books["003"], author=authors["J. K. Rowling"]),
BookAuthor(book=books["004"], author=authors["J. K. Rowling"]),
BookAuthor(
book=books["005"], author=authors["James Patterson"]
),
BookAuthor(
book=books["006"], author=authors["James Patterson"]
),
BookAuthor(book=books["007"], author=authors["Melinda Leigh"]),
BookAuthor(book=books["008"], author=authors["Melinda Leigh"]),
BookAuthor(book=books["007"], author=authors["Tolu Aina"]),
BookAuthor(book=books["008"], author=authors["Tolu Aina"]),
]
with subtransactions(session):
session.add_all(book_authors)
book_subjects: t.List[BookSubject] = [
BookSubject(book=books["001"], subject=subjects["Literature"]),
BookSubject(book=books["002"], subject=subjects["Literature"]),
BookSubject(book=books["003"], subject=subjects["Poetry"]),
BookSubject(book=books["004"], subject=subjects["Poetry"]),
BookSubject(book=books["005"], subject=subjects["Romance"]),
BookSubject(book=books["006"], subject=subjects["Romance"]),
BookSubject(
book=books["007"],
subject=subjects["Science Fiction & Fantasy"],
),
BookSubject(
book=books["008"],
subject=subjects["Science Fiction & Fantasy"],
),
]
with subtransactions(session):
session.add_all(book_subjects)
book_languages: t.List[BookLanguage] = [
BookLanguage(book=books["001"], language=languages["en-GB"]),
BookLanguage(book=books["002"], language=languages["en-GB"]),
BookLanguage(book=books["003"], language=languages["en-GB"]),
BookLanguage(book=books["004"], language=languages["en-GB"]),
BookLanguage(book=books["005"], language=languages["en-GB"]),
BookLanguage(book=books["006"], language=languages["en-GB"]),
BookLanguage(book=books["007"], language=languages["en-GB"]),
BookLanguage(book=books["008"], language=languages["en-GB"]),
BookLanguage(book=books["001"], language=languages["fr-FR"]),
BookLanguage(book=books["002"], language=languages["fr-FR"]),
BookLanguage(book=books["003"], language=languages["fr-FR"]),
BookLanguage(book=books["004"], language=languages["fr-FR"]),
BookLanguage(book=books["005"], language=languages["fr-FR"]),
BookLanguage(book=books["006"], language=languages["fr-FR"]),
BookLanguage(book=books["007"], language=languages["fr-FR"]),
BookLanguage(book=books["008"], language=languages["fr-FR"]),
]
with subtransactions(session):
session.add_all(book_languages)
book_shelves: t.List[BookShelf] = [
BookShelf(book=books["001"], shelf=shelves["Shelf A"]),
BookShelf(book=books["001"], shelf=shelves["Shelf B"]),
BookShelf(book=books["002"], shelf=shelves["Shelf A"]),
BookShelf(book=books["002"], shelf=shelves["Shelf B"]),
BookShelf(book=books["003"], shelf=shelves["Shelf A"]),
BookShelf(book=books["004"], shelf=shelves["Shelf A"]),
BookShelf(book=books["005"], shelf=shelves["Shelf A"]),
BookShelf(book=books["006"], shelf=shelves["Shelf A"]),
BookShelf(book=books["007"], shelf=shelves["Shelf A"]),
BookShelf(book=books["008"], shelf=shelves["Shelf A"]),
]
with subtransactions(session):
session.add_all(book_shelves)
# Dummy data
if nsize > 1:
nsamples = int(nsize)
faker = Faker()
print("Adding {} books".format(nsamples))
for _ in range(nsamples):
book = Book(
isbn=faker.isbn13(),
title=faker.sentence(),
description=faker.text(),
publisher=random.choice(list(publishers.values())),
)
session.add(book)
author = Author(
name="{} .{} {}".format(
faker.first_name(),
faker.random_letter(),
faker.last_name(),
),
date_of_birth=faker.date_object(),
city=random.choice(list(cities.values())),
)
session.add(author)
book_author = BookAuthor(book=book, author=author)
session.add(book_author)
book_subject = BookSubject(
book=book,
subject=random.choice(list(subjects.values())),
)
session.add(book_subject)
book_languge = BookLanguage(
book=book,
language=random.choice(list(languages.values())),
)
session.add(book_languge)
book_shelf = BookShelf(
book=book,
shelf=random.choice(list(shelves.values())),
)
session.add(book_shelf)
session.commit()
if __name__ == "__main__":
main()