Search code examples
pythonif-statementreturn

if code returning none value while it should return a specified value


Checked my loop several times. I cannot spot a mistake need another pair of eyes to help out. The code below is returning none while it should return a particular value.

I have checked the value of self.mdlname it is correct. I think there is something to do with with first function curr. It is not correctly reassigning value to self.currency. I see no other reason why it should return a none value.

Below ommitted functions after spartSABR function

import string

datatype = "Inflation SABR Vol ATM ZC"
dataconvention = "UK-RPI-ZERO-COUPON-SWAP-RATE"
mdlname = 'INFLATION_SABR'
t1 = 'should-send-back-none'


class srtqualifier:
    def __init__(self,mdlname,datatype, dataconvention,t1):
        self.mdlname = mdlname
        self.datatype = datatype
        self.dataconvention = dataconvention
        self.t1 = t1 ##TO BE REMOVED, ONLY USE FOR TESTING##
        self.tempholder =  self.dataconvention.split("-")
        self.currency = self.tempholder[0]

    def curr(self):
        if self.mdlname == 'INFLATION_SABR':
            inflationcurrency = {'UK':'GBP','FR':'EUR','EU':'EUR','US':'USD'}
            self.currency = inflationcurrency.get(self.currency)
            return self.currency

    def spartSABR(self):
        if self.dataconvention == 'JPY-SWAPTION-CLEAREDPHYSICAL-SEMI-ACT365F':
            return 'LIBOR_JPY_6M'
        elif self.dataconvention == 'USD-SIFMA Municipal Swap Index-1W-SWAPTION-PHYSICAL-SEMI-BOND':
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            separator = "_"
            secondpart = separator.join(secondpartsp[1:len(secondpartsp)])
            secondpart = secondpart.upper()
            return secondpart
        elif self.mdlname == 'INFLATION_SABR':
            secondpartsp = self.tempholder[0:2]
            secondpartsp = secondpartsp.append([self.currency,'1Y'])
            return secondpartsp
        else:
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            secondpartsp.insert(1,self.currency)
            separator = "_"
            secondpart = separator.join(secondpartsp)
            secondpart = secondpart.upper()
            return secondpart

test1 = srtqualifier(mdlname,datatype,dataconvention,t1)
print (test1.curr())
print (test1.spartSABR())

Solution

  • Based on the logic provided by Ganga Siva Krishna. I figured out the problem. I didnt need to assign and append together. Solution is below:

    def spartSABR(self):
            if self.dataconvention == 'JPY-SWAPTION-CLEAREDPHYSICAL-SEMI-ACT365F':
                return 'LIBOR_JPY_6M'
            elif self.dataconvention == 'USD-SIFMA Municipal Swap Index-1W-SWAPTION-PHYSICAL-SEMI-BOND':
                secondpartsp = self.tempholder[1].split(" ")
                secondpartsp.append(self.tempholder[2])
                separator = "_"
                secondpart = separator.join(secondpartsp[1:len(secondpartsp)])
                secondpart = secondpart.upper()
                return secondpart
            elif self.mdlname == 'INFLATION_SABR':
                secondpartsp = self.tempholder[0:2]
                print(self.tempholder[0:2])
                secondpartsp.append(self.currency)## I was doing secondpartsp= secondpartsp.append(self.currency)
                secondpartsp.append('1Y')
                separator = "_"
                secondpartsp = separator.join(secondpartsp)
                return secondpartsp
            else:
                secondpartsp = self.tempholder[1].split(" ")
                secondpartsp.append(self.tempholder[2])
                secondpartsp.insert(1,self.currency)
                separator = "_"
                secondpart = separator.join(secondpartsp)
                secondpart = secondpart.upper()
                return secondpart