I want to fill a list within a class but I do not find a way to pass it from the calling module to the class and/or back again. Here is the calling module:
from NamesClass import Names
global TABLE, Names_db
TABLE = []
Names_db = []
RDATAPATH = "C:/users/richo/documents/python/Name Data/yob.txt"
L = Names.LoadTable(RDATAPATH)
print(L, len(Names_db))
The print returns 1888 0.
The two numbers should be the same.
Here is the class code:
class Names:
def __init__(self):
pass
def LoadTable(rdatapath):
""" load and sort a list from the text file
return the length of the list """
global TABLE, Names_db
TABLE = []
Names_db = []
first_time = True
with open(rdatapath) as f:
for line_of_text in f:
line_of_text = line_of_text.strip()
aname = line_of_text.split(',')
TABLE.append(aname[1]+','+aname[0]+','+aname[2]+','+aname[3]) #name, year, sex, occurences
TABLE.sort() #sort by name
for i, a_record in enumerate(TABLE):
record = a_record.split(',')
if first_time:
previous_name = record[0]
previous_sex = record[2]
sum = 0
first_time = False
if previous_name != record[0]:
Names_db.append(str(i)+previous_name+previous_sex+str(sum))
sum = 0
first_time = True
previous_name = record[0]
previous_sex = record[2]
sum += int(record[3])
return(len(Names_db))