Beefing up the Python Shell to build apps faster and DRYer
benplesser.com
benplesser.com
For iPython, you'd want to implement one change log per source file, so for a source.py file, there'd be a source.py.log file in the same directory. Runtime changes would first write a patch to the source.py.log file at parse/compile time. Then if you ever crash iPython after doing an hour of exploratory coding, iPython could reload your changes after the source.py file. (Actually, you'd want a little ui to come up, giving you the option of leaving off the last few changes, so you can omit something that causes a crash.)
I use it with level 1 and %aimport magic all the time and it's really sweet, especially with %ed. It probably does not work well with Django models however. You could use deep_reload: http://ipython.org/ipython-doc/stable/api/generated/IPython.... but I'm not sure if it would be enough.
EDIT: You seriously just changed my life with %autoreload
I agree that the real solution here is "write unit tests", but even if you like an interactive interpreter, seriously, just restart it, it should take a half a second at most (and yes, reload() is broken by design, though sometimes it happens not to matter).
The builtin Python shell is admittedly faster to start/stop but IPython's features rock.
FWIW you might also like checking out https://github.com/ludios/Pyquitter. I use it occasionally when I want something like this, you could rig it to start iPython's main().
It also doesn't use inotify but that'd be fairly trivial to add.
The big (but admittedly) dangerous feature of my script is that you can maintain state inside of the embedded shell on module reload.
I like pyquitter looks like a cool generalization, though, of the basic idea.
-i When a script is passed as first argument or the -c option is
used, enter interactive mode after executing the script or the
command. It does not read the $PYTHONSTARTUP file. This can be
useful to inspect global variables or a stack trace when a
script raises an exception.
http://man.cx/pythonBut with a Doctest model you just develop a script, and if you change something rerun the script, starting where you left off (assuming the script reruns – if it doesn't then you probably want to start where it fails). You can extend the script without reloading still, but changing the past requires starting over... but it's just CPU cycles, assuming you aren't doing something computationally expensive. But even if so, you could have a kind of cross-session Pickling memoize function if you don't want to recalculate things.
These reloading tricks are fragile and break in weird ways, like it won't fix badly initialized data, or classes that can't be upgraded because of state changes. It leads ultimately to a distrust of the environment, until you throw your hands up and go back to the old restart-frequently model like everyone else. Recursive reloading certainly isn't new, but it's never satisfying.
"ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better introspection with the same interface as the pdb module."
| Django models.py modules can’t be reloaded
| normally due to the AppCache singleton