def process(self, jsonString): """ This function takes as input the a single file containing one valid JSON string, spread over one or more lines. """ # Load the JSON string for the fund try: jsonDict = json.loads(jsonString) except ValueError: print 'ERROR - Invalid JSON...' print jsonString exit(1) # Parse the JSON data jsonDict = json.loads(jsonString) name = jsonDict['name'] sellDate = jsonDict['sell_date'] stocks = jsonDict['stocks'] # Output the name of the fund if self.verbose: print 'Processing {0}...'.format(name) for stock in stocks: ticker = stock['ticker'].upper() if ticker == 'RANDOM': ticker = self.getRandomTicker() buyDate = stock['buy_date'] buyLimit = stock['buy_limit'] # Get the optional parameters, unit limit try: unitLimit = stock['unit_limit'] except KeyError: unitLimit = -1 # And the optional parameter, repeat interval try: repeat = stock['repeat'] except KeyError: repeat = 'never' # Output the stock details if verbose is enabled if self.verbose: print '{0}, BuyDate:{1}, BuyLimit:{2}, UnitLimit{3}, Repeat:{4}'.format(ticker, \ buyDate, buyLimit, unitLimit, repeat) # Initialize the stock and save it to list stock = Stock(ticker, buyDate, sellDate, buyLimit, unitLimit, repeat, self.verbose) self.stockList.append(stock) # Now figure out your winnings for stock in self.stockList: total = stock.printDetails()
def test_Stock_Init_1(self): stock = Stock('AMZN', 20150111, 20160115, 1000.0, 10.0, 'never', False) date, price = stock.getOpeningPrice() self.assertTrue(price > 0.0 and price < 1000.0)
def test_Stock_Units_3(self): stock = Stock('AMZN', 20150106, 20151231, 800.0, 1.0, 'monthly', False) units = stock.totalUnits() self.assertEquals(units, 12)
def test_Stock_Units_2(self): stock = Stock('AMZN', 20160103, 20160113, 800.0, 1.0, 'weekly', False) units = stock.totalUnits() self.assertEquals(units, 2)
def test_Stock_Init_5(self): stock = Stock('AAPL', 20160125, 20160127, 100.0, 2.0, 'daily', False) price = stock.totalPurchased() self.assertTrue(price > 250.0 and price < 300.0)
def test_Stock_Init_3(self): stock = Stock('AAPL', 20160127, 20160127, 100.0, 2.0, 'never', False) date, price = stock.getOpeningPrice() self.assertTrue(price > 93.0 and price < 97.0)