Gentoo Archives: gentoo-user

From: Marc Joliet <marcec@×××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] new machine : suddenly Python 3 appears
Date: Wed, 19 Sep 2012 07:11:21
Message-Id: 20120919090852.177ba25c@marcec.hunte.us
In Reply to: Re: [gentoo-user] new machine : suddenly Python 3 appears by Philip Webb
1 Am Mon, 17 Sep 2012 17:08:38 -0400
2 schrieb Philip Webb <purslow@××××××××.net>:
3
4 [...]
5 > The one limitation of the script is that it doesn't allow variables ;
6 > you can easily recall previous lines via Bash & mouseover+drop bits,
7 > but AFAIK there's no way to assign values to variables.
8 > With Python running as interpreter, I would get much more capability,
9 > but I would need to enter the special line to load the math functions :
10 > is it possible to do it with some capitalised variable in .bashrc ,
11 > which might list parameters telling Python3 what to load when it starts ?
12 > one of the 'man' files seems to refer to something like that, but briefly.
13
14 OK, do I understand correctly, you want an interactive session so you can use
15 temporary variables? I can think of two ways of doing that. From looking at the
16 python man page, I also found a third possibility not involving extra software.
17
18 1.) The poor man's version if you want to do this in bash/dash uses command
19 substitution:
20
21 $ bla=$(python3 test.py 3+3)
22 $ echo $bla
23 $ 6
24 $ python3 test.py "$bla*2"
25 12
26
27 2.) The full blown interactive solution: IPython. You can create a session and
28 configure which modules you want preloaded via startup scripts. This is
29 overkill for what you want, I think, but IPython is a much nicer interactive
30 Python interpreter than python itself. For instance, you can reuse previous
31 outputs, e.g. "Out[2]", to get the output from the third command you entered
32 (indexing starts at 0). Inputs can be similarly recalled by referencing
33 "In[i]".
34
35 3.) Put the "import" line in its own file and put it in the variable
36 PYTHONSTARTUP, e.g. "export PYTHONSTARTUP=/path/to/my/script.py". Python
37 executes it's contents before presenting the prompt, so you can put whatever
38 imports you want in that script. It's simple, and if the python interpreter is
39 enough for you, then I'd go with this.
40
41 There are probably more possibilities, but this is what I can think of right
42 now.
43
44 HTH
45
46 --
47 Marc Joliet
48 --
49 "People who think they know everything really annoy those of us who know we
50 don't" - Bjarne Stroustrup

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies

Subject Author
Re: [gentoo-user] new machine : Python calculator Philip Webb <purslow@××××××××.net>
Re: [gentoo-user] new machine : suddenly Python 3 appears Willie WY Wong <wongwwy@××××××××××.org>