Skip to content

Commit 51fb932

Browse files
committed
Post TR2 -Attempt 1
1 parent 497360e commit 51fb932

File tree

9 files changed

+70
-76
lines changed

9 files changed

+70
-76
lines changed

tinydb/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ This file contains the downloadable code from the RealPython tutorial: \[TinyDB:
44

55

66

7-
create.py - File containing all code from the CREATE section of the tutorial.
7+
create\_db.py - SCRIPT file containing code from the CREATE section of the tutorial.
88

9-
read.py - File containing all code from the READ section of the tutorial.
9+
read.py - REPL code file containing code from the READ section of the tutorial.
1010

11-
update.py - File containing all code from the UPDATE section of the tutorial.
11+
update\_db.py - SCRIPT file containing code from the UPDATE section of the tutorial.
1212

13-
delete.py - File containing all code from the DELETE section of the tutorial.
13+
update\_db\_v2.py - SCRIPT file containing code from the UPDATE section of the tutorial.
14+
15+
update\_db\_v3.py - SCRIPT file containing code from the UPDATE section of the tutorial.
16+
17+
delete.py - REPL code file containing code from the DELETE section of the tutorial.
1418

1519

1620

tinydb/create.py renamed to tinydb/create_db.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from csv import DictReader
2-
from pprint import pprint
32

43
from tinydb import TinyDB
54

6-
with TinyDB("countries.json", indent=4) as countries:
7-
countries_table = countries.table(name="countries")
5+
with TinyDB("countries.json", indent=4) as countries_db:
6+
countries_table = countries_db.table(name="countries")
87

98
countries_table.insert(
109
{"location": "Vatican City", "population": 501}
@@ -21,6 +20,6 @@
2120
reader = DictReader(csv_source)
2221

2322
for row in reader:
23+
row["population"] = int(row["population"])
2424
countries_table.insert(row)
2525

26-
pprint(countries_table.get(doc_ids=[3, 4]))

tinydb/delete.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
# REPL code
2+
13
from tinydb import TinyDB, where
24

35
countries_db = TinyDB("ten_countries.json")
4-
countries_tb = countries_db.table(name="countries")
5-
6-
len(countries_tb)
6+
countries_table = countries_db.table(name="countries")
7+
len(countries_table)
78

8-
countries_tb.remove(doc_ids=[3, 5, 7])
9+
countries_table.remove(doc_ids=[3, 5, 7])
910

10-
len(countries_tb)
11+
len(countries_table)
1112

12-
countries_tb.remove(where("population") < 300_000_000)
13+
countries_table.remove(where("population") < 300_000_000)
14+
len(countries_table)
1315

14-
countries_tb.truncate()
15-
len(countries_tb)
16+
countries_table.truncate()
17+
len(countries_table)
1618

1719
countries_db.tables()
1820

1921
countries_db.drop_table(name="countries")
20-
2122
countries_db.tables()

tinydb/pyproject.toml

Lines changed: 0 additions & 21 deletions
This file was deleted.

tinydb/read.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
# REPL Code
2+
13
from pprint import pprint
24

35
from tinydb import Query, TinyDB
46

57
countries_db = TinyDB("ten_countries.json")
6-
countries_tb = countries_db.table(name="countries")
7-
8+
countries_table = countries_db.table(name="countries")
89
query = Query()
910
query_def = (query.population > 220_000_000) & (
1011
query.population < 250_000_000
1112
)
12-
13-
pprint(countries_tb.search(query_def))
14-
15-
pprint(countries_tb.search(query["% of world"] >= 17))
13+
pprint(countries_table.search(query_def))
14+
pprint(countries_table.search(query_def))
15+
pprint(countries_table.search(query["% of world"] >= 17))
16+
pprint(countries_table.get(doc_ids=[9, 10]))
17+
countries_db.close()

tinydb/update.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

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)