-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_readme.py
More file actions
56 lines (46 loc) · 2.17 KB
/
update_readme.py
File metadata and controls
56 lines (46 loc) · 2.17 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
""" Run this script after build_database.py since it needs tils.db """
'''
@File : update_readme.py
@Time : 2024-12-04 22:03:31
@Author : Silverbullet069
@Version : 1.0
@License : MIT License
@Desc : Update README.md to reflect tils.db. Credit goes to Simon Willison for logic and Claude 3.5 Sonnet for comments
'''
import pathlib
import sqlite3
import sys
import re
root = pathlib.Path(__file__).parent.resolve()
# Compiles regular expressions to match the sections in README.md that need to be replaced:
# - toc_re matches the block containing the table of contents.
# - count_re matches the block containing the count of TILs.
# - index_re matches the block containing the index of topics.
toc_re = re.compile(
r"<!\-\- toc starts \-\->.*<!\-\- toc ends \-\->", re.DOTALL)
index_re = re.compile(
r"<!\-\- index starts \-\->.*<!\-\- index ends \-\->", re.DOTALL)
til_count_re = re.compile(
r"<!\-\- til count starts \-\->.*<!\-\- til count ends \-\->", re.DOTALL)
# separate template from code, use .format()
COUNT_TEMPLATE = "<!-- count starts -->{}<!-- count ends -->"
if __name__ == "__main__":
# Load db
db_path = root / "tils.db"
by_topic = {}
with sqlite3.connect(db_path) as conn: # transaction
conn.row_factory = (
sqlite3.Row
)
cursor = conn.cursor()
# create topic list by using GROUP command
topic_count_rows = cursor.execute("SELECT topic, COUNT(*) as count FROM til GROUP BY topic").fetchall()
if len(topic_count_rows) == 0:
assert False, "There is not a single row inside your database! Plase run build_database.py for the first time!"
topic_count = topic_count_rows[0]["count"]
# Group db to create a dict whose keys are topics name
# Traverse db to populate value for each key - an array of indexes with a predefined structure
# Replace current ToC with dict's list of keys, sort by alphabet (if the db was sorted, this is not needed)
# Replace current TIL count with the total number of entries inside tils.db
# Replace current indexes with the dict's list of values, sort by alphabet (if the db was sorted, this is not needed)
pass