Giter Site home page Giter Site logo

Comments (6)

christophercrouzet avatar christophercrouzet commented on August 22, 2024

Hi Luke!

Theorically it should be possible to add a sort of undo functionality but in practice I'm not sure if that would be very recommandable. I mean, it's already not so clean to do monkey patching in the first place, so patching and unpatching a same object over and over again seems like it could easily get out of control.

As for the module reloading function, this is out of the scope of this library but this is exactly the approach that I chose to speed up development when working on the banana.maya library. I ended up writing a unload_user_modules() function that I would call at the begin of my code, and then I would call again the gorilla patching method to patch my latest changes on a fresh instance of the reloaded modules.

The cool thing about it is that it reloads ALL the modules located in your home directory, if that's where you put your Python sources, but you could easily rewrite it to fit your needs. Here it is:

def unload_user_modules():
    module_names = []
    home_path = os.path.expanduser('~')
    for name, module in sys.modules.iteritems():
        if getattr(module, '__file__', '').startswith(home_path):
            module_names.append(name)

    for name in module_names:
        del sys.modules[name]

And here's an example of its usage at the begin of a script to run:

import utils
utils.unload_user_modules()

import banana.maya
banana.maya.patch()

[...]

I hope it helps, cheers!

from gorilla.

mrharris avatar mrharris commented on August 22, 2024

Thanks Christopher I'll try with this!

from gorilla.

jayvdb avatar jayvdb commented on August 22, 2024

unpatch support would be useful, especially for use with a context manager.
https://pypi.org/project/python-monkey-business/ and https://pypi.org/project/patchy/ support it.

from gorilla.

christophercrouzet avatar christophercrouzet commented on August 22, 2024

Hello @jayvdb, thanks for chiming in!

Would you have some use cases to share that could help me to understand where unpatching could be useful?

from gorilla.

jayvdb avatar jayvdb commented on August 22, 2024

streeter/django-db-readonly#22 is the case I was working on where it would be nice to use a managed toolkit for the patching, and a comment there shows another project doing similar patch/unpatch inside context managers.

https://github.com/bskinn/stdio-mgr is another example of a project I've worked on that comes to mind, where nestable context managers needed to patch another library.

from gorilla.

christophercrouzet avatar christophercrouzet commented on August 22, 2024

Hello @jayvdb,

I guess this won't cost anything to implement it so here it is: #4!

Please let me know if that would work for you!

from gorilla.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.