1

I am working with htcondor python bindings (https://htcondor.readthedocs.io/en/latest/apis/python-bindings/index.html)

You don't need to know htcondor, but for reference I am working with the htcondor.JobEvent class to get the data that I want. From the description of that object it follows that it behaves like a dictionary, but it has no __dict__ property.

Basically you can't tell what this object is, because it's translated from C++ into python, hence I want to wrap it with all it's functionalities to add more functionalities.

The way I am solving this atm is:

class HTCJobEventWrapper:
    """
    Wrapper for HTCondor JobEvent.

    Extracts event number and time_stamp of an event.
    The wrapped event can be printed to the terminal for dev purpose.

    :param job_event: HTCJobEvent
    """

    def __init__(self, job_event: HTCJobEvent):
        self.wrapped_class = job_event
        self.event_number = job_event.get('EventTypeNumber')
        self.time_stamp = date_time.strptime(
                job_event.get('EventTime'),
                STRP_FORMAT
            )

    def __getattr__(self, attr):
        return getattr(self.wrapped_class, attr)

    def get(self, *args, **kwargs):
        """Wraps wrapped_class get function."""
        return self.wrapped_class.get(*args, **kwargs)

    def items(self):
        """Wraps wrapped_class items method."""
        return self.wrapped_class.items()

    def keys(self):
        """Wraps wrapped_class keys method."""
        return self.wrapped_class.keys()

    def values(self):
        """Wraps wrapped_class values method."""
        return self.wrapped_class.values()

    def to_dict(self):
        """Turns wrapped_class items into a dictionary."""
        return dict(self.items())

    def __repr__(self):
        return json.dumps(
            self.to_dict(),
            indent=2
        )

With this it's possible to get any attribute and to use the methods described in the documentation.

However as you can see HTCJobEventWrapper is not of type htcondor.JobEvent and is not inheriting from it. If you try to instatiate a htcondor.JobEvent class it results in the following error: RuntimeError: This class cannot be instantiated from Python.

What I want: I would like it to be a child class which copies a given htcondor.JobEvent object completely and adds the functionalities I want and returns a HTCJobEventWrapper object

This kind of relates to this question: completely wrap an object in python

Is there a pythonic way to dynamically call every attribute, function or method on self.wrapped_class ? Just like with getattr ? But in this case I've tried getattr but it works only for attributes.

Krotonix
  • 25
  • 5
  • Why can't you simply subclass `HTCJobEventWrapper`, override the methods you like and use `super()` as needed? If you inherit from `HTCJobEventWrapper` then objects of this new type *are* considered to be `HTCJobEventWrapper` objects (in the sense that `isinstance(obj, HTCJobEventWrapper)` returns `True`). – a_guest Nov 18 '21 at 10:55
  • I can't cause I don't know how to initialize this htcondor.JobEvent, i really don't know nothing about it. That's the point here. – Krotonix Nov 18 '21 at 11:01
  • The `__init__` method ships with `HTCJobEventWrapper` so you don't need to know its details. – a_guest Nov 18 '21 at 11:15
  • A I see but this only answers my question partly. What if I have an existing `htcondor.JobEvent` object that I want to turn into a HTCJobEventWrapper ? Consider that I would like to dynamically call any attribute or method on the original wrapped object. – Krotonix Nov 18 '21 at 11:50
  • I have tried this: I get the following result: `RuntimeError: This class cannot be instantiated from Python ` – Krotonix Nov 18 '21 at 11:59
  • Can you clarify why it is relevant that your wrapper *is a* ``htcondor.JobEvent``? You cannot *completely* wrap an object and even a child class is not a complete replacement for the parent. What does the wrapper have to provide in practice? – MisterMiyagi Nov 18 '21 at 12:06
  • @Krotonix So you want a wrapper object like the one you created *and* make it behave as if `isinstance(obj, HTCJobEventWrapper)`? – a_guest Nov 19 '21 at 11:07

0 Answers0