Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tinydb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This file contains the downloadable code from the RealPython tutorial: \[TinyDB: A Lightweight JSON Database for Small Projects](https://realpython.com/tinydb-lightweight-json-database-small-projects/)





create.py - File containing all code from the CREATE section of the tutorial.

read.py - File containing all code from the READ section of the tutorial.

update.py - File containing all code from the UPDATE section of the tutorial.

delete.py - File containing all code from the DELETE section of the tutorial.



countries\_file.csv - File containing csv data to be converted to TinyDB documents.

ten\_countries.json - File containing an existing JSON object representing ten TinyDB records in a countries table.



README.md - This file

4 changes: 4 additions & 0 deletions tinydb/countries_file.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
location,population,continent
Argentina,45929925,South America
Switzerland,8990428,Europe
Mozambique,36147236,Africa
26 changes: 26 additions & 0 deletions tinydb/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from csv import DictReader
from pprint import pprint

from tinydb import TinyDB

with TinyDB("countries.json", indent=4) as countries:
countries_table = countries.table(name="countries")

countries_table.insert(
{"location": "Vatican City", "population": 501}
)

countries_table.insert_multiple(
[
{"location": "India", "population": 1_417_492_000},
{"location": "China", "population": 1_408_280_000},
]
)

with open("countries_file.csv", "r") as csv_source:
reader = DictReader(csv_source)

for row in reader:
countries_table.insert(row)

pprint(countries_table.get(doc_ids=[3, 4]))
21 changes: 21 additions & 0 deletions tinydb/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from tinydb import TinyDB, where

countries_db = TinyDB("ten_countries.json")
countries_tb = countries_db.table(name="countries")

len(countries_tb)

countries_tb.remove(doc_ids=[3, 5, 7])

len(countries_tb)

countries_tb.remove(where("population") < 300_000_000)

countries_tb.truncate()
len(countries_tb)

countries_db.tables()

countries_db.drop_table(name="countries")

countries_db.tables()
21 changes: 21 additions & 0 deletions tinydb/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tool.black]
line-length = 70
target-version = ["py312"]
exclude = '''
/(
\.git
| venv
| migrations
| node_modules
)/
'''


[tool.ruff]
target-version = "py312"
exclude = [".git", "venv", "migrations", "node_modules"]
line-length = 70

[tool.ruff.lint]
select = ["E", "F", "I", "RUF100"]
ignore = ["E501"] # Line length is handled by Black
15 changes: 15 additions & 0 deletions tinydb/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pprint import pprint

from tinydb import Query, TinyDB

countries_db = TinyDB("ten_countries.json")
countries_tb = countries_db.table(name="countries")

query = Query()
query_def = (query.population > 220_000_000) & (
query.population < 250_000_000
)

pprint(countries_tb.search(query_def))

pprint(countries_tb.search(query["% of world"] >= 17))
74 changes: 74 additions & 0 deletions tinydb/ten_countries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"countries": {
"1": {
"location": "India",
"population": 1417492000,
"% of world": 17.3,
"date": "1 Jul 2025",
"source": "Official projection"
},
"2": {
"location": "China",
"population": 1408280000,
"% of world": 17.2,
"date": "31 Dec 2024",
"source": "Official estimate"
},
"3": {
"location": "United States",
"population": 340110988,
"% of world": 4.1,
"date": "1 Jul 2024",
"source": "Official estimate"
},
"4": {
"location": "Indonesia",
"population": 284438782,
"% of world": 3.5,
"date": "30 Jun 2025",
"source": "National annual projection"
},
"5": {
"location": "Pakistan",
"population": 241499431,
"% of world": 2.9,
"date": "1 Mar 2023",
"source": "2023 Census result"
},
"6": {
"location": "Nigeria",
"population": 223800000,
"% of world": 2.7,
"date": "1 Jul 2023",
"source": "Official projection"
},
"7": {
"location": "Brazil",
"population": 213421037,
"% of world": 2.6,
"date": "1 Jul 2025",
"source": "Unknown"
},
"8": {
"location": "Bangladesh",
"population": 169828911,
"% of world": 2.1,
"date": "14 Jun 2022",
"source": "2022 Census result"
},
"9": {
"location": "Russia",
"population": 146028325,
"% of world": 1.8,
"date": "1 Jan 2025",
"source": "???"
},
"10": {
"location": "Mexico",
"population": 0,
"% of world": 1.6,
"date": "30 Jun 2025",
"source": "???"
}
}
}
32 changes: 32 additions & 0 deletions tinydb/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pprint import pprint

from tinydb import TinyDB, where

with TinyDB("ten_countries.json") as countries_db:
countries_tb = countries_db.table(name="countries")
countries_tb.update(
{"population": 130_575_786},
where("location") == "Mexico",
)
countries_tb.update(
{"source": "National quarterly update"},
where("location") == "Mexico",
)
pprint(countries_tb.search(where("location") == "Mexico"))

countries_tb.update_multiple(
[
(
{"population": 130_575_786},
where("location") == "Mexico",
),
(
{"source": "National quarterly update"},
where("location") == "Mexico",
),
]
)

countries_tb.update(
{"source": "Official estimate"}, doc_ids=[7, 9]
)