Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Giao diện GNU readline trong Python

Module readline là module chuyên dụng cho UNIX. Nó định nghĩa nhiều hàm để đọc và ghi file lịch sử (history files) dễ dàng hơn từ trình thông dịch Python. Ta có thể dùng module này trực tiếp hoặc qua module rlcompleter. Cài đặt module này có thể ảnh hưởng đến prompt của phương thức built-in input() và prompt tương tác.

Trên hệ thống MAC (MAC OS X), module readline có thể được triển khai bằng thư viện libedit. Cấu hình libedit khác với GNU readline.

Để dùng module này, cần nhập module readline trong mã Python:

import readline

Một số phương thức của GNU readline:

STTHàm & Mô tả
1readline.parse_and_bind(string) – Lấy một dòng từ file init readline và thực thi sau khi parse
2readline.get_line_buffer() – Lấy nội dung hiện tại của line buffer
3readline.insert_text(string) – Chèn text vào dòng lệnh
4readline.read_init_file([filename]) – Parse file khởi tạo readline. Mặc định là giá trị cuối cung cấp
5readline.read_history_file([filename]) – Đọc lịch sử từ file cho trước. Mặc định ~/.history
6readline.write_history_file([filename]) – Lưu lịch sử vào file cho trước. Mặc định ~/.history
7readline.clear_history() – Xóa lịch sử hiện tại
8readline.get_history_length() – Lấy độ dài tối đa của file lịch sử
9readline.set_history_length(length) – Thiết lập độ dài file lịch sử (số dòng)
10readline.get_current_history_length() – Lấy tổng số dòng trong file lịch sử
11readline.get_history_item(index) – Lấy mục lịch sử bằng index
12readline.remove_history_item(pos) – Xóa lịch sử bằng vị trí
13readline.replace_history_item(pos, line) – Thay thế lịch sử bằng vị trí
14readline.redisplay() – Hiển thị nội dung hiện tại của line buffer
15readline.get_begidx() – Lấy chỉ mục bắt đầu phạm vi tab-completion
16readline.get_endidx() – Lấy chỉ mục kết thúc phạm vi tab-completion
17readline.add_history(line) – Thêm một dòng vào cuối history buffer

Mã dưới đây dùng để đọc file lịch sử và lưu nó trong thư mục home. Mã sẽ chạy khi biên dịch và chạy ở chế độ tương tác. Sau khi thoát shell Python, nó sẽ lưu file lịch sử.

Mã ví dụ

import readline as rl
import os
import atexit

my_hist_file = os.path.join(os.path.expanduser("~"), ".my_python_hist")
try:
    rl.read_history_file(my_hist_file)
    rl.clear_history()
except FileNotFoundError:
    pass
print("Done")
atexit.register(rl.write_history_file, my_hist_file)
del os, my_hist_file

Chạy trong shell tương tác:

$ python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open("./readline_task.py").read())
Done
>>> print("readline_task.py is executed")
readline_task.py is executed
>>> print("History File will be updated after exit.")
History File will be updated after exit.
>>> 2 ** 10
1024
>>> 2 ** 20
1048576
>>> 2 ** 30
1073741824
>>> import math
>>> math.factorial(6)
720
>>> exit()
$ cat ~/.my_python_hist
print("readline_task.py is executed")
print("History File will be updated after exit.")
2 ** 10
2 ** 20
2 ** 30
import math
math.factorial(6)
exit()
$