GrahamDumpleton / wrapt

A Python module for decorators, wrappers and monkey patching.
BSD 2-Clause "Simplified" License
2.03k stars 231 forks source link

Best way to associate some data with `ObjectProxy`? #255

Open Trung0246 opened 9 months ago

Trung0246 commented 9 months ago

Current my implementation works like this:

DATA_DB = weakref.WeakKeyDictionary()

def set_data_db(wrapper_instance, data):
    DATA_DB[id(wrapper_instance)] = data

def get_data_db(wrapper_instance):
    return DATA_DB.get(id(wrapper_instance), None)

class Wrapper(wrapt.ObjectProxy):
    def __init__(self, wrapped, extra_data=None):
        super().__init__(wrapped)
        # Set the initial extra data using the external function
        set_data_db(self, extra_data)

But the moment when Wrapper is Wrapper(123) or some other primitive values, this becomes unusable. I have also attempted to do the following:

class Wrapper(wrapt.ObjectProxy):
    def __init__(self, wrapped, data=None):
        super().__init__(wrapped)
        # Initialize a separate dict for the Wrapper-specific attributes
        self._self_dict = {}
        self._self_dict['_data'] = data

    @property
    def data(self):
        # Access the special member variable from the Wrapper-specific dict
        return self._self_dict['_data']

    @data.setter
    def data(self, value):
        # Set the special member variable in the Wrapper-specific dict
        self._self_dict['_data'] = value

But unsure if it's reliable for the case when self itself have data or _self_dict attribute when looking at Proxy Object Attributes section in the docs.

GrahamDumpleton commented 9 months ago

Can you give a high level explanation with usage examples of what you are trying to do rather than ask about what you think is the solution. I need to understand what your goal is to be able to suggest the best way, or even if you should be using wrapt at all, since it isn't a magic solution for everything and often not even needed.

Anyway, what you are asking for sort of mirrors what some people have asked for before but never had a good solution. That is, maintain an attribute on the wrapper which isn't propagated through to the wrapped object, but not require a _self_ prefix.

Now for whatever reason an idea of how to solve it just popped into my head for some reason. Rather funny that it has taken me so long to realise this could work.

import wrapt

class Wrapper(wrapt.ObjectProxy):

    # Declare class attribute so lookup will work even
    # though going to use the attribute on the instance.

    data = "wrapper"

    def __init__(self, wrapped, data=None):
        super().__init__(wrapped)

        # Since setting on instance, will convert the class
        # attribute to an instance variable. The original
        # class attribute declaration means it will not
        # be on the wrapped object.

        self.data = data

class Wrapped: pass

o = Wrapped()

p1 = Wrapper(o, "p1")
p2 = Wrapper(o, "p2")

print(Wrapper.data)

print(p1.data)
print(p2.data)

p1.data = "p1-update"
p2.data = "p2-update"

print(Wrapper.data)

print(p1.data)
print(p2.data)

print(dir(o))

o.data = "wrapped"

print(o.data)

print(Wrapper.data)

print(p1.data)
print(p2.data)

print(dir(o))

Which yields:

wrapper
p1
p2
wrapper

p1-update
p2-update
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

wrapped
wrapper
p1-update
p2-update
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'data']
Trung0246 commented 9 months ago

Thanks. It's kinda embarrassing tho since I'm implementing some monkey patching for data hijack through a python system. The entire system have some sort of data passing around, each data can be handled with some sort of callback. The thing is the callbacks are kind of limited for my need but I found this way to workaround with it, which is to to associate some data with since modifying the system itself is basically a game of data chasing.

I guess the linked issue is a dupe of this then?

GrahamDumpleton commented 9 months ago

Can't say whether the other issue which I pointed at this one is a dupe or not since ultimately don't fully understand what your or other posters goals really are.

Anyway, here is another way of tricking the Python attribute lookup mechanism.

class Wrapper(wrapt.ObjectProxy):

    # Declare a slot for the attribute so lookup will work against
    # the instance and not go through to the wrapped object.

    __slots__ = ["data"]

    def __init__(self, wrapped, data=None):
        super().__init__(wrapped)

        self.data = data

IOW. Instead of defining a dummy class attribute, add __slots__ array and list data in it.