Skip to content

Commit ac9079d

Browse files
eyrei123martin-martinlpozo
authored
TinyDB Materials (#731)
* Initial Commit * update after isort * Update pyproject.toml file * Update README * Ruff format * Post TR2 -Attempt 1 * Post TR2 Attempt 3 * Post TR2 Attempt 4 * Post TR2 Attempt 5 * Post TR2 Attempt 6 * Restore folders accidentally removed during merge * Move restored dirs into parent folder * Update README --------- Co-authored-by: martin-martin <breuss.martin@gmail.com> Co-authored-by: Martin Breuss <martin-martin@users.noreply.github.com> Co-authored-by: Leodanis Pozo Ramos <lpozor78@gmail.com>
1 parent e1a0748 commit ac9079d

File tree

9 files changed

+193
-0
lines changed

9 files changed

+193
-0
lines changed

tinydb/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TinyDB: A Lightweight JSON Database for Small Projects
2+
3+
This repository contains the downloadable code for the Real Python tutorial: [TinyDB: A Lightweight JSON
4+
Database for Small Projects](https://realpython.com/tinydb-python/)
5+
6+
## Contents
7+
8+
- `create_db.py` — Code from the **CREATE** section of the tutorial
9+
- `read.py` — Code from the **READ** section of the tutorial
10+
- `update_db.py` — Code from the **UPDATE** section of the tutorial
11+
- `update_db_v2.py` — Additional update code from the **UPDATE** section
12+
- `update_db_v3.py` — Additional update code from the **UPDATE** section
13+
- `delete.py` — Code from the **DELETE** section of the tutorial
14+
- `countries_file.csv` — CSV data used to create TinyDB documents
15+
- `ten_countries.json` — Sample JSON file containing data for ten countries

tinydb/countries_file.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
location,population,continent
2+
Argentina,45929925,South America
3+
Switzerland,8990428,Europe
4+
Mozambique,36147236,Africa

tinydb/create_db.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from csv import DictReader
2+
3+
from tinydb import TinyDB
4+
5+
with TinyDB("countries.json", indent=4) as countries_db:
6+
countries_table = countries_db.table(name="countries")
7+
8+
countries_table.insert({"location": "Vatican City", "population": 501})
9+
10+
countries_table.insert_multiple(
11+
[
12+
{"location": "India", "population": 1_417_492_000},
13+
{"location": "China", "population": 1_408_280_000},
14+
]
15+
)
16+
17+
with open("countries_file.csv", "r") as csv_source:
18+
reader = DictReader(csv_source)
19+
20+
for row in reader:
21+
row["population"] = int(row["population"])
22+
countries_table.insert(row)

tinydb/delete.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# REPL code
2+
3+
from tinydb import TinyDB, where
4+
5+
countries_db = TinyDB("ten_countries.json")
6+
countries_table = countries_db.table(name="countries")
7+
len(countries_table)
8+
9+
countries_table.remove(doc_ids=[3, 5, 7])
10+
11+
len(countries_table)
12+
13+
countries_table.remove(where("population") < 300_000_000)
14+
len(countries_table)
15+
16+
countries_table.truncate()
17+
len(countries_table)
18+
19+
countries_db.tables()
20+
21+
countries_db.drop_table(name="countries")
22+
countries_db.tables()

tinydb/read.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# REPL Code
2+
3+
from pprint import pprint
4+
5+
from tinydb import Query, TinyDB
6+
7+
countries_db = TinyDB("ten_countries.json")
8+
countries_table = countries_db.table(name="countries")
9+
query = Query()
10+
query_def = (query.population > 220_000_000) & (query.population < 250_000_000)
11+
pprint(countries_table.search(query_def))
12+
pprint(countries_table.search(query_def))
13+
pprint(countries_table.search(query["% of world"] >= 17))
14+
pprint(countries_table.get(doc_ids=[9, 10]))
15+
countries_db.close()

tinydb/ten_countries.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"countries": {
3+
"1": {
4+
"location": "India",
5+
"population": 1417492000,
6+
"% of world": 17.3,
7+
"date": "1 Jul 2025",
8+
"source": "Official projection"
9+
},
10+
"2": {
11+
"location": "China",
12+
"population": 1408280000,
13+
"% of world": 17.2,
14+
"date": "31 Dec 2024",
15+
"source": "Official estimate"
16+
},
17+
"3": {
18+
"location": "United States",
19+
"population": 340110988,
20+
"% of world": 4.1,
21+
"date": "1 Jul 2024",
22+
"source": "Official estimate"
23+
},
24+
"4": {
25+
"location": "Indonesia",
26+
"population": 284438782,
27+
"% of world": 3.5,
28+
"date": "30 Jun 2025",
29+
"source": "National annual projection"
30+
},
31+
"5": {
32+
"location": "Pakistan",
33+
"population": 241499431,
34+
"% of world": 2.9,
35+
"date": "1 Mar 2023",
36+
"source": "2023 Census result"
37+
},
38+
"6": {
39+
"location": "Nigeria",
40+
"population": 223800000,
41+
"% of world": 2.7,
42+
"date": "1 Jul 2023",
43+
"source": "Official projection"
44+
},
45+
"7": {
46+
"location": "Brazil",
47+
"population": 213421037,
48+
"% of world": 2.6,
49+
"date": "1 Jul 2025",
50+
"source": "Unknown"
51+
},
52+
"8": {
53+
"location": "Bangladesh",
54+
"population": 169828911,
55+
"% of world": 2.1,
56+
"date": "14 Jun 2022",
57+
"source": "2022 Census result"
58+
},
59+
"9": {
60+
"location": "Russia",
61+
"population": 146028325,
62+
"% of world": 1.8,
63+
"date": "1 Jan 2025",
64+
"source": "???"
65+
},
66+
"10": {
67+
"location": "Mexico",
68+
"population": 0,
69+
"% of world": 1.6,
70+
"date": "30 Jun 2025",
71+
"source": "???"
72+
}
73+
}
74+
}

tinydb/update_db.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from tinydb import TinyDB, where
2+
3+
with TinyDB("ten_countries.json") as countries_db:
4+
countries_table = countries_db.table(name="countries")
5+
6+
countries_table.update(
7+
{"population": 130_575_786}, where("location") == "Mexico"
8+
)
9+
10+
countries_table.update(
11+
{"source": "National quarterly update"},
12+
where("location") == "Mexico",
13+
)
14+
15+
# REPL code.
16+
# from pprint import pprint
17+
# from tinydb import TinyDB, where
18+
# with TinyDB("ten_countries.json") as countries_db:
19+
# countries_table = countries_db.table(name="countries")
20+
# pprint(countries_table.search(where("location") == "Mexico"))

tinydb/update_db_v2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from tinydb import TinyDB, where
2+
3+
with TinyDB("ten_countries.json") as countries_db:
4+
countries_table = countries_db.table(name="countries")
5+
countries_table.update_multiple(
6+
[
7+
(
8+
{"population": 130_575_786},
9+
where("location") == "Mexico",
10+
),
11+
(
12+
{"source": "National quarterly update"},
13+
where("location") == "Mexico",
14+
),
15+
]
16+
)

tinydb/update_db_v3.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from tinydb import TinyDB
2+
3+
with TinyDB("ten_countries.json") as countries_db:
4+
countries_table = countries_db.table(name="countries")
5+
countries_table.update({"source": "Official estimate"}, doc_ids=[7, 9])

0 commit comments

Comments
 (0)