emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Tangling without clutter?


From: Jacek Generowicz
Subject: Re: [O] Tangling without clutter?
Date: Thu, 15 Mar 2012 09:42:26 +0100
User-agent: Wanderlust/2.14.0 (Africa) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (Gojō) APEL/10.8 Emacs/23.3 (i686-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO)

At Thu, 15 Mar 2012 07:25:47 +0100,
Viktor Rosenfeld wrote:
> 
> Hi Jos'h,
> 
> have you looked at the :session header argument? I use it to define
> environment variables in bash that are used in later code blocks.
> 
> Something like this:
> 
> #+BEGIN_SRC sh :session foo
> export W="world."
> #+END_SRC
> 
> #+RESULTS:
> 
> #+BEGIN_SRC sh :session foo
> echo Hello $W
> #+END_SRC
> 
> If these code blocks are executed in order, the latter returns "Hello
> world."

Which is exactly how I was planning to use org babel to prepare a lot
of Python-based teaching material. Unfortunately ...

> Not sure though, how it interacts with Python.

... it interacts with Python in a less than ideal way. In effect, it
pretends you typed the code in the Python source block, into Python's
standard interactive shell. This leads to problems: Imagine that you
want to demonstrate how to write a simple class in Python, and that
you wish to follow Python's style guide, which states that "Method
definitions inside a class are separated by a single blank line."

So you present this example code

#+begin_src python :session :results output
class Foo:

    def __init__(self, state):
        self.state = state

    def get(self):
        return self.state
#+end_src

in the hope of writing some explanatory notes about it, before
demonstrating its use

#+begin_src python :session :results output
f = Foo('frustrated')
print f.get()
#+end_src

Unfortunately, the plan is foiled because the class definiton in the
first block fails to execute properly. This happens because Python's
standard interactive shell (in contrast to Python's non interactive
mode) understands blank lines to close all currently open blocks. This
results in the =class Foo:= block being closed before any of its
components have been defined, at which point the whole thing goes
pear-shaped.

Currently there are two things you can do to get around this problem.

1. Remove any blank lines which are inside any block in any of your
   code (resulting in very ugly and heterodox formatting of your
   Python code: not the sort of thing you want to be doing in
   tutorials, lectures, documentation, etc.).

2. Hunt down all blank lines which are inside a block, and add enough
   whitespace to match the indentation of the next non-whitespace line
   in that block (very tedious and very fragile).


Last week I had a short exchange about this, on list, with Eric
Schulte, and seem to have persuaded him that the current state of
affairs is not the desired one.

I am planning to have a look at how the situation can be improved with
the aim of providing a patch, but

1. I have zero time to devote to this in the next few weeks.

2. I have never tinkered with babel's internals before, so I cannot
   guarantee success, or quality of output.


In the meantime, I have *slightly* changed my position, since the
exchange with Eric. Last week I claimed that what the session mode
should do is, essentially, emulate Emacs' python-mode's C-c C-c
(execute the code block in the context of the session without choking
on blank lines, and without echoing any of the code itself). I stand
by this being the desired primary behaviour, but I do admit that an
alternative valid (though less important) use case would be to have
the code in the src block be 'typed' into a standard interactive
session, and have the whole session (prompt, input, output) be
produced as the result. (IIRC, this currently doesn't work properly
either: the prompts and the inputs are out of sync, and you get a
horrible mess.)



reply via email to

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