2011-12-07 13 views
13

कई बार मैं वास्तव में फ़ाइल में लिखने से पहले चर के निरीक्षण और आदेशों के माध्यम से चरणों का निरीक्षण करने के लिए पाइथन दुभाषिया का उपयोग करूंगा। हालांकि अंत में मेरे पास दुभाषिया में लगभग 30 कमांड हैं, और उन्हें चलाने के लिए उन्हें फ़ाइल में कॉपी/पेस्ट करना होगा। क्या कोई तरीका है कि मैं पाइथन दुभाषिया इतिहास को फ़ाइल में निर्यात/लिख सकता हूं?एक फ़ाइल में पाइथन दुभाषिया इतिहास निर्यात करें?

उदाहरण

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

लिए

और फिर मैं interactions.py फ़ाइल को खोलने और पढ़ सकते हैं:

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

कॉपी और पेस्ट के साथ क्या गलत है? आप '>>>' को ठीक करने के लिए सरल खोज/प्रतिस्थापन का उपयोग कर सकते हैं। क्या यह सबसे आसान नहीं होगा? –

+0

इनपुट की तीस पंक्तियों को प्रतिलिपि/पेस्ट करना दिन में कुछ बार थकाऊ हो जाता है ... –

उत्तर

21

IPython पर अपने अजगर सांत्वना इतिहास की बचत होगी अगर आप इंटरैक्टिव सत्र का उपयोग कर की तरह अत्यंत उपयोगी है। उदाहरण के लिए, आपके उपयोगकेस के लिए सेव कमांड है, तो आप my_useful_session.py में इनपुट लाइन 10 से 20 और 23 को सहेजने के लिए my_useful_session 10-20 23 को इनपुट करते हैं। (इसके साथ मदद करने के लिए, प्रत्येक पंक्ति को इसके नंबर से उपसर्ग किया जाता है)

सुविधाओं का त्वरित अवलोकन पाने के लिए दस्तावेज़ पृष्ठ पर वीडियो देखें।

:: या ::

एक way यह करने के लिए नहीं है। मुक्त करने के लिए में ~/.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 

तुम भी जोड़ सकते हैं फ़ाइल की दुकान:

readline.parse_and_bind('tab: complete') 

कृपया ध्यान दें कि यह केवल * nix सिस्टम पर काम करेंगे। जैसा कि रीडलाइन केवल यूनिक्स मंच में उपलब्ध है।

+1

आईपीथॉन संकेत – silvado

+1

के लिए +1 आईपथन का उपयोग करें, यह पायथन क्लि का भविष्य है। – Will

5

निम्नलिखित अपने ही काम नहीं है, लेकिन स्पष्ट रूप से मुझे याद नहीं है, जहां मैं पहली बार मिला यह ... हालांकि: अपने घर फ़ोल्डर में फ़ाइल (नाम जीएनयू/लिनक्स सिस्टम पर) रखें (फ़ाइल का नाम .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 

तब आप सभी उपहारों को प्राप्त करेंगे जो बैश खोल के साथ आते हैं (इतिहास को नेविगेट करने वाले ऊपर और नीचे तीर, रिवर्स सर्च आदि के लिए ctrl-r ....)।

आपका पूरा आदेश इतिहास: ~/.pyhistory पर स्थित फ़ाइल में संग्रहीत किया जाएगा।

मैं इसे उम्र से उपयोग कर रहा हूं और मुझे कोई समस्या नहीं मिली।

एचटीएच!

11

चाहिए आप लिनक्स/मैक का उपयोग करने और ReadLine पुस्तकालय है रहे हैं, तो आप एक फ़ाइल के लिए निम्न जोड़ सकते हैं और अपने .bash_profile में निर्यात और आप करेंगे दोनों पूर्णता और इतिहास है।

# 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 

निर्यात आदेश:

export PYTHONSTARTUP=path/to/.pythonstartup 

यह ~/.pythonhistory

1

अजगर खोल में:

%history 

आदेश सभी आदेशों आप वर्तमान अजगर खोल में प्रवेश किया है प्रिंट होगा।

% history -g 

कमांड कुछ महत्वपूर्ण संख्याओं तक पाइथन खोल में लॉग इन सभी आदेशों को मुद्रित करेगा।

%history -g -f history.log 

लाइन नंबर के साथ लॉग कमांड लिखेंगे। आप gvim का उपयोग कर ब्याज के आदेशों के लिए निश्चित चौड़ाई रेखा संख्या को हटा सकते हैं।

संबंधित मुद्दे