def test_investment_options_isvalid_trial_numbers(self): '''test if the class raises error when the input trial numbers cannot be conveted to integer''' self.assertRaises(invalid_trial_numbers, lambda: investment_options('[1, 10, 100, 1000]', 'invalid'))
def test_investment_options_invalid_list_value(self): '''test if the class raises error when the list has invalid elements''' self.assertRaises(invalid_list_value, lambda: investment_options('[1, 10, 100, invalid]', '10000'))
def test_investment_options_isvalid(self): '''test if the class has an expected return when input is in right format''' investment_test = investment_options('[1, 10, 100, 1000]', '10000') self.assertTrue(np.array_equal(investment_test.positions, np.array([1, 10, 100, 1000]))) self.assertTrue(investment_test.num_trials, 10000)
def test_investment_options_invalid_list_bound(self): '''test if the class raises error when input string cannot be converted to a list''' self.assertRaises(invalid_list_bound, lambda: investment_options('[1, 10, 100, 1000', '10000'))
from investment import investment_options import exception_user from exception_user import invalid_list_bound, invalid_list_value, invalid_trial_numbers ''' Main program for assignment 8. Also see readme.txt for instructions. ''' try: while True: positions = raw_input('Input a list of the number of shares: ') num_trials = raw_input('Input how many times to ramdomly repeat the test: ') try: investment_list = investment_options(positions, num_trials) with open('results.txt', 'w') as f: #save the results to result.txt for i in range(len(investment_list.positions)): daily_ret = investment_operating.num_trials_investment(investment_list.positions[i], investment_list.position_value[i], investment_list.num_trials) investment_operating.plot_investment(investment_list.positions[i], investment_list.position_value[i], investment_list.num_trials) print 'the mean of the daily return is: ' + str(np.mean(daily_ret)) print 'the stadard deviation of the daily return is: ' + str(np.std(daily_ret)) f.write('\n' + 'Positions: ' + str(investment_list.positions[i]) + ' Position values: '+ str(investment_list.position_value[i]) + ' trial numbers: ' + str(investment_list.num_trials)) f.write('\n' + 'the mean of the daily return is: ' + str(np.mean(daily_ret))) f.write('\n' + 'the stadard deviation of the daily return is: ' + str(np.std(daily_ret))) except invalid_list_bound: print 'Invalid list bound!' except invalid_list_value: print 'Invalid list value!' except invalid_trial_numbers: print 'Invalid trial number!'