diff options
| author | Carsten Dominik | 2010-12-11 17:42:53 +0100 |
|---|---|---|
| committer | Carsten Dominik | 2010-12-11 17:42:53 +0100 |
| commit | acedf35ce08b9df4a0dcbcd1413e7d85f1182034 (patch) | |
| tree | 240e26f10d2feb66e8c0cd0634082fcb7bd577e5 /lisp/org/ob-python.el | |
| parent | 39321b94bfa4e50401e30caedfd09a06629f5bd2 (diff) | |
| download | emacs-acedf35ce08b9df4a0dcbcd1413e7d85f1182034.tar.gz emacs-acedf35ce08b9df4a0dcbcd1413e7d85f1182034.zip | |
Update to Org mode 7.4
Diffstat (limited to 'lisp/org/ob-python.el')
| -rw-r--r-- | lisp/org/ob-python.el | 65 |
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 @@ | |||
| 1 | ;;; ob-python.el --- org-babel functions for python evaluation | 1 | ;;; ob-python.el --- org-babel functions for python evaluation |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2009, 2010 Free Software Foundation |
| 4 | 4 | ||
| 5 | ;; Author: Eric Schulte | 5 | ;; Author: Eric Schulte, Dan Davison |
| 6 | ;; Dan Davison | ||
| 7 | ;; Keywords: literate programming, reproducible research | 6 | ;; Keywords: literate programming, reproducible research |
| 8 | ;; Homepage: http://orgmode.org | 7 | ;; Homepage: http://orgmode.org |
| 9 | ;; Version: 7.3 | 8 | ;; Version: 7.4 |
| 10 | 9 | ||
| 11 | ;; This file is part of GNU Emacs. | 10 | ;; This file is part of GNU Emacs. |
| 12 | 11 | ||
| @@ -57,11 +56,15 @@ This function is called by `org-babel-execute-src-block'." | |||
| 57 | (cdr (assoc :session params)))) | 56 | (cdr (assoc :session params)))) |
| 58 | (result-params (cdr (assoc :result-params params))) | 57 | (result-params (cdr (assoc :result-params params))) |
| 59 | (result-type (cdr (assoc :result-type params))) | 58 | (result-type (cdr (assoc :result-type params))) |
| 59 | (return-val (when (and (eq result-type 'value) (not session)) | ||
| 60 | (cdr (assoc :return params)))) | ||
| 61 | (preamble (cdr (assoc :preamble params))) | ||
| 60 | (full-body | 62 | (full-body |
| 61 | (org-babel-expand-body:generic | 63 | (org-babel-expand-body:generic |
| 62 | body params (org-babel-variable-assignments:python params))) | 64 | (concat body (if return-val (format "return %s" return-val) "")) |
| 65 | params (org-babel-variable-assignments:python params))) | ||
| 63 | (result (org-babel-python-evaluate | 66 | (result (org-babel-python-evaluate |
| 64 | session full-body result-type result-params))) | 67 | session full-body result-type result-params preamble))) |
| 65 | (or (cdr (assoc :file params)) | 68 | (or (cdr (assoc :file params)) |
| 66 | (org-babel-reassemble-table | 69 | (org-babel-reassemble-table |
| 67 | result | 70 | result |
| @@ -118,20 +121,7 @@ specifying a variable of the same value." | |||
| 118 | "Convert RESULTS into an appropriate elisp value. | 121 | "Convert RESULTS into an appropriate elisp value. |
| 119 | If the results look like a list or tuple, then convert them into an | 122 | If the results look like a list or tuple, then convert them into an |
| 120 | Emacs-lisp table, otherwise return the results as a string." | 123 | Emacs-lisp table, otherwise return the results as a string." |
| 121 | ((lambda (res) | 124 | (org-babel-script-escape results)) |
| 122 | (if (listp res) | ||
| 123 | (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res) | ||
| 124 | res)) | ||
| 125 | (org-babel-read | ||
| 126 | (if (and (stringp results) (string-match "^[([].+[])]$" results)) | ||
| 127 | (org-babel-read | ||
| 128 | (concat "'" | ||
| 129 | (replace-regexp-in-string | ||
| 130 | "\\[" "(" (replace-regexp-in-string | ||
| 131 | "\\]" ")" (replace-regexp-in-string | ||
| 132 | ", " " " (replace-regexp-in-string | ||
| 133 | "'" "\"" results t)))))) | ||
| 134 | results)))) | ||
| 135 | 125 | ||
| 136 | (defvar org-babel-python-buffers '((:default . nil))) | 126 | (defvar org-babel-python-buffers '((:default . nil))) |
| 137 | 127 | ||
| @@ -192,35 +182,38 @@ def main(): | |||
| 192 | open('%s', 'w').write( pprint.pformat(main()) )") | 182 | open('%s', 'w').write( pprint.pformat(main()) )") |
| 193 | 183 | ||
| 194 | (defun org-babel-python-evaluate | 184 | (defun org-babel-python-evaluate |
| 195 | (session body &optional result-type result-params) | 185 | (session body &optional result-type result-params preamble) |
| 196 | "Evaluate BODY as python code." | 186 | "Evaluate BODY as python code." |
| 197 | (if session | 187 | (if session |
| 198 | (org-babel-python-evaluate-session | 188 | (org-babel-python-evaluate-session |
| 199 | session body result-type result-params) | 189 | session body result-type result-params) |
| 200 | (org-babel-python-evaluate-external-process | 190 | (org-babel-python-evaluate-external-process |
| 201 | body result-type result-params))) | 191 | body result-type result-params preamble))) |
| 202 | 192 | ||
| 203 | (defun org-babel-python-evaluate-external-process | 193 | (defun org-babel-python-evaluate-external-process |
| 204 | (body &optional result-type result-params) | 194 | (body &optional result-type result-params preamble) |
| 205 | "Evaluate BODY in external python process. | 195 | "Evaluate BODY in external python process. |
| 206 | If RESULT-TYPE equals 'output then return standard output as a | 196 | If RESULT-TYPE equals 'output then return standard output as a |
| 207 | string. If RESULT-TYPE equals 'value then return the value of the | 197 | string. If RESULT-TYPE equals 'value then return the value of the |
| 208 | last statement in BODY, as elisp." | 198 | last statement in BODY, as elisp." |
| 209 | (case result-type | 199 | (case result-type |
| 210 | (output (org-babel-eval org-babel-python-command body)) | 200 | (output (org-babel-eval org-babel-python-command |
| 201 | (concat (if preamble (concat preamble "\n") "") body))) | ||
| 211 | (value (let ((tmp-file (org-babel-temp-file "python-"))) | 202 | (value (let ((tmp-file (org-babel-temp-file "python-"))) |
| 212 | (org-babel-eval org-babel-python-command | 203 | (org-babel-eval org-babel-python-command |
| 213 | (format | 204 | (concat |
| 214 | (if (member "pp" result-params) | 205 | (if preamble (concat preamble "\n") "") |
| 215 | org-babel-python-pp-wrapper-method | 206 | (format |
| 216 | org-babel-python-wrapper-method) | 207 | (if (member "pp" result-params) |
| 217 | (mapconcat | 208 | org-babel-python-pp-wrapper-method |
| 218 | (lambda (line) (format "\t%s" line)) | 209 | org-babel-python-wrapper-method) |
| 219 | (split-string | 210 | (mapconcat |
| 220 | (org-remove-indentation | 211 | (lambda (line) (format "\t%s" line)) |
| 221 | (org-babel-trim body)) | 212 | (split-string |
| 222 | "[\r\n]") "\n") | 213 | (org-remove-indentation |
| 223 | (org-babel-process-file-name tmp-file 'noquote))) | 214 | (org-babel-trim body)) |
| 215 | "[\r\n]") "\n") | ||
| 216 | (org-babel-process-file-name tmp-file 'noquote)))) | ||
| 224 | ((lambda (raw) | 217 | ((lambda (raw) |
| 225 | (if (or (member "code" result-params) | 218 | (if (or (member "code" result-params) |
| 226 | (member "pp" result-params)) | 219 | (member "pp" result-params)) |
| @@ -240,7 +233,7 @@ last statement in BODY, as elisp." | |||
| 240 | (lambda (statement) (insert statement) (comint-send-input)) | 233 | (lambda (statement) (insert statement) (comint-send-input)) |
| 241 | (if pp | 234 | (if pp |
| 242 | (list | 235 | (list |
| 243 | "import pp" | 236 | "import pprint" |
| 244 | (format "open('%s', 'w').write(pprint.pformat(_))" | 237 | (format "open('%s', 'w').write(pprint.pformat(_))" |
| 245 | (org-babel-process-file-name tmp-file 'noquote))) | 238 | (org-babel-process-file-name tmp-file 'noquote))) |
| 246 | (list (format "open('%s', 'w').write(str(_))" | 239 | (list (format "open('%s', 'w').write(str(_))" |