emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* python :session issues
@ 2013-02-06  2:19 John Kitchin
  2013-02-06 15:10 ` Myles English
  0 siblings, 1 reply; 6+ messages in thread
From: John Kitchin @ 2013-02-06  2:19 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2802 bytes --]

I have been trying to use the :session feature of babel for python code
blocks. I have noticed a few things that might be areas for
improvement. Some issues are related to what I think might be a need for
sanitizing the code blocks and the output.

I am using (insert (org-version)) 7.9.3a
I am using python-mode 5.2 (more modern versions do not work with
:session for me)

The first small detail is illustrated here:

#+BEGIN_SRC python :session
a = 5

b = 6

print a + b
#+END_SRC

#+RESULTS:
:
: >>> >>> >>> 11

In the output, there are three sets of >>>, due to the two empty
lines. Compare this to

#+BEGIN_SRC python :session
a = 5
b = 6
print a + b
#+END_SRC

#+RESULTS:
:
: >>> 11

I think it would be nice to sanitize the python code block sent to the
interpreter to eliminate the empty lines, and avoid the multiple >>> in
output. It would also be nice to have an option to not print those at
all.

The second issue is related. The following code does not work for me in
:session, but it works fine as a standalone block. The issue is related
to the presence of an empty line in the function definition. I like
whitespace like this in functions sometimes for readability.

#+BEGIN_SRC python :session
def func(x):
    'doc string'

    return x**2

print func(2)
#+END_SRC

#+RESULTS:
:
: ... >>>   File "<stdin>", line 1
:     return x**2
:     ^
: IndentationError: unexpected indent
: >>> None

Removing the blank line works fine, but still has some non-pretty
output.

#+BEGIN_SRC python :session
def func(x):
    'doc string'
    return x**2

print func(2)
#+END_SRC

#+RESULTS:
:
: ... ... >>> 4

One of the ellipsis is due to the doc string.
#+BEGIN_SRC python :session
def func(x):
    return x**2

print func(2)
#+END_SRC

#+RESULTS:
:
: ... >>> 4

Interestingly, this did not work, because there is no empty line after
the function definition. So, sanitizing could be tricky.

#+BEGIN_SRC python :session
def func(x):
    'doc string'
    return x**2
print func(2)
#+END_SRC

#+RESULTS:
:
: ... ...   File "<stdin>", line 4
:     print func(2)
:         ^
: SyntaxError: invalid syntax

I think if there was a way to strip all the empty lines from the input,
it would go a long way to making the output look like what I think most
people would expect and want.

Anyway, those are the examples I wanted to highlight in this
post.


1. Do you see similar behavior, or is this a feature of my setup?
2. Is there any interest in sanitizing the input and output of a
python :session to get prettier output? if so, Any hints on where to start
with that?

Thanks,

John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu

[-- Attachment #2: Type: text/html, Size: 3473 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: python :session issues
  2013-02-06  2:19 python :session issues John Kitchin
@ 2013-02-06 15:10 ` Myles English
  2013-02-06 15:17   ` John Kitchin
  0 siblings, 1 reply; 6+ messages in thread
From: Myles English @ 2013-02-06 15:10 UTC (permalink / raw)
  To: John Kitchin; +Cc: emacs-orgmode


Hi John,

John Kitchin writes:

> I have been trying to use the :session feature of babel for python code
> blocks. I have noticed a few things that might be areas for
> improvement. Some issues are related to what I think might be a need for
> sanitizing the code blocks and the output.
>
> I am using (insert (org-version)) 7.9.3a
> I am using python-mode 5.2 (more modern versions do not work with
> :session for me)
>
> The first small detail is illustrated here:
>
> #+BEGIN_SRC python :session
> a = 5
>
> b = 6
>
> print a + b
> #+END_SRC
>
> #+RESULTS:
> :
> : >>> >>> >>> 11
>
> In the output, there are three sets of >>>, due to the two empty
> lines. Compare this to
>
> #+BEGIN_SRC python :session
> a = 5
> b = 6
> print a + b
> #+END_SRC
>
> #+RESULTS:
> :
> : >>> 11
>
> I think it would be nice to sanitize the python code block sent to the
> interpreter to eliminate the empty lines, and avoid the multiple >>> in
> output. It would also be nice to have an option to not print those at
> all.
>
> The second issue is related. The following code does not work for me in
> :session, but it works fine as a standalone block. The issue is related
> to the presence of an empty line in the function definition. I like
> whitespace like this in functions sometimes for readability.
>
> #+BEGIN_SRC python :session
> def func(x):
>     'doc string'
>
>     return x**2
>
> print func(2)
> #+END_SRC
>
> #+RESULTS:
> :
> : ... >>>   File "<stdin>", line 1
> :     return x**2
> :     ^
> : IndentationError: unexpected indent
> : >>> None
>
> Removing the blank line works fine, but still has some non-pretty
> output.
>
> #+BEGIN_SRC python :session
> def func(x):
>     'doc string'
>     return x**2
>
> print func(2)
> #+END_SRC
>
> #+RESULTS:
> :
> : ... ... >>> 4
>
> One of the ellipsis is due to the doc string.
> #+BEGIN_SRC python :session
> def func(x):
>     return x**2
>
> print func(2)
> #+END_SRC
>
> #+RESULTS:
> :
> : ... >>> 4
>
> Interestingly, this did not work, because there is no empty line after
> the function definition. So, sanitizing could be tricky.
>
> #+BEGIN_SRC python :session
> def func(x):
>     'doc string'
>     return x**2
> print func(2)
> #+END_SRC
>
> #+RESULTS:
> :
> : ... ...   File "<stdin>", line 4
> :     print func(2)
> :         ^
> : SyntaxError: invalid syntax
>
> I think if there was a way to strip all the empty lines from the input,
> it would go a long way to making the output look like what I think most
> people would expect and want.
>
> Anyway, those are the examples I wanted to highlight in this
> post.
>
>
> 1. Do you see similar behavior, or is this a feature of my setup?

I haven't tried your examples (no time) but you would be able to tell if
it was a feature of your setup by making a minimum working example and
starting emacs with 'emacs -Q'.  If you see the thread in the footnote
it contains such an example.

> 2. Is there any interest in sanitizing the input and output of a
> python :session to get prettier output? if so, Any hints on where to
> start with that?

I had some problems recently with :session, there was a short discussion[1],
and last I heard the maintainer of python-mode.el is looking into it.

Myles

Footnotes: 
[1]  https://lists.gnu.org/archive/html/emacs-orgmode/2013-01/msg00888.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: python :session issues
  2013-02-06 15:10 ` Myles English
