emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: emacs-orgmode@gnu.org
Subject: ob-python session output
Date: Wed, 20 Nov 2013 02:41:04 -0500	[thread overview]
Message-ID: <87y54j34hb.fsf@kmlap.domain.org> (raw)

Hello,

I've been having some issues with the output of babel python session
blocks. They do not seem to be properly processing python shell
characters and leading spaces in the output.

For the examples that follow, the only user configuration is the loading
of python.

#+BEGIN_SRC elisp :results none
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((python . t)))
#+END_SRC

When the session is first started up, python shell character ('>>>' and
'...') make it into the output.

#+BEGIN_SRC python :session :results output
  for i in range(2):
      print(i)
#+END_SRC

#+RESULTS:
:
: >>> >>> >>> ... ... 0
: 1

When ran a second time, only '...' remains.

#+BEGIN_SRC python :session :results output
  for i in range(2):
      print(i)
#+END_SRC

#+RESULTS:
:
: ... 0
: 1

The other issue I'm seeing is that leading spaces in the output are
being discarded.

#+BEGIN_SRC python :session :results output
  print(' one leading space')
#+END_SRC

#+RESULTS:
: one leading space

#+BEGIN_SRC python :session :results output
  print('     many leading spaces')
#+END_SRC

#+RESULTS:
: many leading spaces

I've made the following changes for a quick fix. It's ugly and certainly
not a proper solution, but it seems to take care of all the above cases
except for the one leading space.

---
 lisp/ob-python.el | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 3c3f664..5cda5d6 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -307,7 +307,8 @@ last statement in BODY, as elisp."
           (case result-type
             (output
              (mapconcat
-              #'org-babel-trim
+             #'(lambda (string) (org-babel-chomp
+                                 (org-babel-python-leading-chomp string)))
               (butlast
                (org-babel-comint-with-output
                    (session org-babel-python-eoe-indicator t body)
@@ -339,6 +340,25 @@ last statement in BODY, as elisp."
       (match-string 1 string)
     string))

+(defun org-babel-python-leading-chomp (string)
+  "Strip the leading python output characters from STRING
+
+This is different from `org-babel-chomp' (where '>' and '.' are
+added to the regex) because it considers a bit more of the
+context. If only single characters are matched against, then
+meaningful spaces are often deleted from the output. However, if
+spaces are not trimmed at all, extra spaces creep into the
+output (particularly when the output source line is indented). To
+get around this, only leading spaces that are followed by a
+non-space character are deleted. This completely fails on cases
+where the intended output acutally has one space before non-space
+character."
+  (let ((regexp " *>+\\|\\( \\)*\\.+\\| \\w" ))
+    (while (and (/= (length string) 0)
+                (eq (string-match regexp string) 0))
+      (setq string (substring string 1)))
+    string))
+
 (provide 'ob-python)


--
1.8.4.2

I'm using the following versions of emacs and org mode.

#+BEGIN_SRC elisp
  (emacs-version)
#+END_SRC

#+RESULTS:
: GNU Emacs 24.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.8.2)
:  of 2013-08-06 on -mnt-storage-buildroots-staging-x86_64-eric

#+BEGIN_SRC elisp
  (org-version nil t)
#+END_SRC

#+RESULTS:
: Org-mode version 8.2.3c (release_8.2.3c-251-gbb97f5 @ /home/kyle/src/emacs/org-mode/lisp/)

Thanks

--
Kyle Meyer

                 reply	other threads:[~2013-11-20  7:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=87y54j34hb.fsf@kmlap.domain.org \
    --to=kyle@kyleam.com \
    --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).