emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Sebastian Miele <sebastian.miele@gmail.com>
To: Ken Mankoff <mankoff@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: noweb and :var statements
Date: Mon, 07 Oct 2019 09:22:59 +0000	[thread overview]
Message-ID: <87imp0zz7w.fsf@gmail.com> (raw)
In-Reply-To: <87d0f9f807.fsf@gmail.com>


Ken Mankoff <mankoff@gmail.com> 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 <<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?

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 <<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.

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"
<<mangle>>
#+END_SRC

#+BEGIN_SRC python :tangle setup.py :noweb yes :var table=table_100 :var name="B"
<<mangle>>
#+END_SRC

  reply	other threads:[~2019-10-07  9:23 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-06 16:33 noweb and :var statements Ken Mankoff
2019-10-06 19:39 ` Sebastian Miele
2019-10-06 19:52   ` Sebastian Miele
2019-10-06 20:08     ` Ken Mankoff
2019-10-06 21:08       ` Sebastian Miele
2019-10-06 21:17         ` Sebastian Miele
2019-10-07  5:18         ` Ken Mankoff
2019-10-07  9:22           ` Sebastian Miele [this message]
2019-10-06 20:05   ` Ken Mankoff
2019-10-06 20:55     ` Sebastian Miele

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87imp0zz7w.fsf@gmail.com \
    --to=sebastian.miele@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mankoff@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).