Home > other >  PYTHON 3.9: How can I access attributes of a class being a value of a dict?
PYTHON 3.9: How can I access attributes of a class being a value of a dict?

Time:02-11

I'm writing a python script that I need to check if some automatic-generated code is consistent.

So far I'm stuck trying to compare two strings... Here's the situation

The code I'm trying to read is written in XML. The file being checked is made up like this:

(EDIT: I've been warned that the xml file is not valid written that way. This is not a problem: this file is to be used to configure a very specific embedded system and is actually written as per specification of Ada/C compiler supplier)

<Channel Id="313" Name="Pizza">
    <Source>
    </Source>
    <Destination>
        <Some_Tag Cola="3" FunnyName="Lasagna" Name2="Pizza"/>
    </Destination>
</Channel>

<Channel Id="123" Name="Apple_Pie">
    <Source>
    </Source>
    <Destination>
        <Some_Tag Cola="3" FunnyName="Lasagna" Name2="Apple_Pie"/>
    </Destination>
</Channel>

My goal is to check that:

  1. for each "Channel", "Name" and "Name2" are equal.
  2. The same "Name" does not appear in other "Channel"s
  3. The "Id" is unique for every "Channel"

I'm not using any external library (script must run on corporate PC). I manage to collect the useful tag (I've tried printing them)

After reading my tag, I store them in a Temporary Class. If Goal 1 is satisfied, I will then check if the "Name" or the "Id" tag are already present in previously read "Channel"s, stored in a dict holding the class as value. If all goes well, I copy the temporary Class in a dict of classes.

Class MyClass():
    Id : int
    Name : str
    Name2 : str

Dict_of_classes : dict = {str:MyClass}
TmpClass = MyClass()

#here I try to check goals 2 & 3

for key in Dict_of_classes:
    if TmpClass.Id == Dict_of_classes[key].Id:
       print("error")


thing is: Python reports that the class "MyClass" has no attribute "Id". An AttributeError exception is thrown.

I've tried so far:

  1. Coping Dict_of_classes[key] in a new variable and then comparing: same exception as above
  2. using for key, value in Dict_of classes: Python reports that he cannot iterate through a non-iterable object (my class, i think)

Any help is appreciated, thanks!

CodePudding user response:

My goal is to check that:

for each "Channel", "Name" and "Name2" are equal.

The same "Name" does not appear in other "Channel"s

The "Id" is unique for every "Channel"

The below should work for you

import xml.etree.ElementTree as ET


xml = '''<r>
<Channel Id="313" Name="Pizza">
    <Source>
    </Source>
    <Destination>
        <Some_Tag Cola="3" FunnyName="Lasagna" Name2="Pizza"/>
    </Destination>
</Channel>

<Channel Id="123" Name="Pizza">
    <Source>
    </Source>
    <Destination>
        <Some_Tag Cola="3" FunnyName="Lasagna" Name2="Apple_Pie"/>
    </Destination>
</Channel>

<Channel Id="123" Name="XYZ">
    <Source>
    </Source>
    <Destination>
        <Some_Tag Cola="3" FunnyName="Lasagna" Name2="XYZ"/>
    </Destination>
</Channel>
</r>'''

ids = set()
names = set()
root = ET.fromstring(xml)
channels = root.findall('Channel')
for c in channels:
  name1 = c.attrib['Name'] 
  name2 = c.find('.//Some_Tag').attrib['Name2']
  if name1 != name2:
    print(f'{name1} != {name2}')
  names.add(name1)
  ids.add(c.attrib['Id'])
if len(ids) != len(channels):
  print('We have Id duplications')
if len(names) != len(channels):
  print('We have Name duplications')
  • Related