Am Mon, 17 Sep 2012 17:08:38 -0400
schrieb Philip Webb <purs...@ca.inter.net>:

[...]
> The one limitation of the script is that it doesn't allow variables ;
> you can easily recall previous lines via Bash & mouseover+drop bits,
> but AFAIK there's no way to assign values to variables.
> With Python running as interpreter, I would get much more capability,
> but I would need to enter the special line to load the math functions :
> is it possible to do it with some capitalised variable in  .bashrc ,
> which might list parameters telling Python3 what to load when it starts ?
> one of the 'man' files seems to refer to something like that, but briefly.

OK, do I understand correctly, you want an interactive session so you can use
temporary variables? I can think of two ways of doing that. From looking at the
python man page, I also found a third possibility not involving extra software.

1.) The poor man's version if you want to do this in bash/dash uses command
substitution:

  $ bla=$(python3 test.py 3+3)
  $ echo $bla
  $       6
  $ python3 test.py "$bla*2"
               12

2.) The full blown interactive solution: IPython. You can create a session and
configure which modules you want preloaded via startup scripts. This is
overkill for what you want, I think, but IPython is a much nicer interactive
Python interpreter than python itself. For instance, you can reuse previous
outputs, e.g. "Out[2]", to get the output from the third command you entered
(indexing starts at 0). Inputs can be similarly recalled by referencing
"In[i]".

3.) Put the "import" line in its own file and put it in the variable
PYTHONSTARTUP, e.g. "export PYTHONSTARTUP=/path/to/my/script.py". Python
executes it's contents before presenting the prompt, so you can put whatever
imports you want in that script. It's simple, and if the python interpreter is
enough for you, then I'd go with this.

There are probably more possibilities, but this is what I can think of right
now.

HTH

-- 
Marc Joliet
--
"People who think they know everything really annoy those of us who know we
don't" - Bjarne Stroustrup

Attachment: signature.asc
Description: PGP signature

Reply via email to