Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Emacs中Python预定义的自动导入_Python_Emacs - Fatal编程技术网

Emacs中Python预定义的自动导入

Emacs中Python预定义的自动导入,python,emacs,Python,Emacs,以下是一个导入示例,来自: 这两种进口产品被大量使用。有没有办法做到以下几点: 将“from selenium.webdriver.common.by import by”定义为自动导入建议 在输入“By.”后自动完成建议,即使不导入 选择键入“By”后给出的建议。是否会在第一个代码块中创建导入 我使用yasnippet做类似的事情: # -*- mode: snippet -*- # contributor: jpkotta # name: from __future__ import ..

以下是一个导入示例,来自:

这两种进口产品被大量使用。有没有办法做到以下几点:

  • 将“from selenium.webdriver.common.by import by”定义为自动导入建议
  • 在输入“By.”后自动完成建议,即使不导入
  • 选择键入“By”后给出的建议。是否会在第一个代码块中创建导入

我使用yasnippet做类似的事情:

# -*- mode: snippet -*-
# contributor: jpkotta
# name: from __future__ import ...
# key: future
# --
from __future__ import print_function, division, unicode_literals, absolute_import
如果您不想使用yasnippet,可以使用一个非常简单的命令来执行此操作:

(defun insert-foo ()
  (interactive)
  (let ((str "foo"))
    (save-excursion
      (save-restriction
        (widen)
        (goto-char (point-min))
        (unless (search-forward-regexp (concat "^" (regexp-quote str)) nil t)
          (goto-char (point-min))
          (while (and (looking-at (concat "^" comment-start))
                    (not (eobp)))
            (forward-line 1))
          (insert str))))))
这足够聪明,可以跳过缓冲区开头的注释,并且是幂等的,但不会自动运行。您可以编写一个命令来搜索另一个字符串“By.”,如果找到,则运行insert命令,然后将其添加到例如
python模式钩子
保存钩子之前

(defun insert-foo ()
  (interactive)
  (let ((str "foo"))
    (save-excursion
      (save-restriction
        (widen)
        (goto-char (point-min))
        (unless (search-forward-regexp (concat "^" (regexp-quote str)) nil t)
          (goto-char (point-min))
          (while (and (looking-at (concat "^" comment-start))
                    (not (eobp)))
            (forward-line 1))
          (insert str))))))