Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108934 > unrolled thread

what is new with int conversion in Python 3

Started bySayth Renshaw <flebber.crue@gmail.com>
First post2016-05-22 00:26 -0700
Last post2016-05-22 03:59 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  what is new with int conversion in Python 3 Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-22 00:26 -0700
    Re: what is new with int conversion in Python 3 Chris Angelico <rosuav@gmail.com> - 2016-05-22 17:51 +1000
    Re: what is new with int conversion in Python 3 Steven D'Aprano <steve@pearwood.info> - 2016-05-22 17:55 +1000
    Re: what is new with int conversion in Python 3 Peter Otten <__peter__@web.de> - 2016-05-22 10:32 +0200
    Re: what is new with int conversion in Python 3 Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-22 03:59 -0700

#108934 — what is new with int conversion in Python 3

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-22 00:26 -0700
Subjectwhat is new with int conversion in Python 3
Message-ID<648b46dc-c962-4f95-83bc-faa65ae3c51a@googlegroups.com>
I am doing a passage in a book that was written for python 2 i am writing everything in 3.

This is the author Ivan Idris code to show time difference between python and numpy arrays. The only edit I made was to fix the print statements.

#!/usr/bin/env/python

import sys
from datetime import datetime
import numpy as np

"""
 This program demonstrates vector addition the Python way.
 Run from the command line as follows

  python vectorsum.py n

 where n is an integer that specifies the size of the vectors.

 The first vector to be added contains the squares of 0 up to n.
 The second vector contains the cubes of 0 up to n.
 The program prints the last 2 elements of the sum and the elapsed time.
"""

def numpysum(n):
   a = np.arange(n) ** 2
   b = np.arange(n) ** 3
   c = a + b

   return c

def pythonsum(n):
   a = range(n)
   b = range(n)
   c = []

   for i in range(len(a)):
       a[i] = i ** 2
       b[i] = i ** 3
       c.append(a[i] + b[i])

   return c

size = int(sys.argv[1])

start = datetime.now()
c = pythonsum(size)
delta = datetime.now() - start
print("The last 2 elements of the sum", c[-2:])
print("PythonSum elapsed time in microseconds", delta.microseconds)

start = datetime.now()
c = numpysum(size)
delta = datetime.now() - start
print("The last 2 elements of the sum", c[-2:])
print("NumPySum elapsed time in microseconds", delta.microseconds)


However when I run this I get a valuerror. So either something has changed with int or datetime I cannot google a consistent answer.


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-a54a878f293d> in <module>()
     37    return c
     38 
---> 39 size = int(sys.argv[1])
     40 
     41 start = datetime.now()

ValueError: invalid literal for int() with base 10: '-f'

Had this before?

Sayth

[toc] | [next] | [standalone]


#108935

FromChris Angelico <rosuav@gmail.com>
Date2016-05-22 17:51 +1000
Message-ID<mailman.91.1463903520.27390.python-list@python.org>
In reply to#108934
On Sun, May 22, 2016 at 5:26 PM, Sayth Renshaw <flebber.crue@gmail.com> wrote:
> However when I run this I get a valuerror. So either something has changed with int or datetime I cannot google a consistent answer.
>
>
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
> <ipython-input-8-a54a878f293d> in <module>()
>      37    return c
>      38
> ---> 39 size = int(sys.argv[1])
>      40
>      41 start = datetime.now()
>
> ValueError: invalid literal for int() with base 10: '-f'
>
> Had this before?

Look carefully at the error, and the line that it's coming up on.
Firstly, you can rule out datetime, as nothing has been done with
datetime except import it. Secondly, the invalid literal doesn't look
like a decimal number at all; in fact, it looks to me like a flag of
some sort. Try adding this above the failing line:

print(sys.argv)

And also, try running this at the terminal:

$ file /usr/bin/env/python

How are you invoking Python? The shebang looks wrong; perhaps it
should be "/usr/bin/env python" (note the space where you have an
additional slash), but perhaps it's not even being significant here.

ChrisA

[toc] | [prev] | [next] | [standalone]


#108936

FromSteven D'Aprano <steve@pearwood.info>
Date2016-05-22 17:55 +1000
Message-ID<574165f0$0$1609$c3e8da3$5496439d@news.astraweb.com>
In reply to#108934
On Sun, 22 May 2016 05:26 pm, Sayth Renshaw wrote:

> I am doing a passage in a book that was written for python 2 i am writing
> everything in 3.
[...] 
> However when I run this I get a valuerror. So either something has changed
> with int or datetime I cannot google a consistent answer.

