0

BACKGROUND:

I have set an excel parse the loads into “ObjectGroup” and “ObjectGroupMember”

ObjectGroup: [List of Objects]

Eg: SensitiveObject [‘PCI_SENS_OBJECT’, ‘SOX_SENS_OBJECT’]

ObjectGroupMember: [List of Members of ‘ObjectGroup]

Eg: PCI = [CC1,CC2,CCARD]

The variable name for the “ObjectGroupMember” is derived from values in ‘ObjectGroup’ using,

class GrpMem:

def __init__(self, senObj):
    for item in senObj:
        self.__dict__[item] = []`

My aim is to load my application with list of Objects and its respective members into my external database.

EXPECTATION:

OBJECT_GROUP = DataParse('Datasheet.xlsx')
ObjectList = OBJECT_GROUP.senObj
ObjMem1 = OBJECT_GROUP.SenObjmem.PCI_SENS_OBJECT
  1. Load external database with All Objects (senObj)
  2. Load members for senObj(0). This is derived from SenObjmem.PCISENS_OBJECT

ISSUE:

Hers is my current class

class LoadGrp():
    from GuardDataParse import DataParse
    def __init__(self,sourcefile):
        self.sourcefile = sourcefile
        self.parse = DataParse(self.sourcefile)
    def LoadGroup(self,GrpMembers):
        self.GrpMembers = GrpMembers
        DatasheetParse = DataParse(self.sourcefile)
        SensObj = DatasheetParse.senObj
        SensObjMem = DatasheetParse.SenObjmem+"."+str(GrpMembers)
       #code for application load
if __name__ == "__main__":
    grdapi_run = LoadGrp('DataSheet.xlsx')

RUN:

grdapi_run = LoadGrp('DataSheet.xlsx')
grdapi_run.LoadGroup('PCI_SENS_OBJECT')

Based on the above logic when I provide the LoadGroup with value (eg. PCI_SENS_OBJECT), SensObjMem should end up looking like SensObjMem = DatasheetParse.SenObjmem.PCI_SENS_OBJECT But I get the error” TypeError: unsupported operand type(s) for +: 'instance' and 'str’”

Community
  • 1
  • 1

2 Answers2

0

Python requires all string concatenation to be performed between explicit strings, unlike e.g. Java, which allows you to concatenate anything to a string. DatasheetParse.SenObjmem is presumably not a string, so you need to convert it to one using str before trying to concatenate it with ".".

Navneeth
  • 373
  • 3
  • 15
0

Python will not automatically convert types, even if that class has a str function defined, so when it gets to DatasheetParse.SenObjmem+".", it attempts to call either DatasheetParse.SenObjmem.add(".") or String.radd(DatasheetParse.SenObjmem), both of which are of course going to be undefined by default.

In order to fix this, you can either change it to str(DatasheetParse.SenObjmem)+"."+str(GrpMembers), or overload the add function on SenObjmem, if you have access to it.

It might be better still to rewrite it as

"{}.{}".format(str(DatasheetParse.SenObjmem), str(GrpMembers)

I might also suggest reading up on Difference between __str__ and __repr__.

nchen24
  • 502
  • 2
  • 9