From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sebastian Miele Subject: Re: noweb and :var statements Date: Mon, 07 Oct 2019 09:22:59 +0000 Message-ID: <87imp0zz7w.fsf@gmail.com> References: <87k19hg7g1.fsf@gmail.com> <87k19hirz7.fsf@gmail.com> <87imp1irdr.fsf@gmail.com> <87ftk5fxi5.fsf@gmail.com> <87ftk5inu4.fsf@gmail.com> <87d0f9f807.fsf@gmail.com> Reply-To: sebastian.miele@gmail.com Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:53707) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iHPES-0007JF-9D for emacs-orgmode@gnu.org; Mon, 07 Oct 2019 05:23:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iHPEN-0003y9-8E for emacs-orgmode@gnu.org; Mon, 07 Oct 2019 05:23:08 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]:54991) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iHPEN-0003xu-2V for emacs-orgmode@gnu.org; Mon, 07 Oct 2019 05:23:03 -0400 Received: by mail-wm1-x334.google.com with SMTP id p7so11811470wmp.4 for ; Mon, 07 Oct 2019 02:23:03 -0700 (PDT) In-reply-to: <87d0f9f807.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Ken Mankoff Cc: emacs-orgmode@gnu.org Ken Mankoff writes: > 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 <> 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 <> at > the top, and then hide the lengthy <> code elsewhere. Is this a > correct use case? Yes. > 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 <> just pastes the body, and > <> 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. A solution without having to write code-writing code is tangling to different files (MWE3.py and setup.py). See the example below. I read somewhere that Python has stong reflection features. It should be possible to write Python code that, given a string and a value, introduces a Python variable of that name and binds the value to it. If you find out how that works, the mangle block below can be changed so that the main block has e.g. setup.A.sum() instead of setup.tables["A"].sum(). Final disclaimer: There may and probably are other possibilies that I do not know or have not thought of. * Main Project #+NAME: main #+BEGIN_SRC python :tangle MWE3.py :noweb yes :results output import setup print(setup.tables["A"].sum()) print(setup.tables["B"].sum()) #+END_SRC * Data Tables #+NAME: table_42 | foo | |-----| | 42 | | 42 | #+NAME: table_100 | bar | |-----| | 100 | * Setup #+BEGIN_SRC python :tangle setup.py import numpy as np tables={} #+END_SRC #+NAME: mangle #+BEGIN_SRC python tables[name] = np.array(table).astype(np.float).flatten() #+END_SRC #+BEGIN_SRC python :tangle setup.py :noweb yes :var table=table_42 :var name="A" <> #+END_SRC #+BEGIN_SRC python :tangle setup.py :noweb yes :var table=table_100 :var name="B" <> #+END_SRC