diff --git a/tinydb/README.md b/tinydb/README.md new file mode 100644 index 0000000000..60f9e332c7 --- /dev/null +++ b/tinydb/README.md @@ -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 diff --git a/tinydb/countries_file.csv b/tinydb/countries_file.csv new file mode 100644 index 0000000000..99d7b3fcc7 --- /dev/null +++ b/tinydb/countries_file.csv @@ -0,0 +1,4 @@ +location,population,continent +Argentina,45929925,South America +Switzerland,8990428,Europe +Mozambique,36147236,Africa diff --git a/tinydb/create_db.py b/tinydb/create_db.py new file mode 100644 index 0000000000..8674508eb4 --- /dev/null +++ b/tinydb/create_db.py @@ -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) diff --git a/tinydb/delete.py b/tinydb/delete.py new file mode 100644 index 0000000000..32b5db167d --- /dev/null +++ b/tinydb/delete.py @@ -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() diff --git a/tinydb/read.py b/tinydb/read.py new file mode 100644 index 0000000000..1616edc30c --- /dev/null +++ b/tinydb/read.py @@ -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() diff --git a/tinydb/ten_countries.json b/tinydb/ten_countries.json new file mode 100644 index 0000000000..09096bf005 --- /dev/null +++ b/tinydb/ten_countries.json @@ -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": "???" + } + } +} \ No newline at end of file diff --git a/tinydb/update_db.py b/tinydb/update_db.py new file mode 100644 index 0000000000..c70e67268a --- /dev/null +++ b/tinydb/update_db.py @@ -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")) diff --git a/tinydb/update_db_v2.py b/tinydb/update_db_v2.py new file mode 100644 index 0000000000..d555497c77 --- /dev/null +++ b/tinydb/update_db_v2.py @@ -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", + ), + ] + ) diff --git a/tinydb/update_db_v3.py b/tinydb/update_db_v3.py new file mode 100644 index 0000000000..32311bfcb0 --- /dev/null +++ b/tinydb/update_db_v3.py @@ -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])