mirror of
https://git.savannah.gnu.org/git/emacs/org-mode.git
synced 2024-09-29 22:47:56 +00:00
babel: python: refactor `org-babel-python-evaluate'
* ob-python.el (org-babel-python-evaluate): Refactor as call to either `org-babel-python-evaluate-external-process' or `org-babel-python-evaluate-session'. (org-babel-python-evaluate-external-process): New function to handle evaluation in external process. (org-babel-python-evaluate-session): New function to handle evaluation in emacs inferior process.
This commit is contained in:
parent
dd9d42f515
commit
7127eb44c1
|
@ -194,73 +194,87 @@ def main():
|
||||||
open('%s', 'w').write( pprint.pformat(main()) )")
|
open('%s', 'w').write( pprint.pformat(main()) )")
|
||||||
|
|
||||||
(defun org-babel-python-evaluate
|
(defun org-babel-python-evaluate
|
||||||
(buffer body &optional result-type result-params)
|
(session body &optional result-type result-params)
|
||||||
"Pass BODY to the Python process in BUFFER.
|
"Evaluate BODY as python code."
|
||||||
If RESULT-TYPE equals 'output then return a list of the outputs
|
(if session
|
||||||
of the statements in BODY, if RESULT-TYPE equals 'value then
|
(org-babel-python-evaluate-session
|
||||||
return the value of the last statement in BODY, as elisp."
|
session body result-type result-params)
|
||||||
(if (not buffer)
|
(org-babel-python-evaluate-external-process
|
||||||
;; external process evaluation
|
body result-type result-params)))
|
||||||
(case result-type
|
|
||||||
(output (org-babel-eval org-babel-python-command body))
|
(defun org-babel-python-evaluate-external-process
|
||||||
(value (let ((tmp-file (org-babel-temp-file "python-results-")))
|
(body &optional result-type result-params)
|
||||||
(org-babel-eval org-babel-python-command
|
"Evaluate BODY in external python process.
|
||||||
(format
|
If RESULT-TYPE equals 'output then return standard output as a
|
||||||
(if (member "pp" result-params)
|
string. If RESULT-TYPE equals 'value then return the value of the
|
||||||
org-babel-python-pp-wrapper-method
|
last statement in BODY, as elisp."
|
||||||
org-babel-python-wrapper-method)
|
(case result-type
|
||||||
(mapconcat
|
(output (org-babel-eval org-babel-python-command body))
|
||||||
(lambda (line) (format "\t%s" line))
|
(value (let ((tmp-file (org-babel-temp-file "python-results-")))
|
||||||
(split-string
|
(org-babel-eval org-babel-python-command
|
||||||
(org-remove-indentation
|
(format
|
||||||
(org-babel-trim body))
|
(if (member "pp" result-params)
|
||||||
"[\r\n]") "\n")
|
org-babel-python-pp-wrapper-method
|
||||||
tmp-file))
|
org-babel-python-wrapper-method)
|
||||||
((lambda (raw)
|
(mapconcat
|
||||||
(if (or (member "code" result-params)
|
(lambda (line) (format "\t%s" line))
|
||||||
(member "pp" result-params))
|
(split-string
|
||||||
raw
|
(org-remove-indentation
|
||||||
(org-babel-python-table-or-string raw)))
|
(org-babel-trim body))
|
||||||
(org-babel-eval-read-file tmp-file)))))
|
"[\r\n]") "\n")
|
||||||
;; comint session evaluation
|
tmp-file))
|
||||||
(flet ((dump-last-value (tmp-file pp)
|
((lambda (raw)
|
||||||
(mapc
|
(if (or (member "code" result-params)
|
||||||
(lambda (statement) (insert statement) (comint-send-input))
|
(member "pp" result-params))
|
||||||
(if pp
|
raw
|
||||||
(list
|
(org-babel-python-table-or-string raw)))
|
||||||
"import pp"
|
(org-babel-eval-read-file tmp-file))))))
|
||||||
(format "open('%s', 'w').write(pprint.pformat(_))" tmp-file))
|
|
||||||
(list (format "open('%s', 'w').write(str(_))" tmp-file)))))
|
(defun org-babel-python-evaluate-session
|
||||||
(input-body (body)
|
(session body &optional result-type result-params)
|
||||||
(mapc (lambda (statement) (insert statement) (comint-send-input))
|
"Pass BODY to the Python process in SESSION.
|
||||||
(split-string (org-babel-trim body) "[\r\n]+"))
|
If RESULT-TYPE equals 'output then return standard output as a
|
||||||
(comint-send-input) (comint-send-input)))
|
string. If RESULT-TYPE equals 'value then return the value of the
|
||||||
(case result-type
|
last statement in BODY, as elisp."
|
||||||
(output
|
(flet ((dump-last-value
|
||||||
(mapconcat
|
(tmp-file pp)
|
||||||
#'org-babel-trim
|
(mapc
|
||||||
(butlast
|
(lambda (statement) (insert statement) (comint-send-input))
|
||||||
(org-babel-comint-with-output
|
(if pp
|
||||||
(buffer org-babel-python-eoe-indicator t body)
|
(list
|
||||||
(let ((comint-process-echoes nil))
|
"import pp"
|
||||||
(input-body body)
|
(format "open('%s', 'w').write(pprint.pformat(_))" tmp-file))
|
||||||
(insert org-babel-python-eoe-indicator)
|
(list (format "open('%s', 'w').write(str(_))" tmp-file)))))
|
||||||
(comint-send-input))) 2) "\n"))
|
(input-body (body)
|
||||||
(value
|
(mapc (lambda (statement) (insert statement) (comint-send-input))
|
||||||
((lambda (results)
|
(split-string (org-babel-trim body) "[\r\n]+"))
|
||||||
(if (or (member "code" result-params) (member "pp" result-params))
|
(comint-send-input) (comint-send-input)))
|
||||||
results
|
(case result-type
|
||||||
(org-babel-python-table-or-string results)))
|
(output
|
||||||
(let ((tmp-file (org-babel-temp-file "python-results-")))
|
(mapconcat
|
||||||
(org-babel-comint-with-output
|
#'org-babel-trim
|
||||||
(buffer org-babel-python-eoe-indicator t body)
|
(butlast
|
||||||
(let ((comint-process-echoes nil))
|
(org-babel-comint-with-output
|
||||||
(input-body body)
|
(session org-babel-python-eoe-indicator t body)
|
||||||
(dump-last-value tmp-file (member "pp" result-params))
|
(let ((comint-process-echoes nil))
|
||||||
(comint-send-input) (comint-send-input)
|
(input-body body)
|
||||||
(insert org-babel-python-eoe-indicator)
|
(insert org-babel-python-eoe-indicator)
|
||||||
(comint-send-input)))
|
(comint-send-input))) 2) "\n"))
|
||||||
(org-babel-eval-read-file tmp-file))))))))
|
(value
|
||||||
|
((lambda (results)
|
||||||
|
(if (or (member "code" result-params) (member "pp" result-params))
|
||||||
|
results
|
||||||
|
(org-babel-python-table-or-string results)))
|
||||||
|
(let ((tmp-file (org-babel-temp-file "python-results-")))
|
||||||
|
(org-babel-comint-with-output
|
||||||
|
(session org-babel-python-eoe-indicator t body)
|
||||||
|
(let ((comint-process-echoes nil))
|
||||||
|
(input-body body)
|
||||||
|
(dump-last-value tmp-file (member "pp" result-params))
|
||||||
|
(comint-send-input) (comint-send-input)
|
||||||
|
(insert org-babel-python-eoe-indicator)
|
||||||
|
(comint-send-input)))
|
||||||
|
(org-babel-eval-read-file tmp-file)))))))
|
||||||
|
|
||||||
(defun org-babel-python-read-string (string)
|
(defun org-babel-python-read-string (string)
|
||||||
"Strip 's from around python string"
|
"Strip 's from around python string"
|
||||||
|
|
Loading…
Reference in a new issue