guys so I'm building a python app and something strange is happening with getattr()
. So I'm reading an XML file using objectify and everything going well until I tried to get one of its attributes called text
and for some reason, it falls back to None
, AKA: default, when it should return another class. I already checked using .find
and hasattr()
and indeed it exists so I really don't get what's wrong.
My code:
cdef str name_builder(self):
"""
Cleans name str so its database friendly
and combines custom data accordingly to
build the full name
:return: str
"""
cdef str name = ''
print(self.xml_data.find('text').find('name').text)
print(vars(self.xml_data))
print(hasattr(self.xml_data, 'text'))
cdef textt = getattr(self.xml_data, 'text')
print(textt)
if textt is not None:
name = str(getattr(textt, 'name', ''))
name = name.strip()
name = name.replace("'", '')
return str(name)
The logic that I have for the attr uri
:
cdef str get_a_url(self):
cdef str url = ''
cdef uri = getattr(self.xml_data, 'uri', None)
if uri is not None:
url = str(getattr(uri, 'awTrack', ''))
url = url.replace(' ', '')
return str(url)
Output from console:
LG 24MP88HV-S 23.8" LED IPS
{'brand': <Element brand at 0x7f9c5e022400>, 'cat': <Element cat at 0x7f9c5e022480>, 'price': <Element price at 0x7f9c5e030380>, 'text': <Element text at 0x7f9c5e0488c0>, 'uri': <Element uri at 0x7f9c5e048e40>, 'vertical': '', 'pId': 99982, 'comGroup': 'PER', 'cond': 'new', 'ean': 8806087633771, 'mpn': '24MP88HV-S', 'proType': 'Monitores', 'rating': 8.82}
True
None
XML Schema:
<prod id="" in_stock="" is_for_sale="" lang="" pre_order="" stock_quantity="" web_offer="">
<brand>
<brandName></brandName>
</brand>
<cat>
<mCat></mCat>
</cat>
<price curr="EUR">
<buynow></buynow>
<delivery></delivery>
<rrp></rrp>
<store></store>
</price>
<text>
<name></name>
<desc></desc>
<keywords></keywords>
<promo></promo>
<warranty></warranty>
</text>
<uri>
<awTrack></awTrack>
<alternateImage></alternateImage>
<awImage></awImage>
<awThumb></awThumb>
<mImage></mImage>
<mLink></mLink>
</uri>
</prod>
I'm probably doing some dumb mistake but this is eating my brain out cuz it doesn't make any sense.
Sorry guys but unfortunately I can't share the XML data since it contains private data. Hope you can understand