@ 2013-02-06 15:17   ` John Kitchin
  2013-02-07 16:54     ` Andreas Röhler
  0 siblings, 1 reply; 6+ messages in thread
From: John Kitchin @ 2013-02-06 15:17 UTC (permalink / raw)
  To: Myles English; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 4193 bytes --]

Thanks for the -Q reminder. I get the same output if I run with emacs -Q
(although I do have to add :results output to each block. I had that set as
a default in my init files).

I had the problem described in the footnote you listed, and that is why I
am using python-mode 5.2, which doesn't have that issue.

John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu



On Wed, Feb 6, 2013 at 10:10 AM, Myles English <mylesenglish@gmail.com>wrote:

>
> Hi John,
>
> John Kitchin writes:
>
> > I have been trying to use the :session feature of babel for python code
> > blocks. I have noticed a few things that might be areas for
> > improvement. Some issues are related to what I think might be a need for
> > sanitizing the code blocks and the output.
> >
> > I am using (insert (org-version)) 7.9.3a
> > I am using python-mode 5.2 (more modern versions do not work with
> > :session for me)
> >
> > The first small detail is illustrated here:
> >
> > #+BEGIN_SRC python :session
> > a = 5
> >
> > b = 6
> >
> > print a + b
> > #+END_SRC
> >
> > #+RESULTS:
> > :
> > : >>> >>> >>> 11
> >
> > In the output, there are three sets of >>>, due to the two empty
> > lines. Compare this to
> >
> > #+BEGIN_SRC python :session
> > a = 5
> > b = 6
> > print a + b
> > #+END_SRC
> >
> > #+RESULTS:
> > :
> > : >>> 11
> >
> > I think it would be nice to sanitize the python code block sent to the
> > interpreter to eliminate the empty lines, and avoid the multiple >>> in
> > output. It would also be nice to have an option to not print those at
> > all.
> >
> > The second issue is related. The following code does not work for me in
> > :session, but it works fine as a standalone block. The issue is related
> > to the presence of an empty line in the function definition. I like
> > whitespace like this in functions sometimes for readability.
> >
> > #+BEGIN_SRC python :session
> > def func(x):
> >     'doc string'
> >
> >     return x**2
> >
> > print func(2)
> > #+END_SRC
> >
> > #+RESULTS:
> > :
> > : ... >>>   File "<stdin>", line 1
> > :     return x**2
> > :     ^
> > : IndentationError: unexpected indent
> > : >>> None
> >
> > Removing the blank line works fine, but still has some non-pretty
> > output.
> >
> > #+BEGIN_SRC python :session
> > def func(x):
> >     'doc string'
> >     return x**2
> >
> > print func(2)
> > #+END_SRC
> >
> > #+RESULTS:
> > :
> > : ... ... >>> 4
> >
> > One of the ellipsis is due to the doc string.
> > #+BEGIN_SRC python :session
> > def func(x):
> >     return x**2
> >
> > print func(2)
> > #+END_SRC
> >
> > #+RESULTS:
> > :
> > : ... >>> 4
> >
> > Interestingly, this did not work, because there is no empty line after
> > the function definition. So, sanitizing could be tricky.
> >
> > #+BEGIN_SRC python :session
> > def func(x):
> >     'doc string'
> >     return x**2
> > print func(2)
> > #+END_SRC
> >
> > #+RESULTS:
> > :
> > : ... ...   File "<stdin>", line 4
> > :     print func(2)
> > :         ^
> > : SyntaxError: invalid syntax
> >
> > I think if there was a way to strip all the empty lines from the input,
> > it would go a long way to making the output look like what I think most
> > people would expect and want.
> >
> > Anyway, those are the examples I wanted to highlight in this
> > post.
> >
> >
> > 1. Do you see similar behavior, or is this a feature of my setup?
>
> I haven't tried your examples (no time) but you would be able to tell if
> it was a feature of your setup by making a minimum working example and
> starting emacs with 'emacs -Q'.  If you see the thread in the footnote
> it contains such an example.
>
> > 2. Is there any interest in sanitizing the input and output of a
> > python :session to get prettier output? if so, Any hints on where to
> > start with that?
>
> I had some problems recently with :session, there was a short
> discussion[1],
> and last I heard the maintainer of python-mode.el is looking into it.
>
> Myles
>
> Footnotes:
> [1]
> https://lists.gnu.org/archive/html/emacs-orgmode/2013-01/msg00888.html
>
>
>

