From d30184f5991b29569b60f6ba2d7f412bf3b23215 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 10 May 2021 23:17:03 +0200 Subject: [PATCH 1/9] bpo-44106: Don't use politicians as example database --- Doc/includes/sqlite3/createdb.py | 10 +++++----- Doc/includes/sqlite3/ctx_manager.py | 2 +- Doc/includes/sqlite3/execsql_fetchonerow.py | 10 +++++----- Doc/includes/sqlite3/execsql_printall_1.py | 2 +- Doc/includes/sqlite3/execute_1.py | 13 ++++++------- Doc/includes/sqlite3/insert_more_people.py | 12 ++++++------ Doc/includes/sqlite3/shortcut_methods.py | 16 ++++++++-------- Doc/includes/sqlite3/simple_tableprinter.py | 4 ++-- 8 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Doc/includes/sqlite3/createdb.py b/Doc/includes/sqlite3/createdb.py index ee2950bdf81646..49702121f72534 100644 --- a/Doc/includes/sqlite3/createdb.py +++ b/Doc/includes/sqlite3/createdb.py @@ -12,15 +12,15 @@ con = sqlite3.connect(DB_FILE) cur = con.cursor() cur.execute(""" - create table people + create table lang ( - name_last varchar(20), - age integer + name varchar(20), + first_appeared integer ) """) -cur.execute("insert into people (name_last, age) values ('Yeltsin', 72)") -cur.execute("insert into people (name_last, age) values ('Putin', 51)") +cur.execute("insert into lang (name, first_appeared) values ('Forth', 1970)") +cur.execute("insert into lang (name, first_appeared) values ('Ada', 1980)") con.commit() diff --git a/Doc/includes/sqlite3/ctx_manager.py b/Doc/includes/sqlite3/ctx_manager.py index 6db77d45046e1f..ad65eb4dc5d84c 100644 --- a/Doc/includes/sqlite3/ctx_manager.py +++ b/Doc/includes/sqlite3/ctx_manager.py @@ -1,7 +1,7 @@ import sqlite3 con = sqlite3.connect(":memory:") -con.execute("create table person (id integer primary key, firstname varchar unique)") +con.execute("create table lang (id integer primary key, firstname varchar unique)") # Successful, con.commit() is called automatically afterwards with con: diff --git a/Doc/includes/sqlite3/execsql_fetchonerow.py b/Doc/includes/sqlite3/execsql_fetchonerow.py index 115bcb50c7c754..a4f50279753abb 100644 --- a/Doc/includes/sqlite3/execsql_fetchonerow.py +++ b/Doc/includes/sqlite3/execsql_fetchonerow.py @@ -3,17 +3,17 @@ con = sqlite3.connect("mydb") cur = con.cursor() -SELECT = "select name_last, age from people order by age, name_last" +SELECT = "select name, first_appeared from people order by first_appeared, name" # 1. Iterate over the rows available from the cursor, unpacking the -# resulting sequences to yield their elements (name_last, age): +# resulting sequences to yield their elements (name, first_appeared): cur.execute(SELECT) -for (name_last, age) in cur: - print('%s is %d years old.' % (name_last, age)) +for (name, first_appeared) in cur: + print(f"The {name} programming language appeared in {first_appeared}.") # 2. Equivalently: cur.execute(SELECT) for row in cur: - print('%s is %d years old.' % (row[0], row[1])) + print(f"The {row[0]} programming language appeared in {row[1]}.") con.close() diff --git a/Doc/includes/sqlite3/execsql_printall_1.py b/Doc/includes/sqlite3/execsql_printall_1.py index 19306e6e3ca7d1..b3b42b5567df3b 100644 --- a/Doc/includes/sqlite3/execsql_printall_1.py +++ b/Doc/includes/sqlite3/execsql_printall_1.py @@ -7,7 +7,7 @@ cur = con.cursor() # Execute the SELECT statement: -cur.execute("select * from people order by age") +cur.execute("select * from lang order by first_appeared") # Retrieve all rows as a sequence and print that sequence: print(cur.fetchall()) diff --git a/Doc/includes/sqlite3/execute_1.py b/Doc/includes/sqlite3/execute_1.py index 42aad4d5839f06..ee0000e2b94a32 100644 --- a/Doc/includes/sqlite3/execute_1.py +++ b/Doc/includes/sqlite3/execute_1.py @@ -2,22 +2,21 @@ con = sqlite3.connect(":memory:") cur = con.cursor() -cur.execute("create table lang (lang_name, lang_age)") +cur.execute("create table lang (name, first_appeared)") # This is the qmark style: -cur.execute("insert into lang values (?, ?)", ("C", 49)) +cur.execute("insert into lang values (?, ?)", ("C", 1972)) # The qmark style used with executemany(): lang_list = [ - ("Fortran", 64), - ("Python", 30), - ("Go", 11), + ("Fortran", 1957), + ("Python", 1991), + ("Go", 2009), ] cur.executemany("insert into lang values (?, ?)", lang_list) # And this is the named style: -cur.execute("select * from lang where lang_name=:name and lang_age=:age", - {"name": "C", "age": 49}) +cur.execute("select * from lang where first_appeared=:year", {"year": 1972}) print(cur.fetchall()) con.close() diff --git a/Doc/includes/sqlite3/insert_more_people.py b/Doc/includes/sqlite3/insert_more_people.py index 10cf937243f6da..ceef949608449e 100644 --- a/Doc/includes/sqlite3/insert_more_people.py +++ b/Doc/includes/sqlite3/insert_more_people.py @@ -4,13 +4,13 @@ cur = con.cursor() -newPeople = ( - ('Lebed' , 53), - ('Zhirinovsky' , 57), - ) +languages = ( + ("Smalltalk", 1972), + ("Swift", 2014), +) -for person in newPeople: - cur.execute("insert into people (name_last, age) values (?, ?)", person) +for lang in languages: + cur.execute("insert into lang (name, first_appeared) values (?, ?)", lang) # The changes will not be saved unless the transaction is committed explicitly: con.commit() diff --git a/Doc/includes/sqlite3/shortcut_methods.py b/Doc/includes/sqlite3/shortcut_methods.py index 98a39411495cba..48ea6fad15a898 100644 --- a/Doc/includes/sqlite3/shortcut_methods.py +++ b/Doc/includes/sqlite3/shortcut_methods.py @@ -1,23 +1,23 @@ import sqlite3 -persons = [ - ("Hugo", "Boss"), - ("Calvin", "Klein") - ] +langs = [ + ("C++", 1985), + ("Objective-C", 1984), +] con = sqlite3.connect(":memory:") # Create the table -con.execute("create table person(firstname, lastname)") +con.execute("create table lang(name, first_appeared)") # Fill the table -con.executemany("insert into person(firstname, lastname) values (?, ?)", persons) +con.executemany("insert into lang(name, first_appeared) values (?, ?)", langs) # Print the table contents -for row in con.execute("select firstname, lastname from person"): +for row in con.execute("select name, first_appeared from lang"): print(row) -print("I just deleted", con.execute("delete from person").rowcount, "rows") +print("I just deleted", con.execute("delete from lang").rowcount, "rows") # close is not a shortcut method and it's not called automatically, # so the connection object should be closed manually diff --git a/Doc/includes/sqlite3/simple_tableprinter.py b/Doc/includes/sqlite3/simple_tableprinter.py index 148a1707f948bc..d735aee83f6c2b 100644 --- a/Doc/includes/sqlite3/simple_tableprinter.py +++ b/Doc/includes/sqlite3/simple_tableprinter.py @@ -1,8 +1,8 @@ import sqlite3 FIELD_MAX_WIDTH = 20 -TABLE_NAME = 'people' -SELECT = 'select * from %s order by age, name_last' % TABLE_NAME +TABLE_NAME = 'lang' +SELECT = f'select * from {TABLE_NAME} order by name, first_appeared' con = sqlite3.connect("mydb") From e0db380ac00a20fcbaa5970ddb2e2cc9600db136 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 00:54:18 +0200 Subject: [PATCH 2/9] Add NEWS --- .../next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst diff --git a/Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst b/Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst new file mode 100644 index 00000000000000..fcddbd8f0d9c8e --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst @@ -0,0 +1,2 @@ +In the :mod:`sqlite3` docs, use programming languages instead of politicians +as example database content. From 70da0486a656e23aaf0d9176c5cecf43416b8409 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 01:17:20 +0200 Subject: [PATCH 3/9] Update suspicious rules --- Doc/tools/susp-ignored.csv | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv index d56a2b9fd0bfb9..1fde253feac2fa 100644 --- a/Doc/tools/susp-ignored.csv +++ b/Doc/tools/susp-ignored.csv @@ -209,8 +209,7 @@ library/smtplib,,:port,method must support that as well as a regular host:port library/socket,,::,'5aef:2b::8' library/socket,,:can,"return (can_id, can_dlc, data[:can_dlc])" library/socket,,:len,fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) -library/sqlite3,,:name,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age""," -library/sqlite3,,:age,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age""," +library/sqlite3,,:year,"cur.execute(""select * from lang where first_appeared=:year"", {""year"": 1972})" library/sqlite3,,:memory, library/sqlite3,,:path,"db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)" library/ssl,,:My,"Organizational Unit Name (eg, section) []:My Group" From 8e790187f9b302baf4791466ccdff0de114ee3d2 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 07:21:58 +0200 Subject: [PATCH 4/9] Address review: drop unneeded parens --- Doc/includes/sqlite3/execsql_fetchonerow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/includes/sqlite3/execsql_fetchonerow.py b/Doc/includes/sqlite3/execsql_fetchonerow.py index a4f50279753abb..0ca7e14469760b 100644 --- a/Doc/includes/sqlite3/execsql_fetchonerow.py +++ b/Doc/includes/sqlite3/execsql_fetchonerow.py @@ -8,7 +8,7 @@ # 1. Iterate over the rows available from the cursor, unpacking the # resulting sequences to yield their elements (name, first_appeared): cur.execute(SELECT) -for (name, first_appeared) in cur: +for name, first_appeared in cur: print(f"The {name} programming language appeared in {first_appeared}.") # 2. Equivalently: From 64b992333c1f72b029b7a7d6065630699dbe3764 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 07:23:40 +0200 Subject: [PATCH 5/9] Address review: drop unneeded constants --- Doc/includes/sqlite3/simple_tableprinter.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Doc/includes/sqlite3/simple_tableprinter.py b/Doc/includes/sqlite3/simple_tableprinter.py index d735aee83f6c2b..20a689da6a7362 100644 --- a/Doc/includes/sqlite3/simple_tableprinter.py +++ b/Doc/includes/sqlite3/simple_tableprinter.py @@ -1,13 +1,10 @@ import sqlite3 FIELD_MAX_WIDTH = 20 -TABLE_NAME = 'lang' -SELECT = f'select * from {TABLE_NAME} order by name, first_appeared' con = sqlite3.connect("mydb") - cur = con.cursor() -cur.execute(SELECT) +cur.execute(f"select * from lang order by name, first_appeared") # Print a header. for fieldDesc in cur.description: From 19756010b23504ae5a84c99b49cbfb22483022f3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 07:24:00 +0200 Subject: [PATCH 6/9] Address review: drop NEWS entry --- .../next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst diff --git a/Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst b/Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst deleted file mode 100644 index fcddbd8f0d9c8e..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2021-05-11-00-54-14.bpo-44106.grCmuu.rst +++ /dev/null @@ -1,2 +0,0 @@ -In the :mod:`sqlite3` docs, use programming languages instead of politicians -as example database content. From 174fcef112b190e9f8e4b70de5b5f5692aeb4e4c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 07:25:34 +0200 Subject: [PATCH 7/9] Address review: complete incomplete fix --- Doc/includes/sqlite3/ctx_manager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/includes/sqlite3/ctx_manager.py b/Doc/includes/sqlite3/ctx_manager.py index ad65eb4dc5d84c..2e1175ef44c641 100644 --- a/Doc/includes/sqlite3/ctx_manager.py +++ b/Doc/includes/sqlite3/ctx_manager.py @@ -1,19 +1,19 @@ import sqlite3 con = sqlite3.connect(":memory:") -con.execute("create table lang (id integer primary key, firstname varchar unique)") +con.execute("create table lang (id integer primary key, name varchar unique)") # Successful, con.commit() is called automatically afterwards with con: - con.execute("insert into person(firstname) values (?)", ("Joe",)) + con.execute("insert into lang(name) values (?)", ("Python",)) # con.rollback() is called after the with block finishes with an exception, the # exception is still raised and must be caught try: with con: - con.execute("insert into person(firstname) values (?)", ("Joe",)) + con.execute("insert into lang(name) values (?)", ("Python",)) except sqlite3.IntegrityError: - print("couldn't add Joe twice") + print("couldn't add Python twice") # Connection object used as context manager only commits or rollbacks transactions, # so the connection object should be closed manually From 7429f7af7c21cac9c7a379d11133c6539742111b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 11 May 2021 07:26:29 +0200 Subject: [PATCH 8/9] Address review: fix inconsistent naming --- .../sqlite3/{insert_more_people.py => insert_more_langs.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Doc/includes/sqlite3/{insert_more_people.py => insert_more_langs.py} (100%) diff --git a/Doc/includes/sqlite3/insert_more_people.py b/Doc/includes/sqlite3/insert_more_langs.py similarity index 100% rename from Doc/includes/sqlite3/insert_more_people.py rename to Doc/includes/sqlite3/insert_more_langs.py From 64555247abec6144e794c3b94cf229aca472e3ec Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Wed, 19 May 2021 09:59:43 +0300 Subject: [PATCH 9/9] Update simple_tableprinter.py --- Doc/includes/sqlite3/simple_tableprinter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/includes/sqlite3/simple_tableprinter.py b/Doc/includes/sqlite3/simple_tableprinter.py index 20a689da6a7362..9be6e4f414acd8 100644 --- a/Doc/includes/sqlite3/simple_tableprinter.py +++ b/Doc/includes/sqlite3/simple_tableprinter.py @@ -4,7 +4,7 @@ con = sqlite3.connect("mydb") cur = con.cursor() -cur.execute(f"select * from lang order by name, first_appeared") +cur.execute("select * from lang order by name, first_appeared") # Print a header. for fieldDesc in cur.description: