def calculate_p_e_ratio(created_stock=None): """ A console wrapper for StockExchange.p_e_ratio_calculator :param created_stock: an optional argument for a stock object, for when the method is called from create_stock. :type created_stock: Stock """ global stock_exchange # Creating a stock exchange, on the first run, to register the stock and access p_e_ratio_calculator() # Calling create_stock() to create the stock for which the p/e ratio will be calculated. Only one stock is needed. try: if not stock_exchange: stock_exchange = StockExchange('temp') create_stock('calculate_p_e_ratio') else: user_provided_args = raw_input( 'Please provide the price for stock: {}, or \'q\'to quit\n:'.format(created_stock.stock_symbol)) exit_program(user_provided_args) stock_exchange.add_new_stock(created_stock) p_e_ratio = stock_exchange.p_e_ratio_calculator(created_stock.stock_symbol, user_provided_args.strip()) print 'P/E ratio for stock: {} and price: {} is: {}'.format(created_stock.stock_symbol, user_provided_args.strip(), p_e_ratio) except TypeError as error: print error # Retrying return calculate_p_e_ratio()
def test_all_share_index_calculator(self): stock_exchange = StockExchange('test') stock_exchange.all_share_index_calculator() stock = Stock('POP', 'common', '8', '100') stock.current_price = 150 stock_exchange.add_new_stock(stock) stock1 = Stock('TEA', 'common', '0', '100') stock1.current_price = 250 stock_exchange.add_new_stock(stock1) stock2 = Stock('GIN', 'preferred', '8', '100', '2%') stock_exchange.add_new_stock(stock2) all_share_index = stock_exchange.all_share_index_calculator() self.assertEqual(all_share_index, 155.362)
def test_remove_stock_by_symbol(self): stock_exchange = StockExchange('test') stock = Stock('GIN', 'preferred', '8', '100', '2%') stock_exchange.add_new_stock(stock) stock_exchange.remove_existing_stock_by_symbol('GIN') stock_exchange.remove_existing_stock_by_symbol('TEA')
def test_add_new_stock(self): stock_exchange = StockExchange('test') stock = Stock('GIN', 'preferred', '8', '100', '2%') stock_exchange.add_new_stock(stock) stock_exchange.add_new_stock(stock)