emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: emacs-orgmode@gnu.org
Subject: python :session issues
Date: Tue, 5 Feb 2013 21:19:02 -0500	[thread overview]
Message-ID: <CAJ51ETr+WXyDVTF7+eVTLpy78S51U-dH35Oh3DYNm6hkJnzPxg@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2013-02-06  2:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-06  2:19 John Kitchin [this message]
2013-02-06 15:10 ` python :session issues 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJ51ETr+WXyDVTF7+eVTLpy78S51U-dH35Oh3DYNm6hkJnzPxg@mail.gmail.com \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).