def main():
	# Exercise 4.1.5
	# Add a start method and end method
	print('\n====================================Exercise 4.1.5=====================================\n');
	print('Step a: Using start and end method ... \n');
	ta=Timer('Timer 1');
	ta.start();
	time.sleep(5);
	ta.end();
	raw_input('Program pause. Press enter to continue.\n');

	# Showing the ability to configure the Timer to display either seconds, minutes, or hours
	print('Step b: Running my display function ... \n');
	print('Displaying seconds only: \n');
	td=ta;
	td.display('secs'); print('');
	print('Displaying minites only: \n');
	td.display('mins'); print('');
	print('Displaying hours only: \n');
	td.display('hrs'); print('');
	print('Displaying seconds, minutes and hours: \n');
	td.display('secs','mins','hrs'); print('');
	raw_input('Program pause. Press enter to continue.\n');

	# Method to retrieve the last timer result
	print('Step c: Running my retrieve function ... \n');
	te=Timer('Timer 2');
	te.start();
	time.sleep(20);
	te.end(); te.start();
	time.sleep(5);
	te.end();
	print('Running my retrieve function ... \n');
	print('Retriving the current data: \n');
	print(te.retrieve(0)); print('');
	print('Retriving the previous one data: \n');
	print(te.retrieve(1)); print('');
	raw_input('Exercise 4.1.5 demo is successfully finished. Press any key to exit. \n');
Beispiel #2
0
 def equity(self, period):
     t = Timer('Equity Recur Timer')
     t.start()
     if period > self.term:
         logging.info('Period is greater than the term.')
         return None
     logging.debug('The equity for {} is {}.'.format(
         period,
         self._asset.getPresValue(period) - self.balanceRecur(period)))
     t.end()
     if t.retrieve(0) > t.getWarnThreshold():
         logging.warning(
             'The running time is over {} for {} and it is driving out your computer resource.'
             .format(t.getWarnThreshold(), t._timerName))
     return self._asset.getPresValue(period) - self.balanceRecur(period)
Beispiel #3
0
 def principalDueRecur(self, period):  #this method no rate is involved
     t = Timer('Principal Due Recur Timer')
     t.start()
     if period > self.term:
         logging.info('Period is greater than the term.')
         return None
     logging.debug('The principal due for {} is {}.'.format(
         period,
         self.monthlyPayment(period) - self.interestDueRecur(period)))
     t.end()
     if t.retrieve(0) > t.getWarnThreshold():
         logging.warning(
             'The running time is over {} for {} and it is driving out your computer resource.'
             .format(t.getWarnThreshold(), t._timerName))
     return self.monthlyPayment(period) - self.interestDueRecur(period)
Beispiel #4
0
 def interestDueRecur(self, period):
     t = Timer('Interest Due Recur Timer')
     t.start()
     if period > self.term:
         logging.info('Period is greater than the term.')
         return None
     logging.debug('The interest due for {} is {}.'.format(
         period,
         self.balanceRecur(period - 1) * self.rate(period)))
     t.end()
     if t.retrieve(0) > t.getWarnThreshold():
         logging.warning(
             'The running time is over {} for {} and it is driving out your computer resource.'
             .format(t.getWarnThreshold(), t._timerName))
     return self.balanceRecur(period - 1) * self.rate(period)
Beispiel #5
0
 def balanceRecur(self, period):  #this method no rate is involved
     t = Timer('Balance Recur Timer')
     t.start()
     if period > self.term:
         logging.info('Period is greater than the term.')
         return None
     if period == 0:
         logging.debug('The balance for {} is {}.'.format(
             period, self.face))
         return self.face
     else:
         logging.debug('The balance for {} is {}.'.format(
             period,
             self.balanceRecur(period - 1) -
             self.principalDueRecur(period)))
         t.end()
         if t.retrieve(0) > t.getWarnThreshold():
             logging.warning(
                 'The running time is over {} for {} and it is driving out your computer resource.'
                 .format(t.getWarnThreshold(), t._timerName))
         return self.balanceRecur(period -
                                  1) - self.principalDueRecur(period)
