gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:12:06 27 4
gpt4 key购买 nike

我是 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

但是我已经筋疲力尽了。我花了整整 3 天时间写下以下代码。
(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 one-liners,因为它们是函数式编程。

与 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 添加了打印指令。
' 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

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

您可以在 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 - 我想要一个在光标下执行 Python 单行的 elisp 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14784347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com