question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add remove_hook to undo the effect of set_hook

See original GitHub issue

Despite 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kitchoicommented, Feb 7, 2020

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):

class MySchema(Schema):

    @post_load
    def postprocess(self, data):
        for func in self.context.get("post_loads", []):
            data = func(data)
    return data

class MyAdapter:
    def load(self, schema):
        old_context = self.schema.context
        self.schema.context = old_context.copy()
        self.schema.context["post_loads"] = [func1, func2, ...]
        try:
            self.schema.load(...)
        finally:
            self.schema.context = old_context

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!

0reactions
sloriacommented, Feb 6, 2020

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:

class MySchema(Schema):
    @post_load
    def postprocess(self, data, **kwargs):
        if self.context["foo"] == ...:
            do_something(data)
        else:
            do_something_else(data)

# -----

class MyAdapter:
    def load(self, schema):
        schema.context['foo'] = ....
        return schema.load(...)

Does that meet your use case?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found