Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python - This is might be a silly question but I don't understand the yellow highlights. Please look at my script, how do I go

Python - This is might be a silly question but I don't understand the yellow highlights. Please look at my script, how do I go to the "__main__" section and call the test function that I just created? test_PersonDB(). I assume it should be in line 33 but I don't understand how to call it? Please help

image text in transcribed

image text in transcribedimage text in transcribed

Now, create a new method outside of the class called test_PersonDB() to test load_person() using a context manager: def test_PersonDB(): o Using a "with block with a PersonDB object, attempt to load and print three person records: with PersonDB (people_db_file) as db: print (db.load_person (10000)) # Should print the default print (db.load_person (122)) print (db.load_person (300)) Next, go to the "_main_" section and call test_PersonDBC) to test the method you just created Sample output (your results WILL vary due to the random nature of person creation): (-1,'','') (122, 'ELISA', 'SINGLETON') (300, "GENNY', 'WEBER') import sqlite3 from sqlite3 import Error import random 7 A 8 9 10 11 12 13 14 # PART 1 def generate_people(count): 15 with open('LastNames.txt', 'r') as filehandle: last_names = [line.rstrip() for line in filehandle] with open('FirstNames.txt', 'r') as filehandle: first_names [line.rstrip() for line in filehandle] 16 17 18 19 20 21 22 23 24 people list) for i in range(count): people.append((i, first_names[random.randint(0, len(first_names)-1)], last_names[random.randint(0, len(last_names)-1)])) 25 return people if name "_main_": people generate_people(5) print(people) #PART 2 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 people_db_file = "sqlite.db" # The name of the database file to use max_people = 500 # Number of records to create def create_people_database (db_file, count): conn = - sqlite3.connect(db_file) with conn: AF F F AF AF AF 47 sql_create_people_table = CREATE TABLE IF NOT EXISTS people id integer PRIMARY KEY, first_name text NOT NULL, Last_name text NOT NULL); cursor = conn.cursor() cursor.execute(sql_create_people_table) sql_truncate_people = "DELETE FROM people;" cursor.execute(sql_truncate_people) 48 49 50 51 52 53 people = generate_people (count) 54 55 56 sql_insert_person = "INSERT INTO people(id, first_name, Last_name) VALUES (?, ?, ?);" 57 for person in people: for person in people: cursor.execute(sql_insert_person, person) cursor.close() create_people_database (people_db_file, max_people) #PART 3 class PersonDB(): Context manager class provides read access to database def _init__(self, db_file=''): Store db_file parameter value "" self.db_file db_file def enter_(self): Initiate connection to database self.conn sqlite3.connect(self.db_file, check_same_thread=False) return self def 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 exit_(self, exc_type, exc_value, exc_traceback): Close database connection self.conn.close() def load_person(self, id): sql = "SELECT * FROM people WHERE id=?". cursor = self.conn.cursor() cursor.execute(sql, (id)) records = cursor.fetchall() result = (-1,"','') # id = -1, first_name = last_name if records is not None and len(records) > 0: result = records[@] cursor.close() return result def test_PersonDB(): with PersonDB (people_db_file) as db: print(db.load_person (10000)) # Should print the default print(db.load_person (122)) print(db.load_person (300)) Now, create a new method outside of the class called test_PersonDB() to test load_person() using a context manager: def test_PersonDB(): o Using a "with block with a PersonDB object, attempt to load and print three person records: with PersonDB (people_db_file) as db: print (db.load_person (10000)) # Should print the default print (db.load_person (122)) print (db.load_person (300)) Next, go to the "_main_" section and call test_PersonDBC) to test the method you just created Sample output (your results WILL vary due to the random nature of person creation): (-1,'','') (122, 'ELISA', 'SINGLETON') (300, "GENNY', 'WEBER') import sqlite3 from sqlite3 import Error import random 7 A 8 9 10 11 12 13 14 # PART 1 def generate_people(count): 15 with open('LastNames.txt', 'r') as filehandle: last_names = [line.rstrip() for line in filehandle] with open('FirstNames.txt', 'r') as filehandle: first_names [line.rstrip() for line in filehandle] 16 17 18 19 20 21 22 23 24 people list) for i in range(count): people.append((i, first_names[random.randint(0, len(first_names)-1)], last_names[random.randint(0, len(last_names)-1)])) 25 return people if name "_main_": people generate_people(5) print(people) #PART 2 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 people_db_file = "sqlite.db" # The name of the database file to use max_people = 500 # Number of records to create def create_people_database (db_file, count): conn = - sqlite3.connect(db_file) with conn: AF F F AF AF AF 47 sql_create_people_table = CREATE TABLE IF NOT EXISTS people id integer PRIMARY KEY, first_name text NOT NULL, Last_name text NOT NULL); cursor = conn.cursor() cursor.execute(sql_create_people_table) sql_truncate_people = "DELETE FROM people;" cursor.execute(sql_truncate_people) 48 49 50 51 52 53 people = generate_people (count) 54 55 56 sql_insert_person = "INSERT INTO people(id, first_name, Last_name) VALUES (?, ?, ?);" 57 for person in people: for person in people: cursor.execute(sql_insert_person, person) cursor.close() create_people_database (people_db_file, max_people) #PART 3 class PersonDB(): Context manager class provides read access to database def _init__(self, db_file=''): Store db_file parameter value "" self.db_file db_file def enter_(self): Initiate connection to database self.conn sqlite3.connect(self.db_file, check_same_thread=False) return self def 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 exit_(self, exc_type, exc_value, exc_traceback): Close database connection self.conn.close() def load_person(self, id): sql = "SELECT * FROM people WHERE id=?". cursor = self.conn.cursor() cursor.execute(sql, (id)) records = cursor.fetchall() result = (-1,"','') # id = -1, first_name = last_name if records is not None and len(records) > 0: result = records[@] cursor.close() return result def test_PersonDB(): with PersonDB (people_db_file) as db: print(db.load_person (10000)) # Should print the default print(db.load_person (122)) print(db.load_person (300))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

Compare Web 2.0 and Web 3.0.

Answered: 1 week ago