emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] noweb and :var statements


From: Ken Mankoff
Subject: Re: [O] noweb and :var statements
Date: Mon, 07 Oct 2019 07:18:48 +0200
User-agent: mu4e 0.9.18; emacs 26.3

Hi Sebastian,

Thanks for your help. I was running with "-Q" but must have been making some 
other mistakes. It does work.

As for your other email... I do know several tangles can go to the same file. 
And I may be using <<noweb>> incorrectly, but I'm using it for the following 
reasons:

1) I'd like to bury code that must run first but is inconsequential at the 
bottom of the Org file. Noweb lets me have a tangled <<setup>> at the top, and 
then hide the lengthy <<setup>> code elsewhere. Is this a correct use case?

2) I'd like to import 10 tables, so I thought a noweb function might be useful 
for code reuse.

I finally got the behavior I'm looking for. What I need to remember/understand 
is that <<noweb>> just pastes the body, and <<noweb()>> evaluates the function. 
From this, my Python code needs to generate Python code! I now have the 
following MWE that behaves as I want both for in-buffer C-c C-c eval of main 
code block and tangled results. The key bit of code is the last babel block.

Thanks for your help,

  -k.


* MWE init
#+BEGIN_SRC emacs-lisp :results output
(setq org-confirm-babel-evaluate nil)
(org-babel-do-load-languages 'org-babel-load-languages '((python . t)))
(print (emacs-version))
(print (org-version nil t))
(org-babel-tangle)
#+END_SRC

* Main Project

#+NAME: main
#+BEGIN_SRC python :tangle MWE3.py :noweb yes :results output
<<setup>>
print(t42.sum())
print(t100.sum())
#+END_SRC

#+RESULTS: main
: 84.0
: 100.0

* Data Tables
#+NAME: table_42
| foo |
|-----|
|  42 |
|  42 |

#+NAME: table_100
| bar |
|-----|
| 100 |

* Setup

#+NAME: setup
#+BEGIN_SRC python :noweb yes
import numpy as np
<<import_table_to_varname(table=table_42, varname="t42")>>
<<import_table_to_varname(table=table_100, varname="t100")>>
#+END_SRC

* Table Import Code

#+NAME: import_table_to_varname
#+BEGIN_SRC python :var table=table_42 :var varname="foo" :noweb yes :results 
output
print(varname + "=np.array(" + str(table) + ").astype(np.float).flatten()")
#+END_SRC



reply via email to

[Prev in Thread] Current Thread [Next in Thread]