Re: [gentoo-user] new machine : suddenly Python 3 appears
On Wed, Sep 19, 2012 at 09:08:52AM +0200, Penguin Lover Marc Joliet squawked: 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]. Yes, I recommend ipython too. 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. Unless you want to load the math module every single time you start Python, it is perhaps better to create an alias (say, python-calc) in bash (or shell of your choice) using the `-i' option of python like alias python-calc='python -i loadmath.py' or if you only need one single command alias python-calc='python -i -c from math import *' which will give you an interactive session with the math functions preloaded. Cheers, W -- Data aequatione quotcunque fluentes quantitae involvente fluxiones invenire et vice versa ~~~ I. Newton
Re: [gentoo-user] new machine : suddenly Python 3 appears
Am Thu, 20 Sep 2012 13:38:45 +0200 schrieb Willie WY Wong wong...@member.ams.org: On Wed, Sep 19, 2012 at 09:08:52AM +0200, Penguin Lover Marc Joliet squawked: 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]. Yes, I recommend ipython too. 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. Unless you want to load the math module every single time you start Python, it is perhaps better to create an alias (say, python-calc) in bash (or shell of your choice) using the `-i' option of python like alias python-calc='python -i loadmath.py' or if you only need one single command alias python-calc='python -i -c from math import *' which will give you an interactive session with the math functions preloaded. You are right, exporting PYTHONSTARTUP globally for something like this in the bashrc would in this case be stupid, or at least wasteful. I like your alias version better :) . Cheers, W Greetings -- Marc Joliet -- People who think they know everything really annoy those of us who know we don't - Bjarne Stroustrup signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
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 signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
Am Mon, 17 Sep 2012 23:27:34 +0100 schrieb David W Noon dwn...@ntlworld.com: On Mon, 17 Sep 2012 17:17:47 -0400, Philip Webb wrote about Re: [gentoo-user] new machine : suddenly Python 3 appears: 120917 Philip Webb wrote: 120917 David W Noon wrote: print(' {0}'.format(eval(expression))) That works properly with Python2 in this machine ; I'll check it with Python3 later in the new machine. That *is* Python 3 syntax. It is also accepted under recent releases of Python 2. 120917 Marc Joliet wrote: print(' ',eval(expression)) That does the calculation, but the output is wrongly formatted : 514 bin pycalc1 2+3 (' ', 5) This is because Marc's code is syntactically invalid for Python 3. It is acceptable to Python 2, but does not do what you want; but it won't work at all under Python 3. No, he simply executed it using python2, hence he printed a tuple. My version certainly prints the exact same output as the python2 version, *when executed by python3*. You tried it out first, right? I certainly did. Note that the only difference to your version is that yours does string formatting via str.format(), which also exists in Python 2. That of course has the advantage of working the same in Python 2 and 3 (*if* you care about that): the argument to print is not a tuple anymore, so it is not formatted as such: $ python3 -c print('{0}'.format(3)) 3 $ python2 -c print('{0}'.format(3)) 3 It is clear that you have not taken my advice to use the -3 run-time option in your hash-bang line. At the risk of blowing my own trumpet, that was *extremely sound* advice; you should really take it. It would have revealed the problems with the above code during the Python interpreter's initial scan of the code. I'll repeat it: !#/usr/bin/python2 -3 This will perform a Python 3 syntax check, even under Python 2. It will identify any going-forward issues for your Python script(s). I also just remembered the 2to3 program, which will translate trivial (such as print vs. print()) and (I think) some not so trivial cases to python3 syntax for you. Personally I think you should just convert your scripts to python3 and be done with it, unless you want to avoid python3 forever. Of course, Philip did say this is his only python script, so using python2 -3 might actually be a tad overkill for his purposes, unless he wants to start learning python, in which case: why not just start with python 3? HTH -- Marc Joliet -- People who think they know everything really annoy those of us who know we don't - Bjarne Stroustrup signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
On Sun, 16 Sep 2012 20:24:01 -0400, Philip Webb wrote: We've been warned not to use Python 3 , so it's not installed in this box, but it was included along with Python 2 in the Stage3 for the new machine. I now find that 13 pkgs have been compiled relying on it Portage refuses to unmerge it. Is this safe ? Perfectly safe, because we weren't warned to not use it, only to not set it as the default. That is reasonable because older scripts won't be aware of the differences between python 2 and 3, while newer scripts can explicitly call whichever version they need. The only problem with setting python 3 as the default is that some older scripts may break. Since portage became python3- aware there is no reason to not have it installed beyond the 30MB of disk space it occupies. -- Neil Bothwick WinErr 00C: Memory hog error - More Ram needed. More! More! More! signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
120917 Neil Bothwick wrote: On Sun, 16 Sep 2012 20:24:01 -0400, Philip Webb wrote: We've been warned not to use Python 3 , so it's not installed in this box, but it was included along with Python 2 in the Stage3 for the new machine. I now find that 13 pkgs have been compiled relying on it Portage refuses to unmerge it. Is this safe ? Perfectly safe, because we weren't warned to not use it, only to not set it as the default. That is reasonable because older scripts aren't aware of the differences between Python 2/3, while newer scripts can explicitly call whichever version they need. The only problem with setting Python 3 as the default is that some older scripts may break. I discovered this because my little script to do CLI calculations -- by far the fastest of anything, if you don't need variables -- wouldn't work in the new machine till I did s/python/python2 . In case others might like to use it, the script is : #!/usr/bin/python2 from math import * import sys expression = sys.argv[1] print ' ',eval(expression) Its help is via 'pydoc math'. Expressions need quotes if they have brackets. It was failing with a syntax error in the print line, when the 1st line read #!/usr/bin/python , so I have to assume (1) that Python3 has been set as default -- No ! I didn't do it ! -- (2) its syntax for printing has changed. Thanks for the polite explanation. Further comments welcome. -- ,, SUPPORT ___//___, Philip Webb ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto TRANSIT`-O--O---' purslowatchassdotutorontodotca
Re: [gentoo-user] new machine : suddenly Python 3 appears
On Mon, 17 Sep 2012 13:14:27 -0400, Philip Webb wrote about Re: [gentoo-user] new machine : suddenly Python 3 appears: In case others might like to use it, the script is : #!/usr/bin/python2 from math import * import sys expression = sys.argv[1] print ' ',eval(expression) The above line uses obsolete syntax. A big RTFM is required to get the new syntax, as print is no longer a statement, but a subroutine (or void function in C-speak). Its help is via 'pydoc math'. Expressions need quotes if they have brackets. It was failing with a syntax error in the print line, when the 1st line read #!/usr/bin/python , so I have to assume (1) that Python3 has been set as default -- No ! I didn't do it ! -- (2) its syntax for printing has changed. The latter -- and perhaps the former too, but that is irrelevant from a going-forward point of view. In fact, print changed a few years back, but Python 2,x tolerates the old syntax unless you specify the -3 run-time option. This option was also recommended a few years back, so that syntax that will be flagged by Python 3.x can be detected early (i.e. a few years back). So, try using #!/usr/bin/python2 -3 for your hash-bang line on all your old Python scripts. -- Regards, Dave [RLU #314465] *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* dwn...@ntlworld.com (David W Noon) *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
120917 David W Noon wrote: On Mon, 17 Sep 2012 13:14:27 -0400, Philip Webb wrote re Python 2/3 : print ' ',eval(expression) The above line uses obsolete syntax. Try using #!/usr/bin/python2 -3 for your hash-bang line on all your old Python scripts. Well, thanks for the info -- which is what I suspected -- , but just what is the correct Python3 syntax for that simple print line ? This is my only Python script, which I got from somewhere long forgotten, I generally don't have a need to do Python programming. While this subject is open, can anyone tell me how to get Python3 started from CLI automatically to load the math item ? -- ie to do 'from math import *' without my having to type it ? That would make it possible to use 'python' instead of my script, which would then allow me to use variables, sometimes an advantage. -- ,, SUPPORT ___//___, Philip Webb ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto TRANSIT`-O--O---' purslowatchassdotutorontodotca
Re: [gentoo-user] new machine : suddenly Python 3 appears
Am Mon, 17 Sep 2012 14:45:41 -0400 schrieb Philip Webb purs...@ca.inter.net: 120917 David W Noon wrote: On Mon, 17 Sep 2012 13:14:27 -0400, Philip Webb wrote re Python 2/3 : print ' ',eval(expression) The above line uses obsolete syntax. Try using #!/usr/bin/python2 -3 for your hash-bang line on all your old Python scripts. Well, thanks for the info -- which is what I suspected -- , but just what is the correct Python3 syntax for that simple print line ? This is my only Python script, which I got from somewhere long forgotten, I generally don't have a need to do Python programming. print ' ',eval(expression) becomes print(' ',eval(expression)) While this subject is open, can anyone tell me how to get Python3 started from CLI automatically to load the math item ? -- ie to do 'from math import *' without my having to type it ? That would make it possible to use 'python' instead of my script, which would then allow me to use variables, sometimes an advantage. Sorry, I don't know, and skimming through the python man page didn't turn up anything except -m module, which executes a python module as a script. Perhaps someone else knows a possibility. Apart from that, I don't really understand what you want to do from there. Could you maybe explain in more detail? HTH -- Marc Joliet -- People who think they know everything really annoy those of us who know we don't - Bjarne Stroustrup signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
On Mon, 17 Sep 2012 14:45:41 -0400, Philip Webb wrote about Re: [gentoo-user] new machine : suddenly Python 3 appears: 120917 David W Noon wrote: On Mon, 17 Sep 2012 13:14:27 -0400, Philip Webb wrote re Python 2/3 : print ' ',eval(expression) The above line uses obsolete syntax. Try using Some interesting quoting there. Those 2 sentences were quite a few paragraphs apart when I typed them and now they're on a single line. #!/usr/bin/python2 -3 for your hash-bang line on all your old Python scripts. Well, thanks for the info -- which is what I suspected -- , but just what is the correct Python3 syntax for that simple print line ? print(' {0}'.format(eval(expression))) Pretty, isn't it? ... :-) Just be aware that the above is only for your original print statement. There are a myriad of new formatting options that address various other configurations of the old print statement and the above does not cover anything like all of them. That's why I wrote that a big RTFM is required to learn the new syntax. This is my only Python script, which I got from somewhere long forgotten, I generally don't have a need to do Python programming. In the Gentoo world, programming in Python is always helpful. While this subject is open, can anyone tell me how to get Python3 started from CLI automatically to load the math item ? -- ie to do 'from math import *' without my having to type it ? You can't. It is program code and you have to code it yourself. That would make it possible to use 'python' instead of my script, which would then allow me to use variables, sometimes an advantage. I would simply write a more flexible script, one that does exactly what I need it to do. If your Python variable named expression contains an arithmetic expression, you might be able to get the shell to evaluate it for you, thus eliminating the need for Python; just be aware that shells do not normally do logarithms, trigonometry or other transcendental functions. -- Regards, Dave [RLU #314465] *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* dwn...@ntlworld.com (David W Noon) *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* signature.asc Description: PGP signature
Re: [gentoo-user] new machine : suddenly Python 3 appears
120917 David W Noon wrote: On Mon, 17 Sep 2012 14:45:41 -0400, Philip Webb asked about print ' ',eval(expression) whose syntax is obsolete in Python3. print(' {0}'.format(eval(expression))) be aware that the above is only for your original print statement. There are a myriad of new formatting options that address various other configurations of the old print statement and the above does not cover anything like all of them. Thanks : someone else suggested a different format below. It doesn't matter today, but perhaps Python2 will disappear sometime. how to get Python3 started from CLI automatically to load the math item ? -- ie to do 'from math import *' without my having to type it ? You can't. It is program code and you have to code it yourself. If your Python variable expression contains an arithmetic expression, you might be able to get the shell to evaluate it for you, but shells do not normally do logs, trig or other transcendental functions. Yes, that's why I use my script, which does all the everyday jobs. 120917 Marc Joliet wrote: print ' ',eval(expression) becomes print(' ',eval(expression)) Thanks for that version too (smile). 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. -- ,, SUPPORT ___//___, Philip Webb ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto TRANSIT`-O--O---' purslowatchassdotutorontodotca
Re: [gentoo-user] new machine : suddenly Python 3 appears
120917 Philip Webb wrote: 120917 David W Noon wrote: print(' {0}'.format(eval(expression))) That works properly with Python2 in this machine ; I'll check it with Python3 later in the new machine. 120917 Marc Joliet wrote: print(' ',eval(expression)) That does the calculation, but the output is wrongly formatted : 514 bin pycalc1 2+3 (' ', 5) Thanks again -- ,, SUPPORT ___//___, Philip Webb ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto TRANSIT`-O--O---' purslowatchassdotutorontodotca
Re: [gentoo-user] new machine : suddenly Python 3 appears
On Mon, 17 Sep 2012 17:17:47 -0400, Philip Webb wrote about Re: [gentoo-user] new machine : suddenly Python 3 appears: 120917 Philip Webb wrote: 120917 David W Noon wrote: print(' {0}'.format(eval(expression))) That works properly with Python2 in this machine ; I'll check it with Python3 later in the new machine. That *is* Python 3 syntax. It is also accepted under recent releases of Python 2. 120917 Marc Joliet wrote: print(' ',eval(expression)) That does the calculation, but the output is wrongly formatted : 514 bin pycalc1 2+3 (' ', 5) This is because Marc's code is syntactically invalid for Python 3. It is acceptable to Python 2, but does not do what you want; but it won't work at all under Python 3. It is clear that you have not taken my advice to use the -3 run-time option in your hash-bang line. At the risk of blowing my own trumpet, that was *extremely sound* advice; you should really take it. It would have revealed the problems with the above code during the Python interpreter's initial scan of the code. I'll repeat it: !#/usr/bin/python2 -3 This will perform a Python 3 syntax check, even under Python 2. It will identify any going-forward issues for your Python script(s). -- Regards, Dave [RLU #314465] *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* dwn...@ntlworld.com (David W Noon) *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* signature.asc Description: PGP signature
[gentoo-user] new machine : suddenly Python 3 appears
We've been warned not to use Python 3 , so it's not installed in this box, but it was included along with Python 2 in the Stage3 for the new machine. I now find that 13 pkgs have been compiled relying on it Portage refuses to unmerge it. Is this safe ? -- ,, SUPPORT ___//___, Philip Webb ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto TRANSIT`-O--O---' purslowatchassdotutorontodotca
Re: [gentoo-user] new machine : suddenly Python 3 appears
Philip Webb wrote: We've been warned not to use Python 3 , so it's not installed in this box, but it was included along with Python 2 in the Stage3 for the new machine. I now find that 13 pkgs have been compiled relying on it Portage refuses to unmerge it. Is this safe ? If you want to get rid of python3, you need to put -python3 in your make.conf and then do a emerge -Na world to rebuild. That should rebuild everything that was built against python3 then you can unemerge it and emerge shouldn't complain either. If it does, you need to make sure why before removing it. Hope that helps. Dale :-) :-) -- I am only responsible for what I said ... Not for what you understood or how you interpreted my words!
Re: [gentoo-user] new machine : suddenly Python 3 appears
On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb purs...@ca.inter.net wrote: We've been warned not to use Python 3 Maybe I missed something...but by whom? -- :wq
Re: [gentoo-user] new machine : suddenly Python 3 appears
Michael Mol wrote: On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb purs...@ca.inter.net wrote: We've been warned not to use Python 3 Maybe I missed something...but by whom? I think he means that we are not supposed to set the system default to python3. I'm just reading tea leaves, between the lines etc but that was the way I took it. Dale :-) :-) -- I am only responsible for what I said ... Not for what you understood or how you interpreted my words!
Re: [gentoo-user] new machine : suddenly Python 3 appears
On Sun, Sep 16, 2012 at 9:18 PM, Dale rdalek1...@gmail.com wrote: Michael Mol wrote: On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb purs...@ca.inter.net wrote: We've been warned not to use Python 3 Maybe I missed something...but by whom? I think he means that we are not supposed to set the system default to python3. I'm just reading tea leaves, between the lines etc but that was the way I took it. And portage's deps don't handle it? Weird. -- :wq
Re: [gentoo-user] new machine : suddenly Python 3 appears
Michael Mol wrote: On Sun, Sep 16, 2012 at 9:18 PM, Dale rdalek1...@gmail.com wrote: Michael Mol wrote: On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb purs...@ca.inter.net wrote: We've been warned not to use Python 3 Maybe I missed something...but by whom? I think he means that we are not supposed to set the system default to python3. I'm just reading tea leaves, between the lines etc but that was the way I took it. And portage's deps don't handle it? Weird. Well, it appears he just doesn't want python3 on his system. Cool by me since he may have some good reason for it. So, if he wants it gone as his post suggests, I just told him how to do it that hopefully won't break things. Dale :-) :-) -- I am only responsible for what I said ... Not for what you understood or how you interpreted my words!