-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use adapter factory to change signature depending on instance #263
Comments
Why do you want to be able to do this? Overall it doesn't even make sense to me why it would be needed or whether it make sense. This is because signatures in Python are normally based on the unbound method prototype attached to the class and not something that dynamically adapts itself per instance of the class. |
Thanks for the response @GrahamDumpleton. Let me explain a bit more since I know it does sound an odd thing to do. I have a
At runtime these data loading functions are run with the arguments provided by the user (e.g. through a URL query parameter):
So far everything is ok. Next I wanted to incorporate flask-caching in order to memoize the loading calls. Flask-caching enables you to set a
The above works great in most contexts, but the following will not generate the same cache key:
There is functionality built into flask-caching to make this work correctly, but it relies on inspection of the function being memoized. Unfortunately, this function always appear as
The problem is I can't find any way to achieve this. Possibly the easiest solution is to just
In this case the signature of |
I'll have to think about it, but first quick glance through (I could have misunderstood things), you might want to use the special If I get a chance later I will try and get together and example of what am thinking of. |
BTW, why not just do something like:
|
That would be possible (it's one answer suggested on this question on stackoverflow) but would not work with the following, which is actually how
I could also implement the custom behaviour to ensure that when |
If |
Indeed that is possible, but it starts getting a bit awkward since in reality there will be more than one argument like
No doubt this could be made less repetitive using some sort of I don't mean to dismiss all your suggestions here - they're definitely very valid ideas! At the moment I am doing my workaround of |
But doesn't creating the memoize wrapper on each load call resulting in all cached data being discarded each time? Doesn't the memoize instance need to persist across calls with the same one being called through each time in order to get what you want? |
No, it seems to work ok, although now you mention it I am not entirely sure why... Indeed it doesn't feel like the right thing to do for that reason. I suppose that flask-caching |
Hello and thank you for you this amazing library! I started using
wrapt
because I wanted access to theinstance
inside my decorator. I'd now like to take this a bit further to useinstance
in theadapter
but not sure if it's possible.I know that adapter factories can be used to alter a signature at runtime based on
wrapped
, but is it possible to somehow useinstance
also? Or am trying to do something that's just too weird here?Possibly related: #232.
Here's some code to illustrate what I'm trying to do. Thank you very much!
The text was updated successfully, but these errors were encountered: