Mercurial > emacs
comparison lisp/org/ob-python.el @ 111506:5cb272c831e8
Install org-mode version 7.3
author | Carsten Dominik <carsten.dominik@gmail.com> |
---|---|
date | Thu, 11 Nov 2010 22:10:19 -0600 |
parents | a150e8a14679 |
children | 76c2ff4450eb |
comparison
equal
deleted
inserted
replaced
111505:62aa3653746a | 111506:5cb272c831e8 |
---|---|
3 ;; Copyright (C) 2009, 2010 Free Software Foundation | 3 ;; Copyright (C) 2009, 2010 Free Software Foundation |
4 | 4 |
5 ;; Author: Eric Schulte, Dan Davison | 5 ;; Author: Eric Schulte, Dan Davison |
6 ;; Keywords: literate programming, reproducible research | 6 ;; Keywords: literate programming, reproducible research |
7 ;; Homepage: http://orgmode.org | 7 ;; Homepage: http://orgmode.org |
8 ;; Version: 7.01 | 8 ;; Version: 7.3 |
9 | 9 |
10 ;; This file is part of GNU Emacs. | 10 ;; This file is part of GNU Emacs. |
11 | 11 |
12 ;; GNU Emacs is free software: you can redistribute it and/or modify | 12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
13 ;; it under the terms of the GNU General Public License as published by | 13 ;; it under the terms of the GNU General Public License as published by |
45 "Name of command for executing python code.") | 45 "Name of command for executing python code.") |
46 | 46 |
47 (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) | 47 (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) |
48 "Preferred python mode for use in running python interactively.") | 48 "Preferred python mode for use in running python interactively.") |
49 | 49 |
50 (defun org-babel-expand-body:python (body params &optional processed-params) | 50 (defvar org-src-preserve-indentation) |
51 "Expand BODY according to PARAMS, return the expanded body." | |
52 (concat | |
53 (mapconcat ;; define any variables | |
54 (lambda (pair) | |
55 (format "%s=%s" | |
56 (car pair) | |
57 (org-babel-python-var-to-python (cdr pair)))) | |
58 (nth 1 (or processed-params (org-babel-process-params params))) "\n") | |
59 "\n" (org-babel-trim body) "\n")) | |
60 | 51 |
61 (defun org-babel-execute:python (body params) | 52 (defun org-babel-execute:python (body params) |
62 "Execute a block of Python code with Babel. | 53 "Execute a block of Python code with Babel. |
63 This function is called by `org-babel-execute-src-block'." | 54 This function is called by `org-babel-execute-src-block'." |
64 (let* ((processed-params (org-babel-process-params params)) | 55 (let* ((session (org-babel-python-initiate-session |
65 (session (org-babel-python-initiate-session (first processed-params))) | 56 (cdr (assoc :session params)))) |
66 (result-params (nth 2 processed-params)) | 57 (result-params (cdr (assoc :result-params params))) |
67 (result-type (nth 3 processed-params)) | 58 (result-type (cdr (assoc :result-type params))) |
68 (full-body (org-babel-expand-body:python | 59 (full-body |
69 body params processed-params)) | 60 (org-babel-expand-body:generic |
61 body params (org-babel-variable-assignments:python params))) | |
70 (result (org-babel-python-evaluate | 62 (result (org-babel-python-evaluate |
71 session full-body result-type result-params))) | 63 session full-body result-type result-params))) |
72 (or (cdr (assoc :file params)) | 64 (or (cdr (assoc :file params)) |
73 (org-babel-reassemble-table | 65 (org-babel-reassemble-table |
74 result | 66 result |
75 (org-babel-pick-name (nth 4 processed-params) | 67 (org-babel-pick-name (cdr (assoc :colname-names params)) |
76 (cdr (assoc :colnames params))) | 68 (cdr (assoc :colnames params))) |
77 (org-babel-pick-name (nth 5 processed-params) | 69 (org-babel-pick-name (cdr (assoc :rowname-names params)) |
78 (cdr (assoc :rownames params))))))) | 70 (cdr (assoc :rownames params))))))) |
79 | 71 |
80 (defun org-babel-prep-session:python (session params) | 72 (defun org-babel-prep-session:python (session params) |
81 "Prepare SESSION according to the header arguments in PARAMS." | 73 "Prepare SESSION according to the header arguments in PARAMS. |
74 VARS contains resolved variable references" | |
82 (let* ((session (org-babel-python-initiate-session session)) | 75 (let* ((session (org-babel-python-initiate-session session)) |
83 (vars (org-babel-ref-variables params)) | 76 (var-lines |
84 (var-lines (mapcar ;; define any variables | 77 (org-babel-variable-assignments:python params))) |
85 (lambda (pair) | |
86 (format "%s=%s" | |
87 (car pair) | |
88 (org-babel-python-var-to-python (cdr pair)))) | |
89 vars))) | |
90 (org-babel-comint-in-buffer session | 78 (org-babel-comint-in-buffer session |
91 (mapc (lambda (var) | 79 (mapc (lambda (var) |
92 (end-of-line 1) (insert var) (comint-send-input) | 80 (end-of-line 1) (insert var) (comint-send-input) |
93 (org-babel-comint-wait-for-output session)) var-lines)) | 81 (org-babel-comint-wait-for-output session)) var-lines)) |
94 session)) | 82 session)) |
101 (goto-char (process-mark (get-buffer-process (current-buffer)))) | 89 (goto-char (process-mark (get-buffer-process (current-buffer)))) |
102 (insert (org-babel-chomp body))) | 90 (insert (org-babel-chomp body))) |
103 buffer))) | 91 buffer))) |
104 | 92 |
105 ;; helper functions | 93 ;; helper functions |
94 | |
95 (defun org-babel-variable-assignments:python (params) | |
96 "Return list of python statements assigning the block's variables" | |
97 (mapcar | |
98 (lambda (pair) | |
99 (format "%s=%s" | |
100 (car pair) | |
101 (org-babel-python-var-to-python (cdr pair)))) | |
102 (mapcar #'cdr (org-babel-get-header params :var)))) | |
106 | 103 |
107 (defun org-babel-python-var-to-python (var) | 104 (defun org-babel-python-var-to-python (var) |
108 "Convert an elisp value to a python variable. | 105 "Convert an elisp value to a python variable. |
109 Convert an elisp value, VAR, into a string of python source code | 106 Convert an elisp value, VAR, into a string of python source code |
110 specifying a variable of the same value." | 107 specifying a variable of the same value." |
123 ((lambda (res) | 120 ((lambda (res) |
124 (if (listp res) | 121 (if (listp res) |
125 (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res) | 122 (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res) |
126 res)) | 123 res)) |
127 (org-babel-read | 124 (org-babel-read |
128 (if (or (string-match "^\\[.+\\]$" results) | 125 (if (and (stringp results) (string-match "^[([].+[])]$" results)) |
129 (string-match "^(.+)$" results)) | |
130 (org-babel-read | 126 (org-babel-read |
131 (concat "'" | 127 (concat "'" |
132 (replace-regexp-in-string | 128 (replace-regexp-in-string |
133 "\\[" "(" (replace-regexp-in-string | 129 "\\[" "(" (replace-regexp-in-string |
134 "\\]" ")" (replace-regexp-in-string | 130 "\\]" ")" (replace-regexp-in-string |
149 (require org-babel-python-mode) | 145 (require org-babel-python-mode) |
150 (save-window-excursion | 146 (save-window-excursion |
151 (let* ((session (if session (intern session) :default)) | 147 (let* ((session (if session (intern session) :default)) |
152 (python-buffer (org-babel-python-session-buffer session))) | 148 (python-buffer (org-babel-python-session-buffer session))) |
153 (cond | 149 (cond |
154 ((and (equal 'python org-babel-python-mode) | 150 ((and (eq 'python org-babel-python-mode) |
155 (fboundp 'run-python)) ; python.el | 151 (fboundp 'run-python)) ; python.el |
156 (run-python)) | 152 (run-python)) |
157 ((and (equal 'python-mode org-babel-python-mode) | 153 ((and (eq 'python-mode org-babel-python-mode) |
158 (fboundp 'py-shell)) ; python-mode.el | 154 (fboundp 'py-shell)) ; python-mode.el |
159 ;; `py-shell' creates a buffer whose name is the value of | 155 ;; `py-shell' creates a buffer whose name is the value of |
160 ;; `py-which-bufname' with '*'s at the beginning and end | 156 ;; `py-which-bufname' with '*'s at the beginning and end |
161 (let* ((bufname (if python-buffer | 157 (let* ((bufname (if python-buffer |
162 (replace-regexp-in-string ;; zap surrounding * | 158 (replace-regexp-in-string ;; zap surrounding * |
193 %s | 189 %s |
194 | 190 |
195 open('%s', 'w').write( pprint.pformat(main()) )") | 191 open('%s', 'w').write( pprint.pformat(main()) )") |
196 | 192 |
197 (defun org-babel-python-evaluate | 193 (defun org-babel-python-evaluate |
198 (buffer body &optional result-type result-params) | 194 (session body &optional result-type result-params) |
199 "Pass BODY to the Python process in BUFFER. | 195 "Evaluate BODY as python code." |
200 If RESULT-TYPE equals 'output then return a list of the outputs | 196 (if session |
201 of the statements in BODY, if RESULT-TYPE equals 'value then | 197 (org-babel-python-evaluate-session |
202 return the value of the last statement in BODY, as elisp." | 198 session body result-type result-params) |
203 (if (not buffer) | 199 (org-babel-python-evaluate-external-process |
204 ;; external process evaluation | 200 body result-type result-params))) |
205 (case result-type | 201 |
206 (output (org-babel-eval org-babel-python-command body)) | 202 (defun org-babel-python-evaluate-external-process |
207 (value (let ((tmp-file (make-temp-file "org-babel-python-results-"))) | 203 (body &optional result-type result-params) |
208 (org-babel-eval org-babel-python-command | 204 "Evaluate BODY in external python process. |
209 (format | 205 If RESULT-TYPE equals 'output then return standard output as a |
210 (if (member "pp" result-params) | 206 string. If RESULT-TYPE equals 'value then return the value of the |
211 org-babel-python-pp-wrapper-method | 207 last statement in BODY, as elisp." |
212 org-babel-python-wrapper-method) | 208 (case result-type |
213 (mapconcat | 209 (output (org-babel-eval org-babel-python-command body)) |
214 (lambda (line) (format "\t%s" line)) | 210 (value (let ((tmp-file (org-babel-temp-file "python-"))) |
215 (split-string | 211 (org-babel-eval org-babel-python-command |
216 (org-remove-indentation | 212 (format |
217 (org-babel-trim body)) | 213 (if (member "pp" result-params) |
218 "[\r\n]") "\n") | 214 org-babel-python-pp-wrapper-method |
219 tmp-file)) | 215 org-babel-python-wrapper-method) |
220 ((lambda (raw) | 216 (mapconcat |
221 (if (or (member "code" result-params) | 217 (lambda (line) (format "\t%s" line)) |
222 (member "pp" result-params)) | 218 (split-string |
223 raw | 219 (org-remove-indentation |
224 (org-babel-python-table-or-string raw))) | 220 (org-babel-trim body)) |
225 (org-babel-eval-read-file tmp-file))))) | 221 "[\r\n]") "\n") |
226 ;; comint session evaluation | 222 (org-babel-process-file-name tmp-file 'noquote))) |
227 (flet ((dump-last-value (tmp-file pp) | 223 ((lambda (raw) |
228 (mapc | 224 (if (or (member "code" result-params) |
229 (lambda (statement) (insert statement) (comint-send-input)) | 225 (member "pp" result-params)) |
230 (if pp | 226 raw |
231 (list | 227 (org-babel-python-table-or-string raw))) |
232 "import pp" | 228 (org-babel-eval-read-file tmp-file)))))) |
233 (format "open('%s', 'w').write(pprint.pformat(_))" tmp-file)) | 229 |
234 (list (format "open('%s', 'w').write(str(_))" tmp-file))))) | 230 (defun org-babel-python-evaluate-session |
235 (input-body (body) | 231 (session body &optional result-type result-params) |
236 (mapc (lambda (statement) (insert statement) (comint-send-input)) | 232 "Pass BODY to the Python process in SESSION. |
237 (split-string (org-babel-trim body) "[\r\n]+")) | 233 If RESULT-TYPE equals 'output then return standard output as a |
238 (comint-send-input) (comint-send-input))) | 234 string. If RESULT-TYPE equals 'value then return the value of the |
239 (case result-type | 235 last statement in BODY, as elisp." |
240 (output | 236 (flet ((dump-last-value |
241 (mapconcat | 237 (tmp-file pp) |
242 #'org-babel-trim | 238 (mapc |
243 (butlast | 239 (lambda (statement) (insert statement) (comint-send-input)) |
244 (org-babel-comint-with-output | 240 (if pp |
245 (buffer org-babel-python-eoe-indicator t body) | 241 (list |
246 (let ((comint-process-echoes nil)) | 242 "import pp" |
247 (input-body body) | 243 (format "open('%s', 'w').write(pprint.pformat(_))" |
248 (insert org-babel-python-eoe-indicator) | 244 (org-babel-process-file-name tmp-file 'noquote))) |
249 (comint-send-input))) 2) "\n")) | 245 (list (format "open('%s', 'w').write(str(_))" |
250 (value | 246 (org-babel-process-file-name tmp-file 'noquote)))))) |
251 ((lambda (results) | 247 (input-body (body) |
252 (if (or (member "code" result-params) (member "pp" result-params)) | 248 (mapc (lambda (statement) (insert statement) (comint-send-input)) |
253 results | 249 (split-string (org-babel-trim body) "[\r\n]+")) |
254 (org-babel-python-table-or-string results))) | 250 (comint-send-input) (comint-send-input))) |
255 (let ((tmp-file (make-temp-file "org-babel-python-results-"))) | 251 (case result-type |
256 (org-babel-comint-with-output | 252 (output |
257 (buffer org-babel-python-eoe-indicator t body) | 253 (mapconcat |
258 (let ((comint-process-echoes nil)) | 254 #'org-babel-trim |
259 (input-body body) | 255 (butlast |
260 (dump-last-value tmp-file (member "pp" result-params)) | 256 (org-babel-comint-with-output |
261 (comint-send-input) (comint-send-input) | 257 (session org-babel-python-eoe-indicator t body) |
262 (insert org-babel-python-eoe-indicator) | 258 (let ((comint-process-echoes nil)) |
263 (comint-send-input))) | 259 (input-body body) |
264 (org-babel-eval-read-file tmp-file)))))))) | 260 (insert org-babel-python-eoe-indicator) |
261 (comint-send-input))) 2) "\n")) | |
262 (value | |
263 ((lambda (results) | |
264 (if (or (member "code" result-params) (member "pp" result-params)) | |
265 results | |
266 (org-babel-python-table-or-string results))) | |
267 (let ((tmp-file (org-babel-temp-file "python-"))) | |
268 (org-babel-comint-with-output | |
269 (session org-babel-python-eoe-indicator t body) | |
270 (let ((comint-process-echoes nil)) | |
271 (input-body body) | |
272 (dump-last-value tmp-file (member "pp" result-params)) | |
273 (comint-send-input) (comint-send-input) | |
274 (insert org-babel-python-eoe-indicator) | |
275 (comint-send-input))) | |
276 (org-babel-eval-read-file tmp-file))))))) | |
265 | 277 |
266 (defun org-babel-python-read-string (string) | 278 (defun org-babel-python-read-string (string) |
267 "Strip 's from around python string" | 279 "Strip 's from around python string" |
268 (if (string-match "^'\\([^\000]+\\)'$" string) | 280 (if (string-match "^'\\([^\000]+\\)'$" string) |
269 (match-string 1 string) | 281 (match-string 1 string) |