#On the name of ALLAH #Author : Fouad Teniou #Date : 07/03/09 #version :2.6.1 """ New in python 2.6 namedtuple function is used in my program IRR-Versus-NPV with a PresentValue as a typename, and discount rates r_1, r_2 and number of periods until payments n as fieldnames (PresentValue('r_1','r_2','n')) IRR-Versus-NPV program provide an NPV and IRR (linear interpolation) calculations by entering the outflows/inflows values for a project with 2 different rates r_1 and r_2, and only an NPV if r_2 is equal to zero The program also returns any value or a series of values from the Present Value Annuity Tables """ import itertools import operator import collections import math as m class MinusSignError(ArithmeticError): """ user attempt an operation on negative number""" pass class PresentValue(collections.namedtuple('PresentValue', 'r_1,r_2,n')): """PresentValue a subclass of namedtuple Python class with two rates values (r_1,r_2) and a period number n """ #set __slots__ to an empty tuple keep memory requirements low __slots__ = () @property def DF(self): """Compute the discount factor of two values""" if self.r_1<0 or self.r_2 < 0 or self.n<0: raise MinusSignError,\ "\n self.r_2: print "python "C:Fouad Teniou\Documents\IRRvNPV.py" # years Cash flows DF at 27% PV # 0 -70000 1.000 -70000 # 1 28000 0.787 22036 # 2 24000 0.620 14880 # 3 22500 0.488 10980 # 4 13000 0.384 4992 # 5 33000 0.303 9999 # 6 15000 0.238 3570 # 7 17000 0.188 3196 # NPV = -347 # years Cash flows DF at 13% PV # 0 -70000 1.000 -70000 # 1 28000 0.885 24780 # 2 24000 0.783 18792 # 3 22500 0.693 15592 # 4 13000 0.613 7969 # 5 33000 0.543 17919 # 6 15000 0.480 7200 # 7 17000 0.425 7225 # NPV = 29477 # ###########################################################################################