nominally working
[org-mode/org-jambu.git] / lisp / ob-python.el
blob91c8545297af78e8fc7927e079ed2155f6bec44c
1 ;;; ob-python.el --- org-babel functions for python evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.01trans
10 ;; This file is part of GNU Emacs.
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating python source code.
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-ref)
32 (require 'ob-comint)
33 (require 'ob-eval)
34 (eval-when-compile (require 'cl))
36 (declare-function org-remove-indentation "org" )
37 (declare-function py-shell "ext:python-mode" (&optional argprompt))
38 (declare-function run-python "ext:python" (&optional cmd noshow new))
40 (add-to-list 'org-babel-tangle-lang-exts '("python" . "py"))
42 (defvar org-babel-default-header-args:python '())
44 (defvar org-babel-python-command "python"
45 "Name of command for executing python code.")
47 (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python)
48 "Preferred python mode for use in running python interactively.")
50 (defvar org-src-preserve-indentation)
51 (defun org-babel-expand-body:python (body params &optional processed-params)
52 "Expand BODY according to PARAMS, return the expanded body."
53 (let ((var-lines
54 (org-babel-python-variable-assignments params processed-params)))
55 (mapconcat
56 #'identity
57 (append
58 (org-babel-python-variable-assignments params processed-params)
59 (list
60 (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]"))))
61 "\n")))
63 (defun org-babel-execute:python (body params)
64 "Execute a block of Python code with Babel.
65 This function is called by `org-babel-execute-src-block'."
66 (let* ((processed-params (org-babel-process-params params))
67 (session (org-babel-python-initiate-session (first processed-params)))
68 (result-params (nth 2 processed-params))
69 (result-type (nth 3 processed-params))
70 (full-body (org-babel-expand-body:python
71 body params processed-params))
72 (result (org-babel-python-evaluate
73 session full-body result-type result-params)))
74 (or (cdr (assoc :file params))
75 (org-babel-reassemble-table
76 result
77 (org-babel-pick-name (nth 4 processed-params)
78 (cdr (assoc :colnames params)))
79 (org-babel-pick-name (nth 5 processed-params)
80 (cdr (assoc :rownames params)))))))
82 (defun org-babel-prep-session:python (session params)
83 "Prepare SESSION according to the header arguments in PARAMS."
84 (let* ((session (org-babel-python-initiate-session session))
85 (var-lines (org-babel-python-variable-assignments params)))
86 (org-babel-comint-in-buffer session
87 (mapc (lambda (var)
88 (end-of-line 1) (insert var) (comint-send-input)
89 (org-babel-comint-wait-for-output session)) var-lines))
90 session))
92 (defun org-babel-load-session:python (session body params)
93 "Load BODY into SESSION."
94 (save-window-excursion
95 (let ((buffer (org-babel-prep-session:python session params)))
96 (with-current-buffer buffer
97 (goto-char (process-mark (get-buffer-process (current-buffer))))
98 (insert (org-babel-chomp body)))
99 buffer)))
101 ;; helper functions
103 (defun org-babel-python-variable-assignments (params &optional processed-params)
104 "Return list of python statements assigning the block's variables"
105 (mapcar
106 (lambda (pair)
107 (format "%s=%s"
108 (car pair)
109 (org-babel-python-var-to-python (cdr pair))))
110 (nth 1 (or processed-params (org-babel-process-params params)))))
112 (defun org-babel-python-var-to-python (var)
113 "Convert an elisp value to a python variable.
114 Convert an elisp value, VAR, into a string of python source code
115 specifying a variable of the same value."
116 (if (listp var)
117 (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
118 (if (equal var 'hline)
119 "None"
120 (format
121 (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S")
122 var))))
124 (defun org-babel-python-table-or-string (results)
125 "Convert RESULTS into an appropriate elisp value.
126 If the results look like a list or tuple, then convert them into an
127 Emacs-lisp table, otherwise return the results as a string."
128 ((lambda (res)
129 (if (listp res)
130 (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res)
131 res))
132 (org-babel-read
133 (if (and (stringp results) (string-match "^[([].+[])]$" results))
134 (org-babel-read
135 (concat "'"
136 (replace-regexp-in-string
137 "\\[" "(" (replace-regexp-in-string
138 "\\]" ")" (replace-regexp-in-string
139 ", " " " (replace-regexp-in-string
140 "'" "\"" results t))))))
141 results))))
143 (defvar org-babel-python-buffers '((:default . nil)))
145 (defun org-babel-python-session-buffer (session)
146 "Return the buffer associated with SESSION."
147 (cdr (assoc session org-babel-python-buffers)))
149 (defun org-babel-python-initiate-session-by-key (&optional session)
150 "Initiate a python session.
151 If there is not a current inferior-process-buffer in SESSION
152 then create. Return the initialized session."
153 (require org-babel-python-mode)
154 (save-window-excursion
155 (let* ((session (if session (intern session) :default))
156 (python-buffer (org-babel-python-session-buffer session)))
157 (cond
158 ((and (eq 'python org-babel-python-mode)
159 (fboundp 'run-python)) ; python.el
160 (run-python))
161 ((and (eq 'python-mode org-babel-python-mode)
162 (fboundp 'py-shell)) ; python-mode.el
163 ;; `py-shell' creates a buffer whose name is the value of
164 ;; `py-which-bufname' with '*'s at the beginning and end
165 (let* ((bufname (if python-buffer
166 (replace-regexp-in-string ;; zap surrounding *
167 "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer)
168 (concat "Python-" (symbol-name session))))
169 (py-which-bufname bufname))
170 (py-shell)
171 (setq python-buffer (concat "*" bufname "*"))))
173 (error "No function available for running an inferior python.")))
174 (setq org-babel-python-buffers
175 (cons (cons session python-buffer)
176 (assq-delete-all session org-babel-python-buffers)))
177 session)))
179 (defun org-babel-python-initiate-session (&optional session params)
180 "Create a session named SESSION according to PARAMS."
181 (unless (string= session "none")
182 (org-babel-python-session-buffer
183 (org-babel-python-initiate-session-by-key session))))
185 (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'"
186 "A string to indicate that evaluation has completed.")
187 (defvar org-babel-python-wrapper-method
189 def main():
192 open('%s', 'w').write( str(main()) )")
193 (defvar org-babel-python-pp-wrapper-method
195 import pprint
196 def main():
199 open('%s', 'w').write( pprint.pformat(main()) )")
201 (defun org-babel-python-evaluate
202 (session body &optional result-type result-params)
203 "Evaluate BODY as python code."
204 (if session
205 (org-babel-python-evaluate-session
206 session body result-type result-params)
207 (org-babel-python-evaluate-external-process
208 body result-type result-params)))
210 (defun org-babel-python-evaluate-external-process
211 (body &optional result-type result-params)
212 "Evaluate BODY in external python process.
213 If RESULT-TYPE equals 'output then return standard output as a
214 string. If RESULT-TYPE equals 'value then return the value of the
215 last statement in BODY, as elisp."
216 (case result-type
217 (output (org-babel-eval org-babel-python-command body))
218 (value (let ((tmp-file (org-babel-temp-file "python-")))
219 (org-babel-eval org-babel-python-command
220 (format
221 (if (member "pp" result-params)
222 org-babel-python-pp-wrapper-method
223 org-babel-python-wrapper-method)
224 (mapconcat
225 (lambda (line) (format "\t%s" line))
226 (split-string
227 (org-remove-indentation
228 (org-babel-trim body))
229 "[\r\n]") "\n")
230 (org-babel-process-file-name tmp-file 'noquote)))
231 ((lambda (raw)
232 (if (or (member "code" result-params)
233 (member "pp" result-params))
235 (org-babel-python-table-or-string raw)))
236 (org-babel-eval-read-file tmp-file))))))
238 (defun org-babel-python-evaluate-session
239 (session body &optional result-type result-params)
240 "Pass BODY to the Python process in SESSION.
241 If RESULT-TYPE equals 'output then return standard output as a
242 string. If RESULT-TYPE equals 'value then return the value of the
243 last statement in BODY, as elisp."
244 (flet ((dump-last-value
245 (tmp-file pp)
246 (mapc
247 (lambda (statement) (insert statement) (comint-send-input))
248 (if pp
249 (list
250 "import pp"
251 (format "open('%s', 'w').write(pprint.pformat(_))"
252 (org-babel-process-file-name tmp-file 'noquote)))
253 (list (format "open('%s', 'w').write(str(_))"
254 (org-babel-process-file-name tmp-file 'noquote))))))
255 (input-body (body)
256 (mapc (lambda (statement) (insert statement) (comint-send-input))
257 (split-string (org-babel-trim body) "[\r\n]+"))
258 (comint-send-input) (comint-send-input)))
259 (case result-type
260 (output
261 (mapconcat
262 #'org-babel-trim
263 (butlast
264 (org-babel-comint-with-output
265 (session org-babel-python-eoe-indicator t body)
266 (let ((comint-process-echoes nil))
267 (input-body body)
268 (insert org-babel-python-eoe-indicator)
269 (comint-send-input))) 2) "\n"))
270 (value
271 ((lambda (results)
272 (if (or (member "code" result-params) (member "pp" result-params))
273 results
274 (org-babel-python-table-or-string results)))
275 (let ((tmp-file (org-babel-temp-file "python-")))
276 (org-babel-comint-with-output
277 (session org-babel-python-eoe-indicator t body)
278 (let ((comint-process-echoes nil))
279 (input-body body)
280 (dump-last-value tmp-file (member "pp" result-params))
281 (comint-send-input) (comint-send-input)
282 (insert org-babel-python-eoe-indicator)
283 (comint-send-input)))
284 (org-babel-eval-read-file tmp-file)))))))
286 (defun org-babel-python-read-string (string)
287 "Strip 's from around python string"
288 (if (string-match "^'\\([^\000]+\\)'$" string)
289 (match-string 1 string)
290 string))
292 (provide 'ob-python)
294 ;; arch-tag: f19b6c3d-dfcb-4a1a-9ce0-45ade1ebc212
296 ;;; ob-python.el ends here