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
15 changes: 15 additions & 0 deletions tinydb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# TinyDB: A Lightweight JSON Database for Small Projects

This repository contains the downloadable code for the Real Python tutorial: [TinyDB: A Lightweight JSON
Database for Small Projects](https://realpython.com/tinydb-python/)

## Contents

- `create_db.py` — Code from the **CREATE** section of the tutorial
- `read.py` — Code from the **READ** section of the tutorial
- `update_db.py` — Code from the **UPDATE** section of the tutorial
- `update_db_v2.py` — Additional update code from the **UPDATE** section
- `update_db_v3.py` — Additional update code from the **UPDATE** section
- `delete.py` — Code from the **DELETE** section of the tutorial
- `countries_file.csv` — CSV data used to create TinyDB documents
- `ten_countries.json` — Sample JSON file containing data for ten countries
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
22 changes: 22 additions & 0 deletions tinydb/create_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from csv import DictReader

from tinydb import TinyDB

with TinyDB("countries.json", indent=4) as countries_db:
countries_table = countries_db.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:
row["population"] = int(row["population"])
countries_table.insert(row)
22 changes: 22 additions & 0 deletions tinydb/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# REPL code

from tinydb import TinyDB, where

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

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

len(countries_table)

countries_table.remove(where("population") < 300_000_000)
len(countries_table)

countries_table.truncate()
len(countries_table)

countries_db.tables()

countries_db.drop_table(name="countries")
countries_db.tables()
15 changes: 15 additions & 0 deletions tinydb/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# REPL Code

from pprint import pprint

from tinydb import Query, TinyDB

countries_db = TinyDB("ten_countries.json")
countries_table = countries_db.table(name="countries")
query = Query()
query_def = (query.population > 220_000_000) & (query.population < 250_000_000)
pprint(countries_table.search(query_def))
pprint(countries_table.search(query_def))
pprint(countries_table.search(query["% of world"] >= 17))
pprint(countries_table.get(doc_ids=[9, 10]))
countries_db.close()
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": "???"
}
}
}
20 changes: 20 additions & 0 deletions tinydb/update_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from tinydb import TinyDB, where

with TinyDB("ten_countries.json") as countries_db:
countries_table = countries_db.table(name="countries")

countries_table.update(
{"population": 130_575_786}, where("location") == "Mexico"
)

countries_table.update(
{"source": "National quarterly update"},
where("location") == "Mexico",
)

# REPL code.
# from pprint import pprint
# from tinydb import TinyDB, where
# with TinyDB("ten_countries.json") as countries_db:
# countries_table = countries_db.table(name="countries")
# pprint(countries_table.search(where("location") == "Mexico"))
16 changes: 16 additions & 0 deletions tinydb/update_db_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from tinydb import TinyDB, where

with TinyDB("ten_countries.json") as countries_db:
countries_table = countries_db.table(name="countries")
countries_table.update_multiple(
[
(
{"population": 130_575_786},
where("location") == "Mexico",
),
(
{"source": "National quarterly update"},
where("location") == "Mexico",
),
]
)
5 changes: 5 additions & 0 deletions tinydb/update_db_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from tinydb import TinyDB

with TinyDB("ten_countries.json") as countries_db:
countries_table = countries_db.table(name="countries")
countries_table.update({"source": "Official estimate"}, doc_ids=[7, 9])