summaryrefslogtreecommitdiff
path: root/lisp/org/ob-python.el
diff options
context:
space:
mode:
authorCarsten Dominik <carsten.dominik@gmail.com>2010-12-11 17:42:53 +0100
committerCarsten Dominik <carsten.dominik@gmail.com>2010-12-11 17:42:53 +0100
commitacedf35ce08b9df4a0dcbcd1413e7d85f1182034 (patch)
tree240e26f10d2feb66e8c0cd0634082fcb7bd577e5 /lisp/org/ob-python.el
parent39321b94bfa4e50401e30caedfd09a06629f5bd2 (diff)
downloademacs-acedf35ce08b9df4a0dcbcd1413e7d85f1182034.tar.gz
Update to Org mode 7.4
Diffstat (limited to 'lisp/org/ob-python.el')
-rw-r--r--lisp/org/ob-python.el65
1 files changed, 29 insertions, 36 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el
index 22cb5337d7a..27b69bff5ab 100644
--- a/lisp/org/ob-python.el
+++ b/lisp/org/ob-python.el
@@ -1,12 +1,11 @@
;;; ob-python.el --- org-babel functions for python evaluation
-;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+;; Copyright (C) 2009, 2010 Free Software Foundation
-;; Author: Eric Schulte
-;; Dan Davison
+;; Author: Eric Schulte, Dan Davison
;; Keywords: literate programming, reproducible research
;; Homepage: http://orgmode.org
-;; Version: 7.3
+;; Version: 7.4
;; This file is part of GNU Emacs.
@@ -57,11 +56,15 @@ This function is called by `org-babel-execute-src-block'."
(cdr (assoc :session params))))
(result-params (cdr (assoc :result-params params)))
(result-type (cdr (assoc :result-type params)))
+ (return-val (when (and (eq result-type 'value) (not session))
+ (cdr (assoc :return params))))
+ (preamble (cdr (assoc :preamble params)))
(full-body
(org-babel-expand-body:generic
- body params (org-babel-variable-assignments:python params)))
+ (concat body (if return-val (format "return %s" return-val) ""))
+ params (org-babel-variable-assignments:python params)))
(result (org-babel-python-evaluate
- session full-body result-type result-params)))
+ session full-body result-type result-params preamble)))
(or (cdr (assoc :file params))
(org-babel-reassemble-table
result
@@ -118,20 +121,7 @@ specifying a variable of the same value."
"Convert RESULTS into an appropriate elisp value.
If the results look like a list or tuple, then convert them into an
Emacs-lisp table, otherwise return the results as a string."
- ((lambda (res)
- (if (listp res)
- (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res)
- res))
- (org-babel-read
- (if (and (stringp results) (string-match "^[([].+[])]$" results))
- (org-babel-read
- (concat "'"
- (replace-regexp-in-string
- "\\[" "(" (replace-regexp-in-string
- "\\]" ")" (replace-regexp-in-string
- ", " " " (replace-regexp-in-string
- "'" "\"" results t))))))
- results))))
+ (org-babel-script-escape results))
(defvar org-babel-python-buffers '((:default . nil)))
@@ -192,35 +182,38 @@ def main():
open('%s', 'w').write( pprint.pformat(main()) )")
(defun org-babel-python-evaluate
- (session body &optional result-type result-params)
+ (session body &optional result-type result-params preamble)
"Evaluate BODY as python code."
(if session
(org-babel-python-evaluate-session
session body result-type result-params)
(org-babel-python-evaluate-external-process
- body result-type result-params)))
+ body result-type result-params preamble)))
(defun org-babel-python-evaluate-external-process
- (body &optional result-type result-params)
+ (body &optional result-type result-params preamble)
"Evaluate BODY in external python process.
If RESULT-TYPE equals 'output then return standard output as a
string. If RESULT-TYPE equals 'value then return the value of the
last statement in BODY, as elisp."
(case result-type
- (output (org-babel-eval org-babel-python-command body))
+ (output (org-babel-eval org-babel-python-command
+ (concat (if preamble (concat preamble "\n") "") body)))
(value (let ((tmp-file (org-babel-temp-file "python-")))
(org-babel-eval org-babel-python-command
- (format
- (if (member "pp" result-params)
- org-babel-python-pp-wrapper-method
- org-babel-python-wrapper-method)
- (mapconcat
- (lambda (line) (format "\t%s" line))
- (split-string
- (org-remove-indentation
- (org-babel-trim body))
- "[\r\n]") "\n")
- (org-babel-process-file-name tmp-file 'noquote)))
+ (concat
+ (if preamble (concat preamble "\n") "")
+ (format
+ (if (member "pp" result-params)
+ org-babel-python-pp-wrapper-method
+ org-babel-python-wrapper-method)
+ (mapconcat
+ (lambda (line) (format "\t%s" line))
+ (split-string
+ (org-remove-indentation
+ (org-babel-trim body))
+ "[\r\n]") "\n")
+ (org-babel-process-file-name tmp-file 'noquote))))
((lambda (raw)
(if (or (member "code" result-params)
(member "pp" result-params))
@@ -240,7 +233,7 @@ last statement in BODY, as elisp."
(lambda (statement) (insert statement) (comint-send-input))
(if pp
(list
- "import pp"
+ "import pprint"
(format "open('%s', 'w').write(pprint.pformat(_))"
(org-babel-process-file-name tmp-file 'noquote)))
(list (format "open('%s', 'w').write(str(_))"