Neither. It will help if you read the error message:


> ValueError: invalid literal for int() with base 10: '-f'

You're passing -f as the first argument to the script, instead of an
integer. Look at the command you are typing. My guess is that you are
typing something like

python vectorsum.py -f 27

instead of 

python vectorsum.py 27


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#108937

FromPeter Otten <__peter__@web.de>
Date2016-05-22 10:32 +0200
Message-ID<mailman.92.1463905981.27390.python-list@python.org>
In reply to#108934
Sayth Renshaw wrote:

Read carefully:

>  Run from the command line as follows
> 
>   python vectorsum.py n
> 
>  where n is an integer that specifies the size of the vectors.

So to run the script with Python 3 you could do

$ python3 vectorsum.py 42

in the shell. This implicitly sets sys.argv[1] to "42" so that the 
conversion

size = int(sys.argv[1]) 

can succeed. This conversion failed because as the traceback indicates

> ValueError                                Traceback (most recent call 
last)
> <ipython-input-8-a54a878f293d> in <module>()
>      37    return c
>      38 
> ---> 39 size = int(sys.argv[1])
>      40 
>      41 start = datetime.now()
> 
> ValueError: invalid literal for int() with base 10: '-f'
> 

sys.argv[1] is "-f" which is not a valid (base-10) integer. This value 
probably got into ipython3 because you invoked it with something like

$ ipython3 console -f foo
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import vectorsum
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-b0b6e1aba9f9> in <module>()
----> 1 import vectorsum

/home/petto/vectorsum.py in <module>()
     37    return c
     38 
---> 39 size = int(sys.argv[1])
     40 
     41 start = datetime.now()

ValueError: invalid literal for int() with base 10: '-f'

In [2]: ! python3 vectorsum.py 42
The last 2 elements of the sum [65600, 70602]
PythonSum elapsed time in microseconds 106
The last 2 elements of the sum [65600 70602]
NumPySum elapsed time in microseconds 121

By the way, I had to fix another problem to make the "In [2]:" invocation 
work. Let's see if you can do that yourself.

[toc] | [prev] | [next] | [standalone]


#108939

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-22 03:59 -0700
Message-ID<b52bc2ec-4305-4f67-a372-844f65babfa2@googlegroups.com>
In reply to#108934
On Sunday, 22 May 2016 17:26:51 UTC+10, Sayth Renshaw  wrote:
> I am doing a passage in a book that was written for python 2 i am writing everything in 3.
> 
> This is the author Ivan Idris code to show time difference between python and numpy arrays. The only edit I made was to fix the print statements.
> 
> #!/usr/bin/env/python
> 
> import sys
> from datetime import datetime
> import numpy as np
> 
> """
>  This program demonstrates vector addition the Python way.
>  Run from the command line as follows
> 
>   python vectorsum.py n
> 
>  where n is an integer that specifies the size of the vectors.
> 
>  The first vector to be added contains the squares of 0 up to n.
>  The second vector contains the cubes of 0 up to n.
>  The program prints the last 2 elements of the sum and the elapsed time.
> """
> 
> def numpysum(n):
>    a = np.arange(n) ** 2
>    b = np.arange(n) ** 3
>    c = a + b
> 
>    return c
> 
> def pythonsum(n):
>    a = range(n)
>    b = range(n)
>    c = []
> 
>    for i in range(len(a)):
>        a[i] = i ** 2
>        b[i] = i ** 3
>        c.append(a[i] + b[i])
> 
>    return c
> 
> size = int(sys.argv[1])
> 
> start = datetime.now()
> c = pythonsum(size)
> delta = datetime.now() - start
> print("The last 2 elements of the sum", c[-2:])
> print("PythonSum elapsed time in microseconds", delta.microseconds)
> 
> start = datetime.now()
> c = numpysum(size)
> delta = datetime.now() - start
> print("The last 2 elements of the sum", c[-2:])
> print("NumPySum elapsed time in microseconds", delta.microseconds)
> 
> 
> However when I run this I get a valuerror. So either something has changed with int or datetime I cannot google a consistent answer.
> 
> 
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
> <ipython-input-8-a54a878f293d> in <module>()
>      37    return c
>      38 
> ---> 39 size = int(sys.argv[1])
>      40 
>      41 start = datetime.now()
> 
> ValueError: invalid literal for int() with base 10: '-f'
> 
> Had this before?
> 
> Sayth

Thank you all, I was way wrong. I was invoking it from within ipython notebook which I don't usually use.

Thanks'

Sayth

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web