# stock.py

class Stock:
    types = (str, int, float)
    def __init__(self, name, shares, price):
        self.name = name
        self.shares = shares
        self.price = price

    @classmethod
    def from_row(cls, row):
        values = [func(val) for func, val in zip(cls.types, row)]
        return cls(*values)

    def cost(self):
        return self.shares * self.price

    def sell(self, nshares):
        self.shares -= nshares

def read_portfolio(filename):
    '''
    Read a CSV file of stock data into a list of Stocks
    '''
    import csv
    portfolio = []
    with open(filename) as f:
        rows = csv.reader(f)
        headers = next(rows)
        for row in rows:
            record = Stock.from_row(row)
            portfolio.append(record)
    return portfolio

if __name__ == '__main__':
    import tableformat
    import reader
    # portfolio = read_portfolio('../../Data/portfolio.csv')
    portfolio = reader.read_csv_as_instances('../../Data/portfolio.csv', Stock)
    tableformat.print_table(portfolio, ['name', 'shares', 'price'])
