2010-08-31 21 views
8

Có bất kỳ thời gian phổ biến nào mà mọi người đưa vào Python Interactive Startup scripts không? Tôi đã thực hiện một dopey một để giúp tôi biết nơi tôi khi tôi cố gắng để làm các hoạt động tập tin tương đối hoặc import s, bằng cách sử dụng một mô-đun win32 để thay đổi tên của cửa sổ giao diện điều khiển.Kịch bản khởi động tương tác Python của bạn là gì?

import sys 
import os 
import win32api 
__title_prefix = 'Python %i.%i.%i %s %s' % (sys.version_info[0:4] + 
              (sys.version_info[4] or "",)) 

def __my_chdir(path): 
    __os_chdir(path) 
    win32api.SetConsoleTitle(__title_prefix + " - " + os.getcwd()) 

# replace chdir func 
__os_chdir = os.chdir 
os.chdir = __my_chdir 

os.chdir(r'C:\Scripts') 
+0

Mặc dù đây có câu trả lời tuyệt vời mà tôi thêm vào kịch bản khởi động riêng của tôi, tôi đã bỏ phiếu để đóng này cho là chủ yếu ý kiến dựa trên. – ArtOfWarfare

Trả lời

4
# 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 

readline.parse_and_bind("tab: complete") 
historyPath = os.path.expanduser("~/.history.py") 

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 
+0

Chỉ Unix từ giao diện của http://docs.python.org/library/readline.html –

+3

@Nic gì ?! Có hệ thống UNIX không? :-) – Anycorn

+0

Psh, tôi phát triển phần lớn thời gian, vì vậy việc sử dụng * nix thực tế là không thể đối với tôi, vì phần lớn các trình biên dịch, lập trình viên và trình gỡ rối đều là Windows được nhắm mục tiêu. –

4

tôi cũng sử dụng see, một dễ dàng hơn trên các thay mắt cho dir của Python.

from see import see 

Một ví dụ về sử dụng see như trái ngược với dir sau:

>>> k = {} 
>>> dir(k) 
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__','__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', 
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 
>>> see(k) 
    []    in    <    <=    == 
    !=    >    >=    hash()   help() 
    iter()   len()   repr()   str()   .clear() 
    .copy()   .fromkeys()  .get()   .has_key()  .items() 
    .iteritems() .iterkeys()  .itervalues() .keys()   .pop() 
    .popitem()  .setdefault() .update()  .values() 
Các vấn đề liên quan