我是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
但我已经筋疲力尽了。我花了整整三天的时间写下下面的代码。在
^{pr2}$我希望让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()计算Legendre符号: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,您可以重用它们。但笔记的内容是一个整体。普通人在写了很多糟糕的表情之后,写了一个有价值的表情。在许多情况下,有价值的表达式依赖于一些前向表达式。把这些依赖的表达式组合起来是很麻烦的。所以纸条被弄得乱七八糟。在
一年后,当你想重复使用有价值的表达方式时,你会忘记笔记的细节,很难再使用有价值的表达方式。因为你必须记住整个细节。在
但是每一个Python一行程序都是自己完成的。即使几年后你也可以很容易地重复使用它们。您可以很容易地合并Python一行程序,因为它们是函数式编程。在
您可能能够比IPython中的Python表达式更容易地处理Python一行程序。在
我从修改你的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吗?在
最后一个问题。 我想按一下接受按钮。但我不知道它在哪里。”这个帖子对你有用吗?是/否按钮“可能不是。在
对于任何偶然发现这一点的人,我修改了@chrisbarrett,以防止新行被python的
print
追加。另外,由于某些原因,使用-m module
导入模块使命令没有返回任何输出,因此我在literal命令中导入模块。在下面是我突然想到的:
您可以使用
M-x eval-line-in-python
运行它。在我更改了它,使它不使用临时文件,而是直接计算该行。如果你还想写一个临时文件,这只是一个小小的改变。在
相关问题 更多 >
编程相关推荐