# report.py
#
# Exercise 2.4
#
# Version 1: return data file as a list of tuples
#
# *** Notable error:  ***
#
#      CSV reads all fields as STRING
#      I forgot to convert the string to INT and FLOAT 

import csv

def read_portfolio(filename):
    '''Read a portfolio file'''

    data = list()        # return data as a list of tuples

    with open(filename, 'rt') as f:
        csv_f = csv.reader(f)

        # Read the header line (and discard)
        header = next(csv_f)
        print(header)
        print(type(header))

        # Read all data rows (and save in data)
        for row in csv_f:
            # print(row)        # row is a list
            # print(type( row) )

            r = tuple( ( row[0], int(row[1]), float(row[2]) )  )
#                                ^^^^^^^^ Convert the string data to numeric !!
            print(type(r) )
            data.append(r)   
    return data


portfolio = read_portfolio("Data/portfolio.csv")

# =============================
# iterate over a list
# =============================
total = 0.0
for s in portfolio:
    total += s[1] * s[2]     # s is a tuple, and it's addressable
print("Total cost = ", total)


# =============================
# iterate over a list
# =============================
total = 0.0
for name, numShares, price in portfolio: # Unpack a tuple
    total += numShares * price     # Used the unpacked fields...
print("Total cost = ", total)





