0

I have a situation like this:

import xml.etree.cElementTree as etree


class Universe(object):
    def get_node(self, label, attribs):
        return etree.Element(
            type(label), label=label, **attribs)

class Planet(Universe):
    def __init__(self, name, nom=0):
        super(Planet, self).__init__()
        self._name = name
        self._nom = nom

    def label(self):
        return self._name

    def no_of_moons(self):
        return self._nom

class Earth(object):
    def __init__(self):
        self.life_support = True
        self.presence_of_water = True
        planet_earth = Planet(self, nom=1)
        self.node = planet_earth.get_node(self, self.__dict__)

earth = Earth()
print earth.node

so I get output of earth like this <Element <class 'Earth'> at 0x108fa1ed0>

but instead I want output like: <Element Earth(life_support=True, nom=True)>

now I know I can override __repr__ to result something like above, but etree.Element is a function of the type 'builtin_function_or_method and I don't know how can output that can something be useful for debugging.

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0

you have to implement a repr function for the object in question.

To change the output of Earth you can add a repr function to the earth class.

changing repr directly of the element doesn't work nicely as this class doesn't seem to allow monkeypatching (Many classes allow to monkeypatch an instance method)

An example that you have to adapt to your needs would be:

class Earth(object):
    def __init__(self):
        self.life_support = True
        self.presence_of_water = True
        planet_earth = Planet(self, nom=1)
        self.node = planet_earth.get_node(self, self.__dict__)

    def __repr__(self):
        return("<Element %s(life_support=%s, nom=%s)" %
               (self.__class__.__name__,
                self.life_support,
                self.node.tag,  # display xml tag of element
                # or self.node.attrib["name"],
               ))

earth = Earth()
print(earth)  # this looks now nicer
print(earth.node)  # this still looks ugly
etree.dump(earth.node)  # sends the xml of the given node to stdout

Please note, that above code was tested with python3 and not with python2

gelonida
  • 5,327
  • 2
  • 23
  • 41
  • this case earth.node returns ` at 0x101d2eed0> ` – Ciasto piekarz Sep 17 '19 at 22:02
  • well you know your output is still what I am getting but instead what I want is *I want output like* in opening post. – Ciasto piekarz Sep 17 '19 at 22:12
  • Apologies. My ouptut looks only different if you do print earth (and not earth.node) If you really insist on having a different repr for the node element itself, then you had to extend the element class. Will send you an example in a few minutes. Extending a class should work along the lines of https://stackoverflow.com/questions/15404256/changing-the-class-of-a-python-object-casting#15404554 – gelonida Sep 17 '19 at 22:13
  • Well the Element class is higly resistant to monkeypatching. So my idea doesn't work. For debugging you can print out: (etree.dump(earth.node) but overloading of Element doesn't work well – gelonida Sep 17 '19 at 22:31