org-babel-python.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ;;; org-babel-python.el --- org-babel functions for python evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating python source code.
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'python)
  27. (org-babel-add-interpreter "python")
  28. (add-to-list 'org-babel-tangle-langs '("python" "py" "#!/usr/bin/env python"))
  29. (defun org-babel-execute:python (body params)
  30. "Execute a block of Python code with org-babel. This function is
  31. called by `org-babel-execute-src-block'."
  32. (message "executing Python source code block")
  33. (let* ((processed-params (org-babel-process-params params))
  34. (session (org-babel-python-initiate-session (first processed-params)))
  35. (vars (second processed-params))
  36. (result-params (third processed-params))
  37. (result-type (fourth processed-params))
  38. (full-body (concat
  39. (mapconcat ;; define any variables
  40. (lambda (pair)
  41. (format "%s=%s"
  42. (car pair)
  43. (org-babel-python-var-to-python (cdr pair))))
  44. vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
  45. (result (org-babel-python-evaluate session full-body result-type)))
  46. (or (cdr (assoc :file params))
  47. (org-babel-reassemble-table
  48. result (nth 4 processed-params) (nth 5 processed-params)))))
  49. (defun org-babel-prep-session:python (session params)
  50. "Prepare SESSION according to the header arguments specified in PARAMS."
  51. (let* ((session (org-babel-python-initiate-session session))
  52. (vars (org-babel-ref-variables params))
  53. (var-lines (mapcar ;; define any variables
  54. (lambda (pair)
  55. (format "%s=%s"
  56. (car pair)
  57. (org-babel-python-var-to-python (cdr pair))))
  58. vars)))
  59. (org-babel-comint-in-buffer session
  60. (mapc (lambda (var)
  61. (move-end-of-line 1) (insert var) (comint-send-input nil t)
  62. (org-babel-comint-wait-for-output session)) var-lines))
  63. session))
  64. (defun org-babel-load-session:python (session body params)
  65. "Load BODY into SESSION."
  66. (save-window-excursion
  67. (let ((buffer (org-babel-prep-session:python session params)))
  68. (with-current-buffer buffer
  69. (goto-char (process-mark (get-buffer-process (current-buffer))))
  70. (insert (org-babel-chomp body)))
  71. buffer)))
  72. ;; helper functions
  73. (defun org-babel-python-var-to-python (var)
  74. "Convert an elisp var into a string of python source code
  75. specifying a var of the same value."
  76. (if (listp var)
  77. (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
  78. (format "%S" var)))
  79. (defun org-babel-python-table-or-string (results)
  80. "If the results look like a list or tuple, then convert them into an
  81. Emacs-lisp table, otherwise return the results as a string."
  82. (org-babel-read
  83. (if (or (string-match "^\\[.+\\]$" results)
  84. (string-match "^(.+)$" results))
  85. (org-babel-read
  86. (concat "'"
  87. (replace-regexp-in-string
  88. "\\[" "(" (replace-regexp-in-string
  89. "\\]" ")" (replace-regexp-in-string
  90. ", " " " (replace-regexp-in-string
  91. "'" "\"" results))))))
  92. results)))
  93. (defvar org-babel-python-buffers '(:default . nil))
  94. (defun org-babel-python-session-buffer (session)
  95. (cdr (assoc session org-babel-python-buffers)))
  96. (defun org-babel-python-initiate-session-by-key (&optional session)
  97. "If there is not a current inferior-process-buffer in SESSION
  98. then create. Return the initialized session."
  99. (save-window-excursion
  100. (let* ((session (if session (intern session) :default))
  101. (python-buffer (org-babel-python-session-buffer session)))
  102. (run-python)
  103. (setq org-babel-python-buffers (cons (cons session python-buffer)
  104. (assq-delete-all session org-babel-python-buffers)))
  105. session)))
  106. (defun org-babel-python-initiate-session (&optional session params)
  107. (unless (string= session "none")
  108. (org-babel-python-session-buffer (org-babel-python-initiate-session-by-key session))))
  109. (defvar org-babel-python-last-value-eval "_"
  110. "When evaluated by Python this returns the return value of the last statement.")
  111. (defvar org-babel-python-pp-last-value-eval
  112. '("results = _"
  113. "import pprint"
  114. "org_babel_pp = pprint.PrettyPrinter()"
  115. "org_babel_pp.pprint(results)")
  116. "When evaluated by Python this pretty prints the value of the last statement.")
  117. (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'"
  118. "Used to indicate that evaluation is has completed.")
  119. (defvar org-babel-python-wrapper-method
  120. "
  121. def main():
  122. %s
  123. open('%s', 'w').write( str(main()) )")
  124. (defvar org-babel-python-pp-wrapper-method
  125. "
  126. import pprint
  127. def main():
  128. %s
  129. open('%s', 'w').write( pprint.pformat(main()) )")
  130. (defun org-babel-python-evaluate (buffer body &optional result-type)
  131. "Pass BODY to the Python process in BUFFER. If RESULT-TYPE equals
  132. 'output then return a list of the outputs of the statements in
  133. BODY, if RESULT-TYPE equals 'value then return the value of the
  134. last statement in BODY, as elisp."
  135. (if (not session)
  136. ;; external process evaluation
  137. (save-excursion
  138. (case result-type
  139. (output
  140. (with-temp-buffer
  141. (insert body)
  142. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  143. (org-babel-shell-command-on-region (point-min) (point-max) "python" 'current-buffer 'replace)
  144. (buffer-string)))
  145. (value
  146. (let* ((tmp-file (make-temp-file "org-babel-python-results-")) exit-code
  147. (stderr
  148. (with-temp-buffer
  149. (insert
  150. (format
  151. (if (member "pp" result-params)
  152. org-babel-python-pp-wrapper-method
  153. org-babel-python-wrapper-method)
  154. (mapconcat
  155. (lambda (line) (format "\t%s" line))
  156. (split-string
  157. (org-remove-indentation (org-babel-trim body)) "[\r\n]") "\n")
  158. tmp-file))
  159. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  160. (setq exit-code (org-babel-shell-command-on-region
  161. (point-min) (point-max) "python" nil 'replace (current-buffer)))
  162. (buffer-string))))
  163. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  164. (let ((raw (with-temp-buffer
  165. (insert-file-contents (org-babel-maybe-remote-file tmp-file))
  166. (buffer-string))))
  167. (if (or (member "code" result-params) (member "pp" result-params))
  168. raw
  169. (org-babel-python-table-or-string raw)))))))
  170. ;; comint session evaluation
  171. (org-babel-comint-in-buffer buffer
  172. (let* ((raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
  173. ;; for some reason python is fussy, and likes enters after every input
  174. (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
  175. (split-string (org-babel-trim full-body) "[\r\n]+"))
  176. (comint-send-input nil t) (comint-send-input nil t)
  177. (if (member "pp" result-params)
  178. (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
  179. org-babel-python-pp-last-value-eval)
  180. (insert org-babel-python-last-value-eval))
  181. (comint-send-input nil t) (comint-send-input nil t)
  182. (insert org-babel-python-eoe-indicator)
  183. (comint-send-input nil t)))
  184. (results (delete org-babel-python-eoe-indicator
  185. (cdr (member org-babel-python-eoe-indicator
  186. (reverse (mapcar #'org-babel-trim raw)))))))
  187. (unless (or (member "code" result-params) (member "pp" result-params))
  188. (setq results (mapcar #'org-babel-python-read-string results)))
  189. (case result-type
  190. (output (org-babel-trim (mapconcat #'identity (reverse (cdr results)) "\n")))
  191. (value
  192. (if (or (member "code" result-params) (member "pp" result-params))
  193. (car results)
  194. (org-babel-python-table-or-string (org-babel-trim (car results))))))))))
  195. (defun org-babel-python-read-string (string)
  196. "Strip 's from around python string"
  197. (if (string-match "^'\\([^\000]+\\)'$" string)
  198. (match-string 1 string)
  199. string))
  200. (provide 'org-babel-python)
  201. ;;; org-babel-python.el ends here