def read_prices(filename): with open(filename, "rt") as f: prices = parse_csv(f, types=[str, float], has_headers=False, silence_errors=True) return dict(prices)
def read_portfolio(filename): '''Computes the total cost (shares*price) of a portfolio file''' with open(filename, 'rt') as file: portdict = parse_csv(file, select=['name', 'shares', 'price'], types=[str, int, float]) portfolio = [Stock(d['name'], d['shares'], d['price']) for d in portdict] return portfolio
def main(): s = Stock("bla", 100, 9.9) print(s) with open("Data/portfolio.csv", "rt") as lines: from Work.fileparse import parse_csv portdicts = parse_csv(lines, select=['name', 'shares', 'price'], types=[str, int, float]) portfolio = [ Stock(p["name"], p["shares"], p["price"]) for p in portdicts ] print(sum([s.cost for s in portfolio]))
def read_prices(filename): with open(filename, 'rt') as file: prices = dict(parse_csv(file, types=[str, float], has_headers=False)) return prices
def read_portfolio(filename): with open(filename, "rt") as f: dicts = parse_csv(f, types=[str, int, float]) return [Stock(**p) for p in dicts]
def portfolio_cost(filename): with open(filename, "rt") as f: return parse_csv(f, select=["shares", "price"], types=[int, float], silence_errors=True)