I’m pulling stock-quotes from Yahoo into a named tuple using the CSV module.
YahooQuote = collections.namedtuple(
'YahooQuote', 'date, open, high, low, close, volume, adj_close')
def prices(ticker):
# make url given ticker
csvfile = urllib2.urlopen(url)
return map(YahooQuote._make, csv.reader(csvfile))
Yahoo’s stock quote csv format does not include the stock ticker. If I adjusted my named tuple class to include a ticker attribute, how would I modify the map expression to make it add the value of the ticker argument to each of the named tuple instances?
I’m genetically incapable of understanding code with map() in it, so I’m just going to transform “map(f, i)” into “[f(x) for x in i]” so I don’t have to:
Then it’s a simple matter to add ticker to the end of the lists returned by csv.reader: