0

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

8
  • Without an minimal reproducible example who knows. My guess would be: cdef attributes aren't exposed to Python by default and so aren't accessible through getattr Commented Sep 25, 2021 at 16:53
  • Probably a duplicate of stackoverflow.com/questions/55230665/… Commented Sep 25, 2021 at 16:53
  • @DavidW I get that but i have the same logic for uri and it works properly. Commented Sep 25, 2021 at 16:57
  • @DavidW i updated the question with more information as per your request. Hope it helps Commented Sep 25, 2021 at 17:03
  • The main information that I can't easily see is "what is the type of self.xml_data?" (both the type you've told Cython it is, and the actual type) Commented Sep 25, 2021 at 17:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.