Add remove_hook to undo the effect of set_hook
See original GitHub issueDespite the documentation says that we should not need to call set_hook
directly, I do have a use case where using set_hook
would result in a nicer design. If there is a way to put up something (set_hook
), there should also be a way to put it down (the proposed remove_hook
here).
Story:
I have an Adapter design pattern where marshmallow schemas are used. We may have one adapter that does one thing, another adapter does something different, but both want to use the same schema. One adapter can use set_hook
to add hooks without affecting another adapter.
Something like this:
class MyAdapter:
def load(self, schema):
schema.set_hook(my_post_load_function, (POST_LOAD, pass_many))
try:
return schema.load(...)
finally:
# remove_hook is proposed here
schema.remove_hook(my_post_load_function, (POST_LOAD, pass_many))
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Function that remove specific lambda from a hook in Emacs
and use it in remove-hook-name-lambda like in add-to-list : (defun remove-hook-name-lambda (name hook-name) (set hook-name ...
Read more >hook.Remove - Garry's Mod Wiki
The unique identifier of the hook to remove, usually a string. Example. Darkens the player's screen for 15 seconds. hook.Add( " ...
Read more >How To Manage State with Hooks on React Components
The action is an object with two properites: type and price . The type can be either add or remove , and the...
Read more >System Wide Hooking for the WM_CHAR Message
Create a Win32 application and add two menus: 'Set Hook' and 'Remove Hook'. Make changes to the application's window procedure.
Read more >Hooks (GNU Emacs Manual)
You can clear out individual functions by calling remove-hook , or do (setq hook-variable nil) to remove everything. If the hook variable is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you @sloria! Yes it does.
We are in agreement here 😃 I’m sorry that I should have presented code like you did, for it is much clearer. We also try to unset the context to avoid side-effect on the schema being passed from elsewhere (i.e. the “remove_hook”).
This is what we are doing (more or less):
For some reason I thought we were just working around a missing feature with this solution. Assuming this is how such use case is expected to be solved, I will close this issue. Please feel free to reopen it for other reasons though. Thank you!
Sorry, to clarify: rather than dynamically adding and removing the post_load method, you can read the schema context in the method and branch there. Along the lines of:
Does that meet your use case?