* [gentoo-user] new machine : suddenly Python 3 appears
@ 2012-09-17 0:24 Philip Webb
2012-09-17 0:46 ` Dale
` (2 more replies)
0 siblings, 3 replies; 26+ messages in thread
From: Philip Webb @ 2012-09-17 0:24 UTC (permalink / raw
To: Gentoo User
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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 0:24 [gentoo-user] new machine : suddenly Python 3 appears Philip Webb
@ 2012-09-17 0:46 ` Dale
2012-09-17 1:03 ` Michael Mol
2012-09-17 8:04 ` Neil Bothwick
2 siblings, 0 replies; 26+ messages in thread
From: Dale @ 2012-09-17 0:46 UTC (permalink / raw
To: gentoo-user
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!
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 0:24 [gentoo-user] new machine : suddenly Python 3 appears Philip Webb
2012-09-17 0:46 ` Dale
@ 2012-09-17 1:03 ` Michael Mol
2012-09-17 1:18 ` Dale
2012-09-17 8:04 ` Neil Bothwick
2 siblings, 1 reply; 26+ messages in thread
From: Michael Mol @ 2012-09-17 1:03 UTC (permalink / raw
To: gentoo-user
On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb <purslow@ca.inter.net> wrote:
> We've been warned not to use Python 3
Maybe I missed something...but by whom?
--
:wq
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 1:03 ` Michael Mol
@ 2012-09-17 1:18 ` Dale
2012-09-17 1:24 ` Michael Mol
0 siblings, 1 reply; 26+ messages in thread
From: Dale @ 2012-09-17 1:18 UTC (permalink / raw
To: gentoo-user
Michael Mol wrote:
> On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb <purslow@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!
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 1:18 ` Dale
@ 2012-09-17 1:24 ` Michael Mol
2012-09-17 1:54 ` Dale
0 siblings, 1 reply; 26+ messages in thread
From: Michael Mol @ 2012-09-17 1:24 UTC (permalink / raw
To: gentoo-user
On Sun, Sep 16, 2012 at 9:18 PM, Dale <rdalek1967@gmail.com> wrote:
> Michael Mol wrote:
>> On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb <purslow@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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 1:24 ` Michael Mol
@ 2012-09-17 1:54 ` Dale
0 siblings, 0 replies; 26+ messages in thread
From: Dale @ 2012-09-17 1:54 UTC (permalink / raw
To: gentoo-user
Michael Mol wrote:
> On Sun, Sep 16, 2012 at 9:18 PM, Dale <rdalek1967@gmail.com> wrote:
>> Michael Mol wrote:
>>> On Sun, Sep 16, 2012 at 8:24 PM, Philip Webb <purslow@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!
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 0:24 [gentoo-user] new machine : suddenly Python 3 appears Philip Webb
2012-09-17 0:46 ` Dale
2012-09-17 1:03 ` Michael Mol
@ 2012-09-17 8:04 ` Neil Bothwick
2012-09-17 17:14 ` Philip Webb
2 siblings, 1 reply; 26+ messages in thread
From: Neil Bothwick @ 2012-09-17 8:04 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 907 bytes --]
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!
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 8:04 ` Neil Bothwick
@ 2012-09-17 17:14 ` Philip Webb
2012-09-17 18:08 ` David W Noon
0 siblings, 1 reply; 26+ messages in thread
From: Philip Webb @ 2012-09-17 17:14 UTC (permalink / raw
To: gentoo-user
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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 17:14 ` Philip Webb
@ 2012-09-17 18:08 ` David W Noon
2012-09-17 18:45 ` Philip Webb
0 siblings, 1 reply; 26+ messages in thread
From: David W Noon @ 2012-09-17 18:08 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 1531 bytes --]
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]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 18:08 ` David W Noon
@ 2012-09-17 18:45 ` Philip Webb
2012-09-17 20:17 ` Marc Joliet
2012-09-17 20:38 ` David W Noon
0 siblings, 2 replies; 26+ messages in thread
From: Philip Webb @ 2012-09-17 18:45 UTC (permalink / raw
To: gentoo-user
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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 18:45 ` Philip Webb
@ 2012-09-17 20:17 ` Marc Joliet
2012-09-17 20:38 ` David W Noon
1 sibling, 0 replies; 26+ messages in thread
From: Marc Joliet @ 2012-09-17 20:17 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 1551 bytes --]
Am Mon, 17 Sep 2012 14:45:41 -0400
schrieb Philip Webb <purslow@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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 18:45 ` Philip Webb
2012-09-17 20:17 ` Marc Joliet
@ 2012-09-17 20:38 ` David W Noon
2012-09-17 21:08 ` Philip Webb
1 sibling, 1 reply; 26+ messages in thread
From: David W Noon @ 2012-09-17 20:38 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 2336 bytes --]
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]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 20:38 ` David W Noon
@ 2012-09-17 21:08 ` Philip Webb
2012-09-17 21:17 ` Philip Webb
2012-09-19 7:08 ` Marc Joliet
0 siblings, 2 replies; 26+ messages in thread
From: Philip Webb @ 2012-09-17 21:08 UTC (permalink / raw
To: gentoo-user
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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 21:08 ` Philip Webb
@ 2012-09-17 21:17 ` Philip Webb
2012-09-17 22:27 ` David W Noon
2012-09-19 7:08 ` Marc Joliet
1 sibling, 1 reply; 26+ messages in thread
From: Philip Webb @ 2012-09-17 21:17 UTC (permalink / raw
To: gentoo-user
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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 21:17 ` Philip Webb
@ 2012-09-17 22:27 ` David W Noon
2012-09-18 7:36 ` Marc Joliet
0 siblings, 1 reply; 26+ messages in thread
From: David W Noon @ 2012-09-17 22:27 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 1556 bytes --]
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]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 22:27 ` David W Noon
@ 2012-09-18 7:36 ` Marc Joliet
0 siblings, 0 replies; 26+ messages in thread
From: Marc Joliet @ 2012-09-18 7:36 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 2779 bytes --]
Am Mon, 17 Sep 2012 23:27:34 +0100
schrieb David W Noon <dwnoon@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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-17 21:08 ` Philip Webb
2012-09-17 21:17 ` Philip Webb
@ 2012-09-19 7:08 ` Marc Joliet
2012-09-20 9:05 ` [gentoo-user] new machine : Python calculator Philip Webb
2012-09-20 11:38 ` [gentoo-user] new machine : suddenly Python 3 appears Willie WY Wong
1 sibling, 2 replies; 26+ messages in thread
From: Marc Joliet @ 2012-09-19 7:08 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 2154 bytes --]
Am Mon, 17 Sep 2012 17:08:38 -0400
schrieb Philip Webb <purslow@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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : Python calculator
2012-09-19 7:08 ` Marc Joliet
@ 2012-09-20 9:05 ` Philip Webb
2012-09-20 20:32 ` Marc Joliet
2012-09-20 11:38 ` [gentoo-user] new machine : suddenly Python 3 appears Willie WY Wong
1 sibling, 1 reply; 26+ messages in thread
From: Philip Webb @ 2012-09-20 9:05 UTC (permalink / raw
To: gentoo-user
120919 Marc Joliet wrote:
> 120918 Philip Webb <purslow@ca.inter.net> wrote:
>> 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.
> 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.
Thanks, that's what I saw in my brief glance at the 'man'.
It works out of the box: the only problem is precision,
which at 16 decimal places is a bit more than I usually need (smile).
I can search out how to limit it to something more useful to me,
but might you have a quick answer ? Thanks for the above.
--
========================,,============================================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-19 7:08 ` Marc Joliet
2012-09-20 9:05 ` [gentoo-user] new machine : Python calculator Philip Webb
@ 2012-09-20 11:38 ` Willie WY Wong
2012-09-20 20:35 ` Marc Joliet
2012-09-21 6:31 ` [gentoo-user] Re: Python precision Philip Webb
1 sibling, 2 replies; 26+ messages in thread
From: Willie WY Wong @ 2012-09-20 11:38 UTC (permalink / raw
To: gentoo-user
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
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : Python calculator
2012-09-20 9:05 ` [gentoo-user] new machine : Python calculator Philip Webb
@ 2012-09-20 20:32 ` Marc Joliet
2012-09-20 20:57 ` Marc Joliet
0 siblings, 1 reply; 26+ messages in thread
From: Marc Joliet @ 2012-09-20 20:32 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 2526 bytes --]
Am Thu, 20 Sep 2012 05:05:11 -0400
schrieb Philip Webb <purslow@ca.inter.net>:
> 120919 Marc Joliet wrote:
> > 120918 Philip Webb <purslow@ca.inter.net> wrote:
> >> 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.
> > 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.
>
> Thanks, that's what I saw in my brief glance at the 'man'.
> It works out of the box: the only problem is precision,
> which at 16 decimal places is a bit more than I usually need (smile).
> I can search out how to limit it to something more useful to me,
> but might you have a quick answer ? Thanks for the above.
Reading up the "format specification mini language"
(http://docs.python.org/library/string.html#formatspec, and the format syntax
explained above it), you could do as follows, to print as float rounded to four
decimal places:
print('{0:.4f}'.format(2.4))
Or, leaving out the zero (you only need the indexes if you print things out of
order or multiple times):
print('{:.4f}'.format(2.4))
Also, I re-remembered that there is an alternative formatting method (I don't
print formatted output that often in python, I guess):
print("%.4f" % 2.4)
will do the same as the above two examples. Either way, to make things easy,
you could define your own print function to do that for you, e.g.:
def myprint(num, places=4, *args, **kargs):
fmt_str = "{:." + str(places) + "f}"
print(fmt_str.format(num), *args, **kargs)
Using it would look like (in IPython):
In [13]: myprint(2.4)
2.4000
In [14]: myprint(2.4, 5)
2.40000
You would put this in the startup script after the import line. Note that it
passes extra positional and keyword arguments to print(), so you can specify a
file to print to, for example. Also note that because of this, it won't work in
Python 2.
HTH
--
Marc Joliet
--
"People who think they know everything really annoy those of us who know we
don't" - Bjarne Stroustrup
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : suddenly Python 3 appears
2012-09-20 11:38 ` [gentoo-user] new machine : suddenly Python 3 appears Willie WY Wong
@ 2012-09-20 20:35 ` Marc Joliet
2012-09-21 6:31 ` [gentoo-user] Re: Python precision Philip Webb
1 sibling, 0 replies; 26+ messages in thread
From: Marc Joliet @ 2012-09-20 20:35 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 1973 bytes --]
Am Thu, 20 Sep 2012 13:38:45 +0200
schrieb Willie WY Wong <wongwwy@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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] new machine : Python calculator
2012-09-20 20:32 ` Marc Joliet
@ 2012-09-20 20:57 ` Marc Joliet
0 siblings, 0 replies; 26+ messages in thread
From: Marc Joliet @ 2012-09-20 20:57 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 841 bytes --]
[...]
>
> def myprint(num, places=4, *args, **kargs):
> fmt_str = "{:." + str(places) + "f}"
> print(fmt_str.format(num), *args, **kargs)
OK, quick update because I just realised how weird it is to have positional
arguments after a (potential) keyword argument (I really should go to bed).
Either of these is better:
# "places" is exclusively a keyword argument now
def myprint(num, *args, places=4, **kargs):
fmt_str = "{:." + str(places) + "f}"
print(fmt_str.format(num), *args, **kargs)
# doesn't support extra arguments to print(), but is simpler
def myprint(num, places=4):
fmt_str = "{:." + str(places) + "f}"
print(fmt_str.format(num))
--
Marc Joliet
--
"People who think they know everything really annoy those of us who know we
don't" - Bjarne Stroustrup
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* [gentoo-user] Re: Python precision
2012-09-20 11:38 ` [gentoo-user] new machine : suddenly Python 3 appears Willie WY Wong
2012-09-20 20:35 ` Marc Joliet
@ 2012-09-21 6:31 ` Philip Webb
2012-09-21 7:19 ` Marc Joliet
2012-09-21 11:02 ` Helmut Jarausch
1 sibling, 2 replies; 26+ messages in thread
From: Philip Webb @ 2012-09-21 6:31 UTC (permalink / raw
To: gentoo-user
120920 Willie WY Wong wrote:
> Unless you want to load the math module every time you start Python,
> it is perhaps better to create an alias in Bash
> using the `-i' option of Python:
> 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 math functions preloaded.
Yes thanks: the 2nd is the simplest way to do what I want.
> 120919 Marc Joliet described how to set a level of precision:
Thanks too, but that's not what I wanted: it's not for printing,
but simply to limit the display to eg 4 decimal places, not 16 ;
the calculations still wb as accurate, but the output easier to read.
Is that possible with Python ? -- ie a setting in ascript.py
to tell Python to display only the 1st 4 places in all output
without any further input from the user when doing the calculations;
presumably it wb a command s/he could enter when in interactive mode too.
--
========================,,============================================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] Re: Python precision
2012-09-21 6:31 ` [gentoo-user] Re: Python precision Philip Webb
@ 2012-09-21 7:19 ` Marc Joliet
2012-09-21 11:29 ` Philip Webb
2012-09-21 11:02 ` Helmut Jarausch
1 sibling, 1 reply; 26+ messages in thread
From: Marc Joliet @ 2012-09-21 7:19 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 1402 bytes --]
Am Fri, 21 Sep 2012 02:31:45 -0400
schrieb Philip Webb <purslow@ca.inter.net>:
[...]
> > 120919 Marc Joliet described how to set a level of precision:
>
> Thanks too, but that's not what I wanted: it's not for printing,
> but simply to limit the display to eg 4 decimal places, not 16 ;
> the calculations still wb as accurate, but the output easier to read.
> Is that possible with Python ? -- ie a setting in ascript.py
> to tell Python to display only the 1st 4 places in all output
> without any further input from the user when doing the calculations;
> presumably it wb a command s/he could enter when in interactive mode too.
I couldn't find anything for regular python on the fly (except by using the
"decimal" module, see
http://docs.python.org/library/decimal.html#module-decimal), but I found out
that IPython can do this by configuring its output formatters appropriately. To
limit to 6 decimal places, my configuration would look like this:
c.PlainTextFormatter.float_precision = '6'
I would expect that all they do is implement hook functions that you could also
put in place yourself, but to find out how to do it would require more digging
than I have time for right now. Maybe you will find something first :) .
HTH
--
Marc Joliet
--
"People who think they know everything really annoy those of us who know we
don't" - Bjarne Stroustrup
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] Re: Python precision
2012-09-21 6:31 ` [gentoo-user] Re: Python precision Philip Webb
2012-09-21 7:19 ` Marc Joliet
@ 2012-09-21 11:02 ` Helmut Jarausch
1 sibling, 0 replies; 26+ messages in thread
From: Helmut Jarausch @ 2012-09-21 11:02 UTC (permalink / raw
To: gentoo-user
On 09/21/2012 08:31:45 AM, Philip Webb wrote:
> 120920 Willie WY Wong wrote:
> > Unless you want to load the math module every time you start Python,
> > it is perhaps better to create an alias in Bash
> > using the `-i' option of Python:
> > 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 math functions
> preloaded.
>
> Yes thanks: the 2nd is the simplest way to do what I want.
>
> > 120919 Marc Joliet described how to set a level of precision:
>
> Thanks too, but that's not what I wanted: it's not for printing,
> but simply to limit the display to eg 4 decimal places, not 16 ;
> the calculations still wb as accurate, but the output easier to read.
> Is that possible with Python ? -- ie a setting in ascript.py
> to tell Python to display only the 1st 4 places in all output
> without any further input from the user when doing the calculations;
> presumably it wb a command s/he could enter when in interactive mode
> too.
>
You could subclass the builtin float class like
#!/usr/bin/python3
class myfloat(float) :
def __init__(self,value):
super().__init__(self,value)
def __str__(self):
S= super().__str__()
return S[:5]
X=3.1415926
print(myfloat(X))
Helmut.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [gentoo-user] Re: Python precision
2012-09-21 7:19 ` Marc Joliet
@ 2012-09-21 11:29 ` Philip Webb
0 siblings, 0 replies; 26+ messages in thread
From: Philip Webb @ 2012-09-21 11:29 UTC (permalink / raw
To: gentoo-user
120921 Marc Joliet wrote:
> 120921 Philip Webb <purslow@ca.inter.net> wrote:
>> Is that possible with Python ? -- ie a setting in ascript.py
>> to tell Python to display only the 1st 4 places in all output
>> without any further input from the user when doing the calculations;
>> presumably it wb a command s/he could enter when in interactive mode too.
> I couldn't find anything for regular python on the fly
> (except by using the "decimal" module, see
> http://docs.python.org/library/decimal.html#module-decimal
Yes, I found that too, but it requires typing 'Decimal' or 'D'
every time you want the shorter output (grimace).
What I would like is something which applies globally to the output,
but it looks as if there's nothing available.
I can do everything I need via the little script
-- I can predefine variables there ! --
& I don't mind loading "math" every time,
as I have no other present use for interactive Python.
I am now a bit ahead of where I was when I first asked (smile).
Thanks for this & the other reply.
--
========================,,============================================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2012-09-21 11:31 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-17 0:24 [gentoo-user] new machine : suddenly Python 3 appears Philip Webb
2012-09-17 0:46 ` Dale
2012-09-17 1:03 ` Michael Mol
2012-09-17 1:18 ` Dale
2012-09-17 1:24 ` Michael Mol
2012-09-17 1:54 ` Dale
2012-09-17 8:04 ` Neil Bothwick
2012-09-17 17:14 ` Philip Webb
2012-09-17 18:08 ` David W Noon
2012-09-17 18:45 ` Philip Webb
2012-09-17 20:17 ` Marc Joliet
2012-09-17 20:38 ` David W Noon
2012-09-17 21:08 ` Philip Webb
2012-09-17 21:17 ` Philip Webb
2012-09-17 22:27 ` David W Noon
2012-09-18 7:36 ` Marc Joliet
2012-09-19 7:08 ` Marc Joliet
2012-09-20 9:05 ` [gentoo-user] new machine : Python calculator Philip Webb
2012-09-20 20:32 ` Marc Joliet
2012-09-20 20:57 ` Marc Joliet
2012-09-20 11:38 ` [gentoo-user] new machine : suddenly Python 3 appears Willie WY Wong
2012-09-20 20:35 ` Marc Joliet
2012-09-21 6:31 ` [gentoo-user] Re: Python precision Philip Webb
2012-09-21 7:19 ` Marc Joliet
2012-09-21 11:29 ` Philip Webb
2012-09-21 11:02 ` Helmut Jarausch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox