FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 09-17-2012, 08:17 PM
Marc Joliet
 
Default new machine : suddenly Python 3 appears

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
 
Old 09-17-2012, 08:38 PM
David W Noon
 
Default 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]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 
Old 09-17-2012, 09:08 PM
Philip Webb
 
Default 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
 
Old 09-17-2012, 09:17 PM
Philip Webb
 
Default 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
 
Old 09-17-2012, 10:27 PM
David W Noon
 
Default 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]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 
Old 09-18-2012, 07:36 AM
Marc Joliet
 
Default new machine : suddenly Python 3 appears

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
 
Old 09-19-2012, 07:08 AM
Marc Joliet
 
Default new machine : suddenly Python 3 appears

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
 
Old 09-20-2012, 11:38 AM
Willie WY Wong
 
Default 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
 
Old 09-20-2012, 08:35 PM
Marc Joliet
 
Default new machine : suddenly Python 3 appears

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
 

Thread Tools




All times are GMT. The time now is 11:30 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org