How to save a Python interactive session?

I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, little for loops and bits of logic) -- some history of the interactive session? If I use something like script I get too much stdout noise. I don't really need to pickle all the objects -- though if there is a solution that does that, it would be OK. Ideally I would just be left with a script that ran as the one I created interactively, and I could just delete the bits I didn't need. Is there a package that does this, or a DIY approach?

UPDATE: I am really amazed at the quality and usefulness of these packages. For those with a similar itch:

  • IPython -- should have been using this for ages, kind of what I had in mind
  • reinteract -- very impressive, I want to learn more about visualization and this seems like it will shine there. Sort of a gtk/gnome desktop app that renders graphs inline. Imagine a hybrid shell + graphing calculator + mini eclipse. Source distribution here: http://www.reinteract.org/trac/wiki/GettingIt . Built fine on Ubuntu, integrates into gnome desktop, Windows and Mac installers too.
  • bpython -- extremely cool, lots of nice features, autocomplete(!), rewind, one keystroke save to file, indentation, well done. Python source distribution, pulled a couple of dependencies from sourceforge.
  • I am converted, these really fill a need between interpreter and editor.


    IPython is extremely useful if you like using interactive sessions. For example for your use-case there is the %save magic command, you just input %save my_useful_session 10-20 23 to save input lines 10 to 20 and 23 to my_useful_session.py (to help with this, every line is prefixed by its number).

    Furthermore, the documentation states:

    This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.

    This allows for example, to reference older sessions, such as

    %save current_session ~0/
    %save previous_session ~1/
    

    Look at the videos on the presentation page to get a quick overview of the features.


    http://www.andrewhjon.es/save-interactive-python-session-history

    import readline
    readline.write_history_file('/home/ahj/history')
    

    There is a way to do it. Store the file in ~/.pystartup ...

    # Add auto-completion and a stored history file of commands to your Python
    # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
    # bound to the Esc key by default (you can change it - see readline docs).
    #
    # Store the file in ~/.pystartup, and set an environment variable to point
    # to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
    #
    # Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
    # full path to your home directory.
    
    import atexit
    import os
    import readline
    import rlcompleter
    
    historyPath = os.path.expanduser("~/.pyhistory")
    
    def save_history(historyPath=historyPath):
        import readline
        readline.write_history_file(historyPath)
    
    if os.path.exists(historyPath):
        readline.read_history_file(historyPath)
    
    atexit.register(save_history)
    del os, atexit, readline, rlcompleter, save_history, historyPath
    

    and then set the environment variable PYTHONSTARTUP in your shell (eg in ~/.bashrc ):

    export PYTHONSTARTUP=$HOME/.pystartup
    

    You can also add this to get autocomplete for free:

    readline.parse_and_bind('tab: complete')
    

    Please note that this will only work on *nix systems. As readline is only available in Unix platform.

    链接地址: http://www.djcxy.com/p/22740.html

    上一篇: 什么Git分支模型适合你?

    下一篇: 如何保存Python交互式会话?