emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* ob-python session output
@ 2013-11-20  7:41 Kyle Meyer
  0 siblings, 0 replies; only message in thread
From: Kyle Meyer @ 2013-11-20  7:41 UTC (permalink / raw)
  To: emacs-orgmode


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
   '((python . t)))

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):

: >>> >>> >>> ... ... 0
: 1

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

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

: ... 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')

: one leading space

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

: 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
-              #'org-babel-trim
+             #'(lambda (string) (org-babel-chomp
+                                 (org-babel-python-leading-chomp string)))
                    (session org-babel-python-eoe-indicator t body)
@@ -339,6 +340,25 @@ last statement in BODY, as elisp."
       (match-string 1 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
+  (let ((regexp " *>+\\|\\( \\)*\\.+\\| \\w" ))
+    (while (and (/= (length string) 0)
+                (eq (string-match regexp string) 0))
+      (setq string (substring string 1)))
+    string))
 (provide 'ob-python)


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

#+BEGIN_SRC elisp

: 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)

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


Kyle Meyer

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-11-20  7:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-20  7:41 ob-python session output Kyle Meyer

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


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).