diff --git a/tinydb/README.md b/tinydb/README.md new file mode 100644 index 0000000000..95c5f61879 --- /dev/null +++ b/tinydb/README.md @@ -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 + 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.py b/tinydb/create.py new file mode 100644 index 0000000000..497b4c6c4a --- /dev/null +++ b/tinydb/create.py @@ -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])) diff --git a/tinydb/delete.py b/tinydb/delete.py new file mode 100644 index 0000000000..e97ab51890 --- /dev/null +++ b/tinydb/delete.py @@ -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() diff --git a/tinydb/pyproject.toml b/tinydb/pyproject.toml new file mode 100644 index 0000000000..b0e704544f --- /dev/null +++ b/tinydb/pyproject.toml @@ -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 \ No newline at end of file diff --git a/tinydb/read.py b/tinydb/read.py new file mode 100644 index 0000000000..b5aa007e9c --- /dev/null +++ b/tinydb/read.py @@ -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)) 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.py b/tinydb/update.py new file mode 100644 index 0000000000..68d1729ae9 --- /dev/null +++ b/tinydb/update.py @@ -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] + )