Re: Patch for py[3] command to avoid print calls

78 views
Skip to first unread message

Bram Moolenaar

unread,
Aug 17, 2012, 4:24:14 PM8/17/12
to Maxim, vim...@googlegroups.com

Maxim / philippovmi wrote:

> Hi, vim_dev!
>
> I'd like to offer a small patch for python interface, which allows to
> avoid inserting print call for every expression in order to see
> result.
>
> Now:
>
> :py 2 + 2
> :py dir.__doc__
>
> After applying patch:
>
> :py 2 + 2
> 4
> :py dir.__doc__
> dir([object]) -> list of strings...
>
> So py/py3 commands are more REPL-like. See pyrepl.diff in attachment
> for details.

I do not see your name. Patches without a full name are not accepted,
for copyright reasons.

With this patch, what does :py print "hello" result in? Is there an
extra line with whatever "print" returns?

--
hundred-and-one symptoms of being an internet addict:
254. You wake up daily with your keyboard printed on your forehead.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Vlad Irnov

unread,
Aug 17, 2012, 4:51:46 PM8/17/12
to vim...@googlegroups.com
On 8/16/12, Maxim <phili...@gmail.com> wrote:
> Hi, vim_dev!
>
> I'd like to offer a small patch for python interface, which allows to avoid
> inserting print call for every expression in order to see result.
>
> Now:
>
> :py 2 + 2
> :py dir.__doc__
>
> After applying patch:
>
> :py 2 + 2
> 4
> :py dir.__doc__
> dir([object]) -> list of strings...
>
> So py/py3 commands are more REPL-like. See pyrepl.diff in attachment for
> details.

There are plugins that implement REPL-like behavior:
http://www.vim.org/scripts/script.php?script_id=3484
http://www.vim.org/scripts/script.php?script_id=3327
http://www.vim.org/scripts/script.php?script_id=3231
and probably other.

It makes no sense for :python commands to behave differently from
other commands. That is, we currently need to type
:echo 2+2
to see the result, why :python should be different?

Regards,
Vlad

Maxim Philippov

unread,
Aug 17, 2012, 8:26:17 PM8/17/12
to vim...@googlegroups.com, Maxim
суббота, 18 августа 2012 г., 0:24:14 UTC+4 пользователь Bram Moolenaar написал:
>
> I do not see your name. Patches without a full name are not accepted,
>
> for copyright reasons.
>

Fixed.

суббота, 18 августа 2012 г., 0:24:14 UTC+4 пользователь Bram Moolenaar написал:
>
> With this patch, what does :py print "hello" result in? Is there an
>
> extra line with whatever "print" returns?
>

Actually after my first patch "print" doesn't print at all. Sorry for inconvenience, I should have tested this first.

Please look at the second version of patch (pyrepl1.diff) which fixes that and also shows some diagnostics in case of python exception. There will be no extra new line after "print" call.
pyrepl1.diff

Maxim Philippov

unread,
Aug 17, 2012, 9:05:07 PM8/17/12
to vim...@googlegroups.com
суббота, 18 августа 2012 г., 0:51:46 UTC+4 пользователь Vlad Irnov написал:
>
> There are plugins that implement REPL-like behavior:
>
> http://www.vim.org/scripts/script.php?script_id=3484
>
> http://www.vim.org/scripts/script.php?script_id=3327
>
> http://www.vim.org/scripts/script.php?script_id=3231
>
> and probably other.
>
>
> It makes no sense for :python commands to behave differently from
>
> other commands. That is, we currently need to type
>
> :echo 2+2
>
> to see the result, why :python should be different?
>

Though this scripts are useful, I think typing ":py 2+2" is more fluent (and maybe more lightweight) than ":PyInteractiveEval 2+2" or running full REPL session.

We need to type ":echo 2+2" because of the vim's script syntax, ":4" by itself is a command, there is no need for this in python.

Often I forget to call "print" because as a python user I expect expression result to be printed automatically. It is difficult to switch from python REPL to vim's current implementation, so why not have the best from both worlds? =)

Roland Puntaier

unread,
Aug 19, 2012, 3:28:10 PM8/19/12
to vim...@googlegroups.com
On 8/18/2012 3:05 AM, Maxim Philippov wrote:
О©╫О©╫О©╫О©╫О©╫О©╫О©╫, 18 О©╫О©╫О©╫О©╫О©╫О©╫О©╫ 2012О©╫О©╫., 0:51:46 UTC+4 О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ Vlad Irnov О©╫О©╫О©╫О©╫О©╫О©╫О©╫:

A macro like
py3<<EOL
def python_with_print(*args):
О©╫ global py_res
О©╫ py_res=None
О©╫ try:
О©╫О©╫О©╫О©╫О©╫ eval(compile('py_res='+' '.join(args),'<string>','exec'),globals())
О©╫ except:
О©╫О©╫О©╫О©╫О©╫ eval(compile(' '.join(args),'<string>','exec'),globals())
О©╫ print(py_res)
EOL
command! -narg=* Py py3 python_with_print(<f-args>)
in $MYVIMRC
would allow you to write

:Py from math import *
:Py sin(30*pi/180)
an you would get results or None on the command line.

Regards,
Roland

Maxim Philippov

unread,
Aug 20, 2012, 5:02:28 AM8/20/12
to vim...@googlegroups.com
It's a nice hack, but, in my opinion, in C it looks more natural (or even simpler).

If it makes sense for Py macro, it should make sense for py command. As far as I see, it doesn't break anything (am I missing something?), it's just more user friendly. Why not make it work out of a box?

skeept

unread,
Aug 20, 2012, 10:41:43 AM8/20/12
to vim...@googlegroups.com
On Thursday, August 16, 2012 8:47:38 AM UTC-4, Maxim Philippov wrote:
> Hi, vim_dev!
>
> I'd like to offer a small patch for python interface, which allows to avoid inserting print call for every expression in order to see result.
>
> Now:
>
> :py 2 + 2
> :py dir.__doc__
>
> After applying patch:
>
> :py 2 + 2
> 4
> :py dir.__doc__
> dir([object]) -> list of strings...
>
> So py/py3 commands are more REPL-like. See pyrepl.diff in attachment for details.

I think this only makes sense if there is an option that allows to enable/disable it by default, say pythonverbose with the default value being disabled.

Maxim Philippov

unread,
Aug 20, 2012, 11:37:18 AM8/20/12
to vim...@googlegroups.com

But why not be a bit more verbose out of the box? I mean I could have just used one of the plugins listed by Vlad or script written by Roland, I admit these are good solutions, but I think default implementation is too much quiet.

Now You may run ":py f = open("/some/file/doesn/t/exist")" and see no error at all, or ":py vim.current.line" without expected output.

Among other things I like vim because of its succinctness, so I can type less and get more, that's why I think this patch would fit perfectly well without option disabled by default.

Reply all
Reply to author
Forward
0 new messages