emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] ob-python session output


From: Kyle Meyer
Subject: [O] ob-python session output
Date: Wed, 20 Nov 2013 02:41:04 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]