I'm trying to remap a key to yank/put selected text from a script into a Python interpreter using ConqueTerm. Everything is fine, except this...
def main():
print "Testing 123"
main()
turns into this...
>>> def main(): print "Testing 123"main()
in the interpreter. How can I keep the multi-line formatting?
Note: I've already tried plugins like vim-conque-repl and vim-ipython, but I haven't been able to get either of them to work correctly.
Related
When you run "python" in your terminal, you get a running instance of python where you can run python code.
I want to do this, but after my script has run a few functions first.
But what I want is this type of interface at the end of my script.
I.e. I want to run my_script.py, in "main" call a few functions, but then after those functions, keep the "workspace" open with the >>> interface so that the user can run more python code.
if __name__ == "__main__":
excel_file = sys.argv[1]
my_list = load_file(excel_file)
#... do a bunch of other things
# open python interface now >>>
while True:
# accept user python commands like in the terminal example above
Is there any way to do this?
You could run your script in interactive mode so that when it would normally exit, you instead drop into an interactive Python interpreter:
python -i my_script.py
You could also enter interactive mode from the script file without the command line flag using the Python code module like in this example:
import code
if __name__ == "__main__":
# Do something.
code.interact(local = locals())
Alternatively, you could use something like the exec keyword in a loop to execute arbitrary commands and kind of "fake it" with something like this:
if __name__ == "__main__":
# Do something.
try:
while True:
exec(input(">>> "))
except KeyboardInterrupt:
sys.exit()
Though this approach is a lot less clean than using interactive mode, especially for executing multi-line code.
If I understand you correctly, you want to load files into variables and play around with those variables.
I think the better option, instead of your proposed makeshift workspace, is to use a Jupyter notebook.
This video gives a good introduction on how the workflow looks like to read an Excel file and play around with it.
I'm taking a Udemy course on Python (my first language) and the environment of choice is Jupyter. When I try to write that code in Sublime, I can't get the same output (there are no errors).
def splicer(mystring):
if len(mystring)%2 == 0:
return "Even"
else:
return "Odd"
names = ["Andy", "Eve", "Sally"]
list(map(splicer,names))
You need to print the result!
print(list(map(splicer,names)))
In Jupyter, it automatically prints the representation of a statement, where as when you're writing applications, you need to print if you want the result to be shown on the screen.
jupyter acts as a python interpreter, so if you enter an object it automatically prints the result underneath. Sublime is a text editor, so it is only executing the code you are giving it. It is running list(map(splicer,names)) but it is not displaying the object because you are not telling it to.
So the interpreter (jupyter) is executing your python code in real time and interpreting (printing to screen). The text editor is only executing your python code. Therefore, you need to add a print statement to your object to have the editor print the object to screen:
print(list(map(splicer,names)))
I am trying to read a string in a python script within vim, but something goes wrong. I do it in a function getting an argument called key, using s = vim.eval("a:key"), but this is a simpler example demonstrating the problem:
:py import vim
:py s = vim.eval("'foo'")
:py print s # this prints 'foo' (without the quotes)
:py if s is 'foo': print 'yes' # this doesn't print 'yes'
The problem is that s is 'foo' does not evaluate to True. My guess is that it has something to do with vim.eval, but it's only a guess.
Is there any easy way to use an applescript like:
set theText to text returned of (display dialog "Please insert Text here:" default answer "" with title "exchange to python" with icon 1)
And use the "theText" variable in python?
You can also run a python script with command line input from AppleScript:
--make sure to escape properly if needed
set pythonvar to "whatever"
set outputvar to (do shell script "python '/path/to/script' '" & pythonvar & "'")
Ned's example has python calling AppleScript, then returning control to python, this is the other way around. Then in Python access list of parameters:
import sys
var_from_as = sys.argv[1] # for 1rst parameter cause argv[0] is file name
print 'this gets returned to AppleScript' # this gets set to outputvar
There are a number of ways to do it. Probably the simplest way, since it does not rely on any third-party Python modules, is to run the script in a child process using the OS X osascript command line utility. By default, osascript returns any output from the AppleScript execution to stdout which can be then be read in Python. You can try it out in the Python interactive interpreter.
With Python 3.4.1:
>>> import subprocess
>>> theText = subprocess.check_output(['osascript', '-e', \
r'''set theText to text returned of (display dialog "Please insert Text here:" default answer "" with title "exchange to python" with icon 1)'''])
>>> theText
b'Hell\xc3\xb6 W\xc3\xb2rld!\n'
>>> print(theText.decode('UTF-8'))
Hellö Wòrld!
With Python 2.7.7:
>>> theText
'Hell\xc3\xb6 W\xc3\xb2rld!\n'
>>> print(theText.decode('UTF-8'))
Hellö Wòrld!
For a real world application, you'll probably want to do some error checking and/or exception catching.
I am running Eclipse with PyDev and Python 3.2 on Windows Vista, and was working through a tutorial on Python and ctypes.
However, I found that when I call msvcrt.printf() to print a string, this is not displayed in the console output for Eclipse until all other print statements have displayed.
Here is the exact code I use:
from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello Worlds!\n"
printf = msvcrt.printf
print(printf("Testing: %s".encode('ascii'),message_string.encode('ascii')))
print("foo")
print("why!?")
and here is the output:
23
foo
why!?
Testing: Hello Worlds!
The only explanations I have seen elsewhere (for C in general) mention how printf is buffered and needs a newline before displaying, but there is a newline in the string, and I also added one directly to the printf statement ('printf("Testing: %s\n",...') and it made no difference.
I want to work in Eclipse, I don't want to have to keep opening a command prompt every time i want to test scripts, so is there any way I can fix this ordering in the console output? And why does this happen?
If the C standard library thinks stdout is connected to a file or a pipe rather than a console, it will block-buffer its output. You can work around this by issuing a fflush after printf:
msvcrt.fflush(msvcrt.stdout)
You may also be able to force stdout into non-buffered mode:
msvcrt.setvbuf(msvcrt.stdout, None, _IONBF, 0)
Although this does not answer your question but printf is returning 23 which is the number of printed characters, you could replace it with sprintf and that will return the string and will be displayed in the console in the right expected order.
However, I don't see a reason for using mscvcrt's printf when you can do the same with python.