Can i use this script as a python evaluator?

P

Peter Wang

<code>
#! /bin/sh
python -c "import sys;exec(sys.stdin)"
</code>

Emacs has a function `shell-command-on-region', which takes region as
input for the evaluator (script above), and output its result. I have
tried and found it works, is there any problems for this, or any other
better solution for it? Thanks.
 
B

Bruno Desthuilliers

Peter Wang a écrit :
<code>
#! /bin/sh
python -c "import sys;exec(sys.stdin)"
</code>

Emacs has a function `shell-command-on-region', which takes region as
input for the evaluator (script above), and output its result. I have
tried and found it works, is there any problems for this, or any other
better solution for it? Thanks.
If your problem is to eval a region of a python buffer and output the
result to another buffer, then python-mode.el (the one that comes with
Python, not the python.el bundled with recent emacs versions) already
know how to do so.

Else please explain what you're trying to do ?
 
P

Peter Wang

Bruno Desthuilliers said:
Peter Wang a écrit :
If your problem is to eval a region of a python buffer and output the
result to another buffer, then python-mode.el (the one that comes with
Python, not the python.el bundled with recent emacs versions) already
know how to do so.
Yes, I want eval a region, but likely not a python buffer, for example,
in a *w3m* buffer.
 
N

Nathan Seese

#! /bin/sh
python -c "import sys;exec(sys.stdin)"

I know this isn't your question, but I think you could write that more
cleanly with:

#!/usr/bin/python
import sys
exec(sys.stdin)
 
P

Peter Wang

Nathan Seese said:
I know this isn't your question, but I think you could write that more
cleanly with:

#!/usr/bin/python
import sys
exec(sys.stdin)
thanks.
What's the difference between this and mine?

I think what i need actually is a python function like
`file_get_contents' in php:)
 
S

Steven D'Aprano

thanks.
What's the difference between this and mine?

Yours launches an new shell, which then calls python, which then executes
whatever it finds in stdin as Python code.

The second one just launches Python directly.


I think what i need actually is a python function like
`file_get_contents' in php:)


I think that would be:

contents = open(filename).read()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top