def main():
    # Exercise 2.1.2
    # Create a loan for the whole demo with face=1000000, APR=.035 and term=360 months
    loan = Loan(None, face=1000000, rate=0.035, term=360)

    # a) Use a method to compute monthly payment
    print(
        '\n====================================Exercise 2.1.2=====================================\n'
    )
    print('Step a: Running my monthlyPayment method ... \n')
    pmt = round(loan.monthlyPayment(period=24), 2)
    #period is a dummy variable
    print('The monthly payment is {}. \n'.format(pmt))
    raw_input('Program pause. Press enter to continue.\n')

    # b) Use a method to compute total payment
    print('Step b: Running my totalPayments method ... \n')
    pmtTtl = round(loan.totalPayments(period=24), 2)
    print('The total payment is {}. \n'.format(pmtTtl))
    raw_input('Program pause. Press enter to continue.\n')

    # c) Use a method to compute total interest
    print('Step c: Running my totalInterest method ... \n')
    instTtl = round(loan.totalInterest(), 2)
    print('The total interest is {}. \n'.format(instTtl))
    raw_input('Program pause. Press enter to continue.\n')

    # Exercise 2.1.3
    print(
        '\n====================================Exercise 2.1.3=====================================\n'
    )
    # Create three timers for interest due, principal due and balance computation respectively
    t_inst = Timer()
    t_prin = Timer()
    t_bala = Timer()
    print('Part 1: Running my formulas evaluation approach ... \n')
    print('Step a: Running my interestDueFoml method ... ')
    t_inst.start()
    instDue = loan.interestDueFoml(period=24)
    t_inst.end()
    print('The interest due for period {} is {}. '.format(
        24, round(instDue, 2)))
    print('Step b: Running my principalDueFoml method ... ')
    t_prin.start()
    principalDue = loan.principalDueFoml(period=24)
    t_prin.end()
    print('The principal due for period {} is {}. '.format(
        24, round(principalDue, 2)))
    print('Step c: Running my balanceFoml method ... ')
    t_bala.start()
    balance = loan.balanceFoml(period=24)
    t_bala.end()
    print('The balance for period {} is {}. \n'.format(24, round(balance, 2)))
    raw_input('Program pause. Press enter to continue.\n')

    print('Part 2: Running my recursive approach ... \n')
    print('Step a: Running my interestDueRecur method ... ')
    t_inst.start()
    instDue = loan.interestDueRecur(period=24)
    t_inst.end()
    print('The interest due for period {} is {}. '.format(24, round(instDue)))
    print('Step b: Running my principalDueRecur method ... ')
    t_prin.start()
    principalDue = loan.principalDueRecur(period=24)
    t_prin.end()
    print('The principal due for period {} is {}. '.format(
        24, round(principalDue)))
    print('Step c: Running my balanceRecur method ... ')
    t_bala.start()
    balance = loan.balanceRecur(period=24)
    t_bala.end()
    print('The balance for period {} is {}. \n'.format(24, round(balance)))
    raw_input('Program pause. Press enter to continue.\n')

    print(
        'Part 3: Comparing two costs of approachs to Implementations of method balance ... \n'
    )
    t_1 = Timer()
    t_2 = Timer()
    for perid in range(360 + 1):
        t_1.start()
        balance = loan.balanceFoml(period=perid)
        t_1.end()
        t_2.start()
        balance = loan.balanceRecur(period=perid)
        t_2.end()
    raw_input('Program pause. Press enter to continue.\n')

    print('Part 4: Comparing two costs of approachs ... \n')
    print('Costs of formula approach are {}, {}, {} respectively. ' \
     .format(t_inst.retrieve(1), t_prin.retrieve(1), t_bala.retrieve(1)))
    print('Costs of recursive approach are {}, {}, {} respectively. ' \
     .format(t_inst.retrieve(0), t_prin.retrieve(0), t_bala.retrieve(0)))
    print(
        'As period increases, the cost of recursive method increases rapidly')
    raw_input('Program pause. Press enter to continue.\n')

    # Exercise 2.1.4
    print(
        '\n====================================Exercise 2.1.4=====================================\n'
    )
    # a) Implement a class-level method called calcMonthlyPmt, in the Loan base class. This should calculate a monthly
    # payment based on three parameters: face, rate, and term
    print('Step a: Running my calcMonthlyPmt class method ... \n')
    pmt = Loan.calcMonthlyPmt(face=1000000, rate=0.035, term=360)
    print('The monthly payment is {}. \n'.format(round(pmt, 2)))
    raw_input('Program pause. Press enter to continue.\n')

    # b) Create a class-level function, in the Loan base class, which calculates the balance (calcBalance). Input
    # parameters should be face, rate, term, period
    print('Step b: Running my calcBalance class method ... \n')
    balance = Loan.calcBalance(face=1000000, rate=0.035, term=360, period=24)
    print('The balance is {}. \n'.format(round(balance, 2)))
    raw_input('Program pause. Press enter to continue.\n')

    # c) Test the class-level methods in main
    print('Step c: Test the class-level methods in main. \n')
    print('This part is shown in step (a) and (b). \n')
    raw_input('Program pause. Press enter to continue.\n')

    # d) Modify the object-level methods for monthlyPayment and balance to delegate to the class-level methods
    # e) Test the object-level methods to ensure they still work correctly.
    print(
        'Step d and e: Running my monthlyPayment2 object-level method ... \n')
    pmt = Loan.monthlyPayment2(loan, period=24)
    print('The monthly payment is {}. \n'.format(round(pmt, 2)))
    raw_input('Program pause. Press enter to continue.\n')

    # f) What are the benefits of class-level methods? When are they useful?
    print('Step f: The benefits of class-level methods. \n')
    print(
        'The class methods can be called on the class itself and does not know about any object istance. \
		It applied to all past, present and future objects of the class. ')
    print(
        'Useful for methods that are related to the class but not logically meant to perform on a \
		instantiated object. ')
    raw_input('The demo successfully finished. Press press any key to exit.\n')

    # Exercise 2.1.5
    # Write comments
    print(
        '\n====================================Exercise 2.1.5=====================================\n'
    )
    # a) Create a static-level method in Loan called monthlyRate. This should return the monthly interest
    # rate for a passed-in annual rate
    print('Step a: Running my monthlyRate static method ... \n')
    monthlyRate()
    raw_input('Program pause. Press enter to continue.\n')

    # b) Create another static-level method that does the opposite (returns an annual rate for a passed-in monthly rate)
    print('Step b: Running my annualRate static method ... \n')
    annualRate()
    raw_input('Program pause. Press enter to continue.\n')

    # c) Test the static-level method in main.
    print('Step c: Test the static-level methods in main. \n')
    print('This part is shown in step (a) and (b). \n')
    raw_input('Program pause. Press enter to continue.\n')

    # d) Modify all the Loan methods that rely on the rate to utilize the static-level rate functions
    # Create a new class Loan2 to modify all the Loan2 methdos that rely on rate with help of static rate functions
    loan2 = Loan2(None, 1000000, 0.035, 360)
    print('The monthly payment is ' + str(loan2.monthlyPayment(period=24)) +
          '\n')
    print('The total payment is ' + str(loan2.totalPayments(period=24)) + '\n')
    print('The total interest is ' + str(loan2.totalInterest()) + '\n')
    print('The interest due for period 24 is ' +
          str(loan2.interestDueFoml(period=24)) + '\n')
    print('The principal due for period 24 is ' +
          str(loan2.principalDueFoml(period=24)) + '\n')
    print('The balance for period is ' + str(loan2.balanceFoml(period=24)) +
          '\n')
    print('The interest due for period 24 is ' +
          str(loan2.interestDueRecur(period=24)) + '\n')
    print('The principal due for period 24 is ' +
          str(loan2.principalDueRecur(period=24)) + '\n')
    print('The balance due for period 24 is ' +
          str(loan2.balanceRecur(period=24)) + '\n')

    # e) What are the benefits of static-level methods? When are they useful?
    print('Step e: The benefits of static-level methods. \n')
    print('The static-level methods will not aware of any object instantiate \
		of over even the class itself.')
    print(
        'Useful for grouping related functions together which may not logically from a class. '
    )
    raw_input('The demo successfully finished. Press press any key to exit.\n')
