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

DavidW
  • 29,336
  • 6
  • 55
  • 86
DeadSec
  • 808
  • 1
  • 12
  • 38
  • Without an [mre] who knows. My guess would be: `cdef` attributes aren't exposed to Python by default and so aren't accessible through `getattr` – DavidW Sep 25 '21 at 16:53
  • Probably a duplicate of https://stackoverflow.com/questions/55230665/cython-class-attributeerror – DavidW Sep 25 '21 at 16:53
  • @DavidW I get that but i have the same logic for `uri` and it works properly. – DeadSec Sep 25 '21 at 16:57
  • @DavidW i updated the question with more information as per your request. Hope it helps – DeadSec Sep 25 '21 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) – DavidW Sep 25 '21 at 17:12
  • The type of self.xml_data is I'm using xml objectify. I didnt give a type to cython cuz it was giving an error saying that I cant insert lxml(...) into an object variable. – DeadSec Sep 25 '21 at 17:15
  • So it looks like `ObjectifiedElement` special-cases both [`__dict__`](https://github.com/lxml/lxml/blob/0c9a2198e4855ca1274c2bd5b2e6a9dbba9f8288/src/lxml/objectify.pyx#L170) (which is what `hasattr` uses) [`__getattr__`](https://github.com/lxml/lxml/blob/0c9a2198e4855ca1274c2bd5b2e6a9dbba9f8288/src/lxml/objectify.pyx#L225) so my initial guess about `cdef` attributes is definitely wrong. I'm afraid I don't know the answer, but I suspect there is enough information for someone who knows lxml to investigate – DavidW Sep 25 '21 at 17:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237488/discussion-between-deadsec-and-davidw). – DeadSec Sep 25 '21 at 17:24

0 Answers0