Search code examples
pythondictionaryget

unable to assign value from python dictionary to variable


I am trying to assign a value to variable from a dictionary but unable to do so.

I have tried:

self.currency = inflationcurrency[self.currency]
self.currency = inflationcurrency.get([self.currency])

on the top I have tried declaring self.currency as

self.currency = ''.join(self.tempholder[0])

This is the code:

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)
        else:
            return self.currency

        def makequalifier(self):
        qualifier = string.join([self.currency,"|",self.spartSABR(),"|",self.modelname(),"|",self.dttype(),"|",self.lastpart()])
        return qualifier 

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

The error on above is expected string but recieved Nonetype from self.currency

I get none when I do below.

print (test1.currency())

Solution

  • You are using currency both as variable and as a method, rename one of them.

    --- Edit ---

    You are not returning in the curr method.

    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
    

    This should work.