def main():
	# Exercise 2.1.1
	print('\n====================================Exercise 2.1.1 (a)=====================================\n');
	
	# a) Create a class called Timer
	print('In step a: Creating a object t belongs to class Timer ... \n');
	ta=Timer();
	print('Type of new object t is '+str(type(ta))+'\n');
	raw_input('Program pause. Press enter to continue.\n');

	# b) Add a start method and end method
	print('\n====================================Exercise 2.1.1 (b)=====================================\n');
	print('In step b: Using start and end method ... \n');
	ta.start();
	for i in range(10000000): #some random functions that takes time to execute
		i+=1;
	ta.end();
	raw_input('Program pause. Press enter to continue.\n');

	# c) Note that start should give an error if the Timer is already started and end should give an error if the 
	# Timer is not currently running
	print('\n====================================Exercise 2.1.1 (c)=====================================\n');
	print('In step c: start and end will raise errors message if unappropriate call ... \n');
	print('Case 1 demo: start is called after the timer is started. \n');
	tc=Timer();
	tc.start();
	tc.start();
	print('Case 2 demo: end is called before the timer is started. \n');
	tc2=Timer();
	tc2.end();
	raw_input('Program pause. Press enter to continue.\n');

	# d) Add the ability to configure the Timer to display either seconds, minutes, or hours
	print('\n====================================Exercise 2.1.1 (d)=====================================\n');
	print('In step d: Running my display function ... \n');
	print('Displaying seconds only: \n');
	td=ta;
	td.display('seconds');
	print('Displaying minites only: \n');
	td.display('minutes');
	print('Displaying hours only: \n');
	td.display('hours');
	print('Displaying seconds, minutes and hours: \n');
	td.display('seconds','minutes','hours');
	raw_input('Program pause. Press enter to continue.\n');

	# e) Add a method to retrieve the last timer result
	print('\n====================================Exercise 2.1.1 (e)=====================================\n');
	te=Timer();
	te.start();
	for i in range(10000000):
		i+=1;
	te.end(); te.start();
	for i in range(5000000):
		i*=1;
	te.end();
	print('Running my retrieve function ... \n');
	print('Retriving the current data: \n');
	print(str(te.retrieve(0))+'\n');
	print('Retriving the previous one data: \n');
	print(str(te.retrieve(1))+'\n');
	raw_input('Timer demo is successfully finished. Press any key to exit. \n');