python - 我想要一个 elisp 脚本在光标下执行 Python 单行代码

标签 python elisp

我是 Emacs 的大佬。我从本月初开始使用 Emacs。

我想将小型 Vim 脚本移植到 Emacs。这些脚本也将使我们能够在 Emacs 中进行这样的计算。

http://www.youtube.com/watch?v=yDR0dTPu6M4

我尝试移植下面编写的 Vim 脚本。

function! s:ExecPySf_1liner()
    let l:strAt = getline(".")
    call writefile([strAt], "temp.pysf")

    let l:strAt = system("python -u -m sfPP -fl temp.pysf")
    if @0 == 0
        let @0 = l:strAt
    else
        let @0 = l:strAt
    endif

    let @" = @0
    if match(&clipboard, "unnamed") >= 0
        let @* = @0
    endif
    echo @0
endfunction         

但是我已经筋疲力尽了。我花了整整三天的时间写下了下面的代码。

(defun ExecPySf_1liner ()
    (let (  (strAt
             (buffer-substring-no-properties (point-at-bol) (point-at-eol))
            )
         )
    ) 
)

我想让 Emacs 执行以下操作。

1 read one line under the cursor.
2 write down the one line string into temp.pysf file in current directory
3 execute "python -u -m sfPP -fl temp.pysf" in a shell.
4 display the returned calculated string in echo arear
5 and copy the string in the clipboard to enable a user to past the calculated result.

请指出相应的elisp函数或代码。

提前致谢

==================================

嗨,克里斯。我修改了你的代码,如下所示。

(defun __getLineOmittingComment ()
    "Get position after ';;' string. If there is no ;; then return line-beginning-posiion"
    (interactive)
    (goto-char (line-beginning-position))
    (let (( posAt (search-forward ";;" (line-end-position) t) ))
     (if (equal posAt nil) (line-beginning-position) posAt)
    )
)

(defun ExecPySf_1liner()
    "Evaluates the current line in PythonSf, then copies the result to the clipboard."
    (interactive)
    (write-region (__getLineOmittingComment) (line-end-position) "temp.pysf" nil)

    (let ((strAt
           (shell-command-to-string "python -u -m sfPP -fl temp.pysf" )
         ))
        (message strAt)
        (kill-new strAt)))

ExecPySf_1liner() 计算勒让德符号:http://en.wikipedia.org/wiki/Legendre_symbol如下。

import sympy as ts; Lgndr=lambda a,b:(lambda c=a%b:0 if ts.gcd(c,b)!=1 else 1 if any((c-k^2)%b==0 for k in range(1,b//2+2)) else -1)(); [Lgndr(3,p) for p in ts.primerange(3,100)] 
===============================
[0, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1]

You should look at IPython. It comes with an Emacs mode that will do all this and more

我能理解你的意见。但你可能会忽略这样一个事实:Python 的单行代码是函数式编程,并且是在单行代码中完成的。因为他们不使用 if then else 语法。他们必须使用 lambda 函数而不使用 def 函数。尽管它们不是严格引用透明的,但它们是像 elisp 脚本一样的函数式编程。而且数学问题很容易用函数式编程风格编写,如上勒让德符号。

IPython 可以将它们的笔记保存为 Matlab、Mathematica,并且您可以重复使用它们。但笔记的内容整体上却是错综复杂的。普通人在许多糟糕的表达之后写出有值(value)的表达。而有值(value)的表达在很多情况下依赖于一些前向表达。而且将依赖的表达式组合在一起很麻烦。所以这张纸条就乱成了一团。

一年后,当您想重新使用有值(value)的表达时,您会忘记注释的详细信息,并且很难重新使用有值(value)的表达。因为你必须记住整个细节。

但是每个 Python 单行代码都是独立完成的。即使几年后您也可以轻松地重复使用它们。您可以轻松合并 Python 单行代码,因为它们是函数式编程。

与 IPython 中的 Python 表达式相比,您可能能够更轻松地处理 Python 单行代码。


通过修改你的 elisp 代码,我学到了很多东西。我成为了 elisp 爱好者。而且我可能会比 Vim 脚本更熟悉 elisp。非常感谢。

================================================== ==================================

You should look at IPython :). It comes with an Emacs mode that will do all this and more. :) ? I can understand your opinion. But I claim that TO USE Emacs AS IPython is better than TO USE IPython AS Emacs.

我稍微扩展了 Python 的数学功能。 sfPP.py 是一个预处理器,它更改了一行代码,如下所示。你不需要写“print”,因为 sfPP.py 添加了 print 指令。

' xy"' in 'abcd"xy"efg'
===============================
False

type __tempConverted.py
from __future__ import division
# -*- encoding: utf-8 -*-
from pysf.sfFnctns import *
setDctGlobals(globals())
from pysf.customize import *
if os.path.exists('./sfCrrntIni.py'):
    from sfCrrntIni import *
rightSideValueAt__= ' xy"' in 'abcd"xy"efg'
print "==============================="
print rightSideValueAt__
putPv(rightSideValueAt__, '_dt')

'"xy"' in 'abcd"xy"efg'
===============================
True

(这个示例代码也说明了为什么我敢于使用临时单行文件。聪明的Chris会明白原因。)

您可以在 Emacs 中轻松观看 Python 源代码,如下所示。

source(np.source)
In file: C:\Python27\lib\site-packages\numpy\lib\utils.py

def source(object, output=sys.stdout):
        snipped
    import inspect
    try:
        print >> output,  "In file: %s\n" % inspect.getsourcefile(object)
        print >> output,  inspect.getsource(object)
    except:
        print >> output,  "Not available for this object."

===============================
None

You should look at IPython I have been watching IPython youtube video:IPython in-depth by bits to write an article:"Use Vim/Emacs as IPython"

您同意我使用 Emacs 作为 IPython 吗?


最后一个问题。 我想按接受按钮。但我不知道它在哪里。 “这篇文章对您有用吗?是/否按钮”可能不是那个。

最佳答案

这是我想出的东西:

(defun get-current-line ()
  (buffer-substring-no-properties (line-beginning-position)
                                  (line-end-position)))

(defun run-python-command (str)
  (shell-command-to-string
   (concat "/usr/bin/env python -u -m sfPP -c "
           (shell-quote-argument (concat "print(" str ")")))))

(defun eval-line-in-python ()
  "Evaluates the current line in python, then copies the result to the clipboard."
  (interactive)
  (let ((str (run-python-command (get-current-line))))
    (message str)
    (kill-new str)))

您可以使用M-x eval-line-in-python运行它。

我对其进行了更改,以便它不使用临时文件,而是直接评估该行。如果您仍然想写入临时文件,那么这是一个微不足道的更改。

关于python - 我想要一个 elisp 脚本在光标下执行 Python 单行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14784347/

相关文章:

emacs - Emacs 22 中(交互式 "^")的向后兼容性

python - Google App Engine 上论坛应用程序的数据建模建议

python - 记录 getLevelName 方法的实现

emacs - 如何轻松更新 Emacs Lisp 表单的评估结果注释

emacs - 如何修复 *Backtrace* 缓冲区中损坏的文本 (emacs)

emacs - 如何使用 emacs/elisp 获取当前缓冲区信息的开始/结束?

python - 查找两个嵌套列表的交集?

python - 如何使用 Sklearn 提高 SVM 分类器的速度

python - 如何在 R 中迭代时动态修改向量

function - Emacs Lisp 可以将 lambda 形式分配给 Scheme 之类的变量吗?