-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
35 lines (29 loc) · 971 Bytes
/
seed.py
File metadata and controls
35 lines (29 loc) · 971 Bytes
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
import json
import uuid
from pathlib import Path
from db import get_db
NAMES = {
"presidents": "US Presidents",
"country_capitals": "Country Capitals",
"state_capitals": "State Capitals",
}
def seed() -> None:
db = get_db()
example_dir = Path(__file__).parent / "example_set"
for path in sorted(example_dir.glob("*.json")):
name = NAMES.get(path.stem, path.stem.replace("_", " ").title())
raw = json.loads(path.read_text(encoding="utf-8"))
cards = [
{"card_id": str(uuid.uuid4()), "front": c["front"], "back": c["back"]}
for c in raw
]
result = db.decks.update_one(
{"name": name},
{"$setOnInsert": {"name": name, "cards": cards}},
upsert=True,
)
status = "inserted" if result.upserted_id else "already exists"
print(f" {name}: {status} ({len(cards)} cards)")
if __name__ == "__main__":
seed()
print("Done")