Question: Python - help with updating my randint logic. I don't know what I am doing wrong here. It seems that it thinks that my lists
Python - help with updating my randint logic. I don't know what I am doing wrong here. It seems that it thinks that my lists are empty. I tried printing the length of the first name and last name list, or maybe it's just the length 1? I tried putting the randit on different lines and print out the different ranges that I got, but I just got more confused. Since it is difficult to debug a large compound of statement. I tried to split it up into parts and print the inputs into my randit functions to see what I sending it.... but I got even more confused. Please help, I have no idea what to do here.
import sqlite3 from sqlite3 import Error import random from concurrent.futures import ThreadPoolExecutor
# PART 1 - Reads the file into a list
last_names = [] first_names = []
def read_file(filename): data = list() 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] return data
def generate_people(count): with ThreadPoolExecutor(max_workers = 10) as executor: future = executor.submit(read_file, 'LastNames.txt') future2 = executor.submit(read_file, 'FirstNames.txt') last_names = future.result() first_names = future2.result()
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)])) return people
if __name__ == "__main__": people = generate_people(5) print(people) #PART 2 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:
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) people = generate_people(count)
sql_insert_person = "INSERT INTO people(id,first_name,last_name) VALUES(?,?,?);" 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 __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[0] 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))
print(test_PersonDB()) #PART 4
def load_person(id, db_file): with PersonDB(db_file) as db: return db.load_person(id) 



In [19]: runfile('- wdir= Traceback (most recent call last): ; line in line , in generate_people File people = generate_people(5) File people.append(ii, first_names[random.randint(0, len(first_names)-1)], File , line 248, in randint return self.randrange(a, b+1) File " ", line 226, in randrange raise ValueError("empty range for randrange() (%d, %d, %d" % (istart, istop, width)) ValueError: empty range for randrange() (0, 0, 6) import sqlite3 from sqlite3 import Error import random from concurrent. futures import ThreadPoolExecutor # PART 1 - Reads the file into a list last_names = [] first_names = [] def read_file(filename): data = list() 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] return data = 7 A 8 9 10 11 12 13 14 15 16 17 18 19 20 21 A 22 23 24 A 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 def generate_people(count): with ThreadPoolExecutor(max_workers 10) as executor: future = executor.submit(read_file, 'LastNames.txt') future2 = executor.submit(read_file, 'FirstNames.txt') last_names = future.result() first_names future2.result() = 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)])) return people if name _main_": people generate_people(5) print(people) #PART 2 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): sqlite3.connect(db_file) with conn: conn = AF AR AF 57 58 59 60 61 AF AR AF 62 63 64 65 66 67 68 69 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) people = generate_people(count) 70 71 72 sql_insert_person = "INSERT INTO people(id, first_name, Last_name) VALUES (?,?,?);" for person in people: cursor.execute(sql_insert_person, person) cursor.close() create_people_database (people_db_file, max_people) 73 74 75 76 77 78 79 #PART 3 80 81 82 83 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 TE 84 85 86 def enter_(self): Initiate connection to database self.conn = sqlite3.connect(self.db_file, check_same_thread=False) return self def 87 88 89 90 91 92 93 94 95 96 97 _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=?" 98 last_name 99 100 101 102 103 104 105 106 107 cursor = self.conn.cursor() cursor.execute(sql, (id, )) records = cursor.fetchall() result = (-1,'','') # id = -1, first_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)) 108 109 110 111 112 113 114 115 116 117 118 119 120 121 print(test_PersonDB) #PART 4 def load_person(id, db_file): with PersonDB(db_file) as db: return db.load_person(id) In [19]: runfile('- wdir= Traceback (most recent call last): ; line in line , in generate_people File people = generate_people(5) File people.append(ii, first_names[random.randint(0, len(first_names)-1)], File , line 248, in randint return self.randrange(a, b+1) File " ", line 226, in randrange raise ValueError("empty range for randrange() (%d, %d, %d" % (istart, istop, width)) ValueError: empty range for randrange() (0, 0, 6) import sqlite3 from sqlite3 import Error import random from concurrent. futures import ThreadPoolExecutor # PART 1 - Reads the file into a list last_names = [] first_names = [] def read_file(filename): data = list() 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] return data = 7 A 8 9 10 11 12 13 14 15 16 17 18 19 20 21 A 22 23 24 A 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 def generate_people(count): with ThreadPoolExecutor(max_workers 10) as executor: future = executor.submit(read_file, 'LastNames.txt') future2 = executor.submit(read_file, 'FirstNames.txt') last_names = future.result() first_names future2.result() = 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)])) return people if name _main_": people generate_people(5) print(people) #PART 2 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): sqlite3.connect(db_file) with conn: conn = AF AR AF 57 58 59 60 61 AF AR AF 62 63 64 65 66 67 68 69 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) people = generate_people(count) 70 71 72 sql_insert_person = "INSERT INTO people(id, first_name, Last_name) VALUES (?,?,?);" for person in people: cursor.execute(sql_insert_person, person) cursor.close() create_people_database (people_db_file, max_people) 73 74 75 76 77 78 79 #PART 3 80 81 82 83 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 TE 84 85 86 def enter_(self): Initiate connection to database self.conn = sqlite3.connect(self.db_file, check_same_thread=False) return self def 87 88 89 90 91 92 93 94 95 96 97 _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=?" 98 last_name 99 100 101 102 103 104 105 106 107 cursor = self.conn.cursor() cursor.execute(sql, (id, )) records = cursor.fetchall() result = (-1,'','') # id = -1, first_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)) 108 109 110 111 112 113 114 115 116 117 118 119 120 121 print(test_PersonDB) #PART 4 def load_person(id, db_file): with PersonDB(db_file) as db: return db.load_person(id)