summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Kamm <jackkamm@gmail.com>2020-09-07 09:39:21 -0700
committerJack Kamm <jackkamm@gmail.com>2020-09-07 09:39:21 -0700
commit6f9929fc3b03f63006337c72c50db0c1e47265e3 (patch)
tree298eaab2c1c4e3bd40bf5fccaa5be74cafd16381
parentdfad6488d3ed7a44ab07a93bb3b1ca4ba14fbbfd (diff)
downloadorg-mode-6f9929fc3b03f63006337c72c50db0c1e47265e3.tar.gz
ob-python: Replace session value format string with function
* lisp/ob-python.el (org-babel-python--eval-ast): Removed. (org-babel-python-format-session-value): New function that returns Python code to evaluate for sessions with value results. (org-babel-python-evaluate-session): Replace org-babel-python--eval-ast with org-babel-python-format-session-value. Motivation is that the new function is more flexible than the old format string. We can pass in the full result-params, which can be used to add more formatting options in future. We could also add more optional arguments in the future to extend this functionality. This function will be particularly helpful for a couple patches after 9.4: - ob-reticulate - https://orgmode.org/list/875z98gj4f.fsf@gmail.com/ - Improved formatting - https://orgmode.org/list/87eenpfe77.fsf@gmail.com/ However, I add this function in now 9.4, so those future patches can rely on it in 9.4. In particular, ob-reticulate will probably be packaged separately from org-mode, and I want to provide a stable interface to it so that it can be released after 9.4.
-rw-r--r--lisp/ob-python.el23
1 files changed, 9 insertions, 14 deletions
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 113e923..1cded45 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -245,7 +245,10 @@ with open('%s') as f:
Has a single %s escape, the tempfile containing the source code
to evaluate.")
-(defconst org-babel-python--eval-ast "\
+(defun org-babel-python-format-session-value
+ (src-file result-file result-params)
+ "Return Python code to evaluate SRC-FILE and write result to RESULT-FILE."
+ (format "\
import ast
with open('%s') as f:
__org_babel_python_ast = ast.parse(f.read())
@@ -264,12 +267,9 @@ if isinstance(__org_babel_python_final, ast.Expr):
else:
exec(compile(__org_babel_python_ast, '<string>', 'exec'))
__org_babel_python_final = None"
- "Template for Python session command with value results.
-
-Has three %s escapes to be filled in:
-1. Tempfile containing source to evaluate.
-2. Tempfile to write results to.
-3. Whether to pretty print, \"True\" or \"False\".")
+ (org-babel-process-file-name src-file 'noquote)
+ (org-babel-process-file-name result-file 'noquote)
+ (if (member "pp" result-params) "True" "False")))
(defun org-babel-python-evaluate
(session body &optional result-type result-params preamble)
@@ -359,13 +359,8 @@ last statement in BODY, as elisp."
(org-babel-python--send-string session body)))
(`value
(let* ((tmp-results-file (org-babel-temp-file "python-"))
- (body (format org-babel-python--eval-ast
- (org-babel-process-file-name
- tmp-src-file 'noquote)
- (org-babel-process-file-name
- tmp-results-file 'noquote)
- (if (member "pp" result-params)
- "True" "False"))))
+ (body (org-babel-python-format-session-value
+ tmp-src-file tmp-results-file result-params)))
(org-babel-python--send-string session body)
(sleep-for 0 10)
(org-babel-eval-read-file tmp-results-file)))))))