File tree Expand file tree Collapse file tree 9 files changed +70
-76
lines changed
Expand file tree Collapse file tree 9 files changed +70
-76
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11from csv import DictReader
2- from pprint import pprint
32
43from 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 }
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 ]))
Original file line number Diff line number Diff line change 1+ # REPL code
2+
13from tinydb import TinyDB , where
24
35countries_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
1719countries_db .tables ()
1820
1921countries_db .drop_table (name = "countries" )
20-
2122countries_db .tables ()
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ # REPL Code
2+
13from pprint import pprint
24
35from tinydb import Query , TinyDB
46
57countries_db = TinyDB ("ten_countries.json" )
6- countries_tb = countries_db .table (name = "countries" )
7-
8+ countries_table = countries_db .table (name = "countries" )
89query = Query ()
910query_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 ()
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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"))
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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 ])
You can’t perform that action at this time.
0 commit comments