4 ms·
Does this "just work"? How does it handle classes? I've been writing my python code in a style that can be hotswapped. Roughly, I use global functions and pass
by sillysaurusx 6y ago
Does this "just work"? How does it handle classes?
I've been writing my python code in a style that can be hotswapped. Roughly, I use global functions and pass around structs, like C. The function calls are made with `api.foo(data)` instead of `data.foo()`. Then I can just replace `api.foo`'s definition with whatever I want after attaching with a debugger. Example: https://github.com/shawwn/gpt-2/blob/e4868225382e8a475ef3c3d6cfdc3192aba0f2b4/train_multi.py#L475-L499 https://github.com/shawwn/gpt-2/blob/e4868225382e8a475ef3c3d...
I assume there's a much better way to do this, but the class system seems to make it hard to swap things out. Doesn't "isinstance" break if you redefine the class?
EDIT: Oh, this isn't actual hot-reloading for python. Darn. I'm really hoping someone will tackle that... Cool project though!
EDIT2: Actually, this project does seem to reload the modules: https://github.com/say4n/hotreload/blob/0cf3a0b466466f99f8c7ccdb546fcef4553617f9/hotreload/reloader.py#L47-L48 https://github.com/say4n/hotreload/blob/0cf3a0b466466f99f8c7... ... so the original question stands.
By the way, you may want to use `import traceback; traceback.print_exc()` to log exceptions. I'm not sure how that plays with python's `logger` class though.
- oefrha 6y agoimportlib.reload does not "just work". https://docs.python.org/3/library/importlib.html#importlib.reload https://docs.python.org/3/library/importlib.html#importlib.r... documents what is done and not done. > Doesn't "isinstance" break if you redefine the class? With importlib.reload, yes. You might be interested in IPython's autoreload implementation, which does not break isinstance: https://github.com/ipython/ipython/blob/f8c9ea7db42d9830f16318a4ceca0ac1c3688697/IPython/extensions/autoreload.py#L253-L417 https://github.com/ipython/ipython/blob/f8c9ea7db42d9830f163... However, you could still end up with semi-broken instances, for instance if you update class A: pass to class A: def __init__(self): self.attr = "something" def __str__(self): return self.attr > By the way, you may want to use `import traceback; traceback.print_exc()` to log exceptions. I'm not sure how that plays with python's `logger` class though. PSL logging has builtin integration with exception tracebacks. See Logger.exception [1] or the exc_info keyword param to Logger.debug/info/warning/error/critical/log [2]. [1] https://docs.python.org/3/library/logging.html#logging.Logger.exception https://docs.python.org/3/library/logging.html#logging.Logge... [2] https://docs.python.org/3/library/logging.html#logging.Logger.debug https://docs.python.org/3/library/logging.html#logging.Logge...
- aidos 6y agoI couldn’t live without ipython autoreload. You need to know where the limitations are that require you to create your instances again. Things like sqlalchemy model changes run into issues because of the meta class magic (I think, not looked too deeply). I feel like this library is a bit misrenamed. It seems to be more like “autorun”. Edit: here’s the ipython config I use to turn it on https://github.com/aidos/dotfiles/blob/master/_config/ipython/profile_default/ipython_config.py https://github.com/aidos/dotfiles/blob/master/_config/ipytho...
- viraptor 6y agoUsing logger.exception("message") would also log the trace by default.