2011-12-07 38 views
13

Nhiều lần tôi sẽ sử dụng trình thông dịch Python để kiểm tra các biến và thực hiện các lệnh trước khi tôi thực sự ghi vào một tệp. Tuy nhiên vào cuối tôi có khoảng 30 lệnh trong trình thông dịch và phải sao chép/dán chúng vào một tệp để chạy. Có cách nào tôi có thể xuất/ghi lịch sử thông dịch Python vào một tệp không?Xuất lịch thông dịch Python sang tệp?

Ví dụ

>>> a = 5 
>>> b = a + 6 
>>> import sys 
>>> export('history', 'interactions.py') 

Và sau đó tôi có thể mở các tập tin interactions.py và đọc:

a = 5 
b = a + 6 
import sys 
+0

Sao chép và dán có vấn đề gì? Bạn có thể sử dụng tìm/thay thế đơn giản để sửa chữa '>>>'. Nó sẽ không đơn giản nhất sao? –

+0

Sao chép/dán ba mươi dòng đầu vào một vài lần mỗi ngày trở nên tẻ nhạt ... –

Trả lời

21

IPython là vô cùng hữu ích nếu bạn thích sử dụng phiên tương tác. Ví dụ cho usecase của bạn có lệnh save, bạn chỉ cần nhập my_useful_session 10-20 23 để lưu các dòng đầu vào từ 10 đến 20 và 23 vào my_useful_session.py. (để trợ giúp điều này, mỗi dòng được bắt đầu bằng số của nó)

Xem các video trên trang tài liệu để có tổng quan nhanh về các tính năng.

:: OR ::

way để làm điều đó. Lưu trữ các tập tin trong ~/.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 

Bạn cũng có thể thêm này để có được autocomplete miễn phí:

readline.parse_and_bind('tab: complete') 

Xin lưu ý rằng điều này sẽ chỉ làm việc trên hệ thống * nix. Như readline chỉ có sẵn trong nền tảng Unix.

+1

+1 cho gợi ý ipython – silvado

+1

Sử dụng iPython, đó là tương lai của Python cli. – Will

5

Sau đây không phải là việc của riêng tôi, nhưng thẳng thắn mà nói tôi không nhớ nơi tôi mới vào nghề nó ... Tuy nhiên: đặt tệp sau (trên hệ thống GNU/Linux) vào thư mục chính của bạn (tên của tệp phải là .pystartup.py):

# 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, e.g. "export PYTHONSTARTUP=/max/home/itamar/.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") 
historyTmp = os.path.expanduser("~/.pyhisttmp.py") 

endMarkerStr= "# # # histDUMP # # #" 

saveMacro= "import readline; readline.write_history_file('"+historyTmp+"'); \ 
    print '####>>>>>>>>>>'; print ''.join(filter(lambda lineP: \ 
    not lineP.strip().endswith('"+endMarkerStr+"'), \ 
    open('"+historyTmp+"').readlines())[-50:])+'####<<<<<<<<<<'"+endMarkerStr 

readline.parse_and_bind('tab: complete') 
readline.parse_and_bind('\C-w: "'+saveMacro+'"') 

def save_history(historyPath=historyPath, endMarkerStr=endMarkerStr): 
    import readline 
    readline.write_history_file(historyPath) 
    # Now filter out those line containing the saveMacro 
    lines= filter(lambda lineP, endMarkerStr=endMarkerStr: 
         not lineP.strip().endswith(endMarkerStr), open(historyPath).readlines()) 
    open(historyPath, 'w+').write(''.join(lines)) 

if os.path.exists(historyPath): 
    readline.read_history_file(historyPath) 

atexit.register(save_history) 

del os, atexit, readline, rlcompleter, save_history, historyPath 
del historyTmp, endMarkerStr, saveMacro 

Sau đó, bạn sẽ nhận được tất cả các tiện ích đi kèm với bash shell (mũi tên lên và xuống điều hướng lịch sử, ctrl-r để tìm kiếm ngược, v.v ...).

Lịch sử lệnh đầy đủ của bạn sẽ được lưu trữ trong một tệp có tại: ~/.pyhistory.

Tôi đang sử dụng tính năng này từ độ tuổi và tôi chưa bao giờ gặp sự cố.

HTH!

11

Nếu bạn đang sử dụng Linux/Mac và có thư viện readline, bạn có thể thêm dòng sau vào một tập tin và lưu lại với .bash_profile của bạn và bạn sẽ có cả hoàn thành và lịch sử.

# python startup file 
import readline 
import rlcompleter 
import atexit 
import os 
# tab completion 
readline.parse_and_bind('tab: complete') 
# history file 
histfile = os.path.join(os.environ['HOME'], '.pythonhistory') 
try: 
    readline.read_history_file(histfile) 
except IOError: 
    pass 
atexit.register(readline.write_history_file, histfile) 
del os, histfile, readline, rlcompleter 

khẩu lệnh:

export PYTHONSTARTUP=path/to/.pythonstartup 

này sẽ tiết kiệm được diện điều khiển lịch sử python của bạn tại ~/.pythonhistory

1

Trong vỏ python:

%history 

Lệnh sẽ in tất cả các lệnh bạn đã nhập trong vỏ python hiện hành.

% history -g 

Lệnh sẽ in tất cả các lệnh được ghi trong vỏ trấu lên đến một số dòng đáng kể.

%history -g -f history.log 

Sẽ ghi các lệnh đã ghi cùng với số dòng. bạn có thể xóa số dòng cố định chiều rộng cho các lệnh quan tâm bằng gvim.

Các vấn đề liên quan