[-- Attachment #2: Type: text/html, Size: 5686 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: python :session issues
  2013-02-06 15:17   ` John Kitchin
@ 2013-02-07 16:54     ` Andreas Röhler
  2013-02-07 21:53       ` Myles English
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Röhler @ 2013-02-07 16:54 UTC (permalink / raw)
  To: emacs-orgmode

Am 06.02.2013 16:17, schrieb John Kitchin:
> Thanks for the -Q reminder. I get the same output if I run with emacs -Q
> (although I do have to add :results output to each block. I had that set as
> a default in my init files).
>
> I had the problem described in the footnote you listed, and that is why I
> am using python-mode 5.2, which doesn't have that issue.
>
> John


Should be fixed in current trunk.

bzr branch lp:python-mode

Andreas

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: python :session issues
  2013-02-07 16:54     ` Andreas Röhler
@ 2013-02-07 21:53       ` Myles English
  2013-02-08  9:23         ` Andreas Röhler
  0 siblings, 1 reply; 6+ messages in thread
From: Myles English @ 2013-02-07 21:53 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: emacs-orgmode


Hi Andreas,

Andreas Röhler writes:

> Am 06.02.2013 16:17, schrieb John Kitchin:
>> Thanks for the -Q reminder. I get the same output if I run with emacs -Q
>> (although I do have to add :results output to each block. I had that set as
>> a default in my init files).
>>
>> I had the problem described in the footnote you listed, and that is why I
>> am using python-mode 5.2, which doesn't have that issue.
>>
>> John
>
>
> Should be fixed in current trunk.
>
> bzr branch lp:python-mode

Thanks for looking at this Andreas, I have tried the bzr trunk and still
have problems.

John, I hope I am not hijacking your thread but I am not sure what
problem is meant to be have been fixed above so I am just jumping in and
assuming that we would both like to fix the same thing.

Using org-mode from git, commit b810431 from Jan 22.

If I start emacs like this:

$ emacs -Q testPy.org -l init.el

then executing the src blocks in order, *restarting Emacs between
attempts*, shows the problems.

-----/ init.el /------------------------------------------
;; use the git trunk for org mode
(add-to-list 'load-path "~/.emacs.d/plugins/org-mode/lisp")
(add-to-list 'load-path "~/.emacs.d/plugins/org-mode/contrib/lisp" t)
(require 'org)

;; use the bzr trunk for python-mode
(setq py-install-directory "/home/myles/.emacs.d/plugins/python-mode")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)

(org-babel-do-load-languages
 'org-babel-load-languages
 '((python . t)))
---------------------------------------/ end init.el /-------

-----/ testPy.org /------------------------------------------
* First attempt 						:doesnotwork:

#+begin_src python :results output :session a
import sys
#+end_src

Message:

inferior-python-mode: Wrong type argument: keymapp, py-down-exception

* Second attempt: set org-babel-python-mode first		:doesnotwork:

#+begin_src elisp
(setq org-babel-python-mode 'python-mode)
(setq org-babel-python-command "python2"
      py-python-command "python2")
#+end_src

I would expect this to open a Python 2 session, however it opens a
Python 3 session:

#+begin_src python :results output :session a
import sys
print(sys.path)
#+end_src

* Third attempt: try python3				      :works:

#+begin_src elisp
(setq org-babel-python-mode 'python-mode)
(setq org-babel-python-command "python3"
      py-python-command "python3")
#+end_src

#+begin_src python :results output :session a
import sys
print(sys.path)
#+end_src

Works ok.
---------------------------------------/ end testPy.org /-------

Perhaps I am not using it correctly?

Thanks,

Myles

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: python :session issues
  2013-02-07 21:53       ` Myles English
@ 2013-02-08  9:23         ` Andreas Röhler
  0 siblings, 0 replies; 6+ messages in thread
From: Andreas Röhler @ 2013-02-08  9:23 UTC (permalink / raw)
  To: Myles English; +Cc: emacs-orgmode

Am 07.02.2013 22:53, schrieb Myles English:
>
> Hi Andreas,
>
> Andreas Röhler writes:
>
>> Am 06.02.2013 16:17, schrieb John Kitchin:
>>> Thanks for the -Q reminder. I get the same output if I run with emacs -Q
>>> (although I do have to add :results output to each block. I had that set as
>>> a default in my init files).
>>>
>>> I had the problem described in the footnote you listed, and that is why I
>>> am using python-mode 5.2, which doesn't have that issue.
>>>
>>> John
>>
>>
>> Should be fixed in current trunk.
>>
>> bzr branch lp:python-mode
>
> Thanks for looking at this Andreas, I have tried the bzr trunk and still
> have problems.
>
> John, I hope I am not hijacking your thread but I am not sure what
> problem is meant to be have been fixed above so I am just jumping in and
> assuming that we would both like to fix the same thing.
>
> Using org-mode from git, commit b810431 from Jan 22.
>
> If I start emacs like this:
>
> $ emacs -Q testPy.org -l init.el
>
> then executing the src blocks in order, *restarting Emacs between
> attempts*, shows the problems.
>
> -----/ init.el /------------------------------------------
> ;; use the git trunk for org mode
> (add-to-list 'load-path "~/.emacs.d/plugins/org-mode/lisp")
> (add-to-list 'load-path "~/.emacs.d/plugins/org-mode/contrib/lisp" t)
> (require 'org)
>
> ;; use the bzr trunk for python-mode
> (setq py-install-directory "/home/myles/.emacs.d/plugins/python-mode")
> (add-to-list 'load-path py-install-directory)
> (require 'python-mode)
>
> (org-babel-do-load-languages
>   'org-babel-load-languages
>   '((python . t)))
> ---------------------------------------/ end init.el /-------
>
> -----/ testPy.org /------------------------------------------
> * First attempt 						:doesnotwork:
>
> #+begin_src python :results output :session a
> import sys
> #+end_src
>
> Message:
>
> inferior-python-mode: Wrong type argument: keymapp, py-down-exception
>
> * Second attempt: set org-babel-python-mode first		:doesnotwork:
>
> #+begin_src elisp
> (setq org-babel-python-mode 'python-mode)
> (setq org-babel-python-command "python2"
>        py-python-command "python2")
> #+end_src
>
> I would expect this to open a Python 2 session, however it opens a
> Python 3 session:
>
> #+begin_src python :results output :session a
> import sys
> print(sys.path)
> #+end_src
>
> * Third attempt: try python3				      :works:
>
> #+begin_src elisp
> (setq org-babel-python-mode 'python-mode)
> (setq org-babel-python-command "python3"
>        py-python-command "python3")
> #+end_src
>
> #+begin_src python :results output :session a
> import sys
> print(sys.path)
> #+end_src
>
> Works ok.
> ---------------------------------------/ end testPy.org /-------
>
> Perhaps I am not using it correctly?
>
> Thanks,
>
> Myles
>

https://bugs.launchpad.net/python-mode/+bug/1119201

Will look into, when 6.1.1 is released.

Cheers,

Andreas

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-02-08  9:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-06  2:19 python :session issues John Kitchin
2013-02-06 15:10 ` Myles English
2013-02-06 15:17   ` John Kitchin
2013-02-07 16:54     ` Andreas Röhler
2013-02-07 21:53       ` Myles English
2013-02-08  9:23         ` Andreas Röhler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).