Re: [Tutor] Value Error

2013-06-13 Thread Dave Angel

On 06/13/2013 04:09 AM, eryksun wrote:

On Wed, Jun 12, 2013 at 6:31 PM, Dave Angel  wrote:



i = complex(0,1)


 >>> 1j
 1j


I had forgotten that notation for a complex literal.  I knew the magic 
syntax had j in it, but didn't remember it needs to be part of the 
numeric token.  Of course the output of the debugger should have 
reminded me, but you did a better job.




http://en.wikipedia.org/wiki/Imaginary_unit#Alternative_notations



cmath.sqrt(float((math.e **(i * math.pi)).real))


The real/imag attributes are already floats:

 >>> from math import e, pi, sin, cos

 >>> cos(pi / 3), (e ** (1j * pi / 3)).real
 (0.5001, 0.5001)

 >>> sin(pi / 3), (e ** (1j * pi / 3)).imag
 (0.8660254037844386, 0.8660254037844386)



Yeah, I know.  I originally put the float in to get rid of the small bit 
of imaginary noise that the complex exponentiation created.  When that 
failed, (apparently you can't use float() to get the real portion of a 
complex value), I added the .real() and forgot to remove the float().


In case this wasn't obvious to everyone, I was just playing with the

"e to the I PI is minus one" trick, then feeding that -1 to square root.

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-13 Thread eryksun
On Wed, Jun 12, 2013 at 6:31 PM, Dave Angel  wrote:
>
 i = complex(0,1)

>>> 1j
1j

http://en.wikipedia.org/wiki/Imaginary_unit#Alternative_notations


 cmath.sqrt(float((math.e **(i * math.pi)).real))

The real/imag attributes are already floats:

>>> from math import e, pi, sin, cos

>>> cos(pi / 3), (e ** (1j * pi / 3)).real
(0.5001, 0.5001)

>>> sin(pi / 3), (e ** (1j * pi / 3)).imag
(0.8660254037844386, 0.8660254037844386)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Jim Mooney
On 12 June 2013 17:08, Marc Tompkins  wrote:

> On Wed, Jun 12, 2013 at 3:31 PM, Dave Angel  wrote:
>
>> >>> cmath.sqrt(float((math.e **(i * math.pi)).real))
>> 1j
>>
> Sh*t just got real.
>


I give up. When I got to Complex Analysis I decided to enlist instead. And
that was during Vietnam ;')

Jim
A noun is just a verb with the hiccups
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Alan Gauld

On 13/06/13 01:08, Marc Tompkins wrote:

On Wed, Jun 12, 2013 at 3:31 PM, Dave Angel mailto:da...@davea.name>> wrote:

 >>> cmath.sqrt(float((math.e **(i * math.pi)).real))
1j

Sh*t just got real.


no, it's imaginary. :-)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Marc Tompkins
On Wed, Jun 12, 2013 at 3:31 PM, Dave Angel  wrote:

> >>> cmath.sqrt(float((math.e **(i * math.pi)).real))
> 1j
>
Sh*t just got real.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Dave Angel

On 06/12/2013 06:07 PM, Jim Mooney wrote:

On 12 June 2013 14:16, Steve Willoughby  wrote:


or if you try to take the square root of a negative number, etc.



Or the log of -10. Although sqrt(-1) works fine for cmath.sqrt(-1) - I
think I get it.

Come to think of it you can do cmath.log(-10), but that's getting scary ;')

Jim


>>> import math, cmath
>>> i = complex(0,1)
>>> math.sqrt((math.e **(i * math.pi)).real)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: math domain error

>>> cmath.sqrt(float((math.e **(i * math.pi)).real))
1j



--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Jim Mooney
On 12 June 2013 14:16, Steve Willoughby  wrote:

> or if you try to take the square root of a negative number, etc.
>

Or the log of -10. Although sqrt(-1) works fine for cmath.sqrt(-1) - I
think I get it.

Come to think of it you can do cmath.log(-10), but that's getting scary ;')

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Roel Schroeven

Jim Mooney schreef:

Although I'm not sure why int('blah') wouldn't be a type error rather
than a value error.


Because passing a string to int() is perfectly okay, as long as the 
string has an appropriate value: int('42') works, int('forty two') 
raises ValueError.


--
"People almost invariably arrive at their beliefs not on the basis of
proof but on the basis of what they find attractive."
-- Pascal Blaise

r...@roelschroeven.net

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Steve Willoughby
int('blah') is not a type error because the int() function is expecting to be 
given a string, and it was given a string. The 'blah' is of the correct type.
The problem is that int() couldn't do anything useful with the value of that 
string.

Steve

On 12-Jun-2013, at 14:41, Jim Mooney  wrote:

> Dave Angel 
> 
>import math
> print math.sqrt(-1)
> 
> Ah, that's what I was looking for. I already saw it trip on type mismatches 
> like int('blah'). I was looking for what would be an actual inappropriate 
> value that was still the right type.  Although I'm not sure why int('blah') 
> wouldn't be a type error rather than a value error.
> 
> Jim 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Steve Willoughby
or if you try to take the square root of a negative number, etc.

On 12-Jun-2013, at 14:06, Sander Sweers  wrote:

> On 06/12/2013 10:49 PM, Jim Mooney wrote:
>> Raised when a built-in operation or function receives an argument that has
>> the right type but an inappropriate value, and the situation is not
>> described by a more precise exception such as
>> IndexError
> 
> You get this when the function gets the right object but the value of
> that object is not correct. For example int() will attempt to create an
> integer object from a string object. However if that string is not a
> number represented as a sting you run into a ValueError.
> 
> int('test')
> 
> Traceback (most recent call last):
>  File "", line 1, in 
>int('test')
> ValueError: invalid literal for int() with base 10: 'test'
> 
> When we give it the string representation of 123 int() will convert the
> string to an integer.
> 
> int('123')
> 123
> 
> ~Sander
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Jim Mooney
Dave Angel 

   import math

> print math.sqrt(-1)


Ah, that's what I was looking for. I already saw it trip on type mismatches
like int('blah'). I was looking for what would be an actual inappropriate
value that was still the right type.  Although I'm not sure why int('blah')
wouldn't be a type error rather than a value error.

Jim

>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Dave Angel

On 06/12/2013 04:49 PM, Jim Mooney wrote:

I'm going through the exceptions so I can recall and use the basic ones
(not much use for unicode errors at this point ;')  But I'm puzzled by an
aspect of the Value Error:
*exception *ValueError

Raised when a built-in operation or function receives an argument that has
the right type but an inappropriate value, and the situation is not
described by a more precise exception such as
IndexError
.

I know you can bring this error up easily with a wrong user input, but
within a program, what would be an example of something that is the "right
type but an inappropriate value"?



import math
print math.sqrt(-1)



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error

2013-06-12 Thread Sander Sweers
On 06/12/2013 10:49 PM, Jim Mooney wrote:
> Raised when a built-in operation or function receives an argument that has
> the right type but an inappropriate value, and the situation is not
> described by a more precise exception such as
> IndexError

You get this when the function gets the right object but the value of
that object is not correct. For example int() will attempt to create an
integer object from a string object. However if that string is not a
number represented as a sting you run into a ValueError.

int('test')

Traceback (most recent call last):
  File "", line 1, in 
int('test')
ValueError: invalid literal for int() with base 10: 'test'

When we give it the string representation of 123 int() will convert the
string to an integer.

int('123')
123

~Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Value Error

2013-06-12 Thread Jim Mooney
I'm going through the exceptions so I can recall and use the basic ones
(not much use for unicode errors at this point ;')  But I'm puzzled by an
aspect of the Value Error:
*exception *ValueError

Raised when a built-in operation or function receives an argument that has
the right type but an inappropriate value, and the situation is not
described by a more precise exception such as
IndexError
.

I know you can bring this error up easily with a wrong user input, but
within a program, what would be an example of something that is the "right
type but an inappropriate value"?
-- 
Jim
A noun is just a verb with the hiccups
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Value Error solved. Another question

2005-02-14 Thread Kent Johnson
Ron Nixon wrote:
Ignore my first posting. Here's what I'm trying to do.
I want to extract headlines from a newspaper's website
using this code. It works, but I want to match the
second group in (.*) and print
that out.
Sugguestions
import urllib, re
pattern = re.compile("""
href="(.*)">(.*)""", re.DOTALL)
page =
urllib.urlopen("http://www.startribune.com";).read()   
for headline in pattern.findall(page):
print headline
I think you want
for headline, body in pattern.findall(page):
print body
pattern.findall() returns a list of tuples of groups. You have two groups in your regex so in your 
code headline is being assigned to a tuple with two items. In my code the tuple is split and you can 
print just the second item.

PS You might want to look at BeautifulSoup:
http://www.crummy.com/software/BeautifulSoup/
Kent

	
		
__ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Value Error solved. Another question

2005-02-13 Thread Ron Nixon
Ignore my first posting. Here's what I'm trying to do.
I want to extract headlines from a newspaper's website
using this code. It works, but I want to match the
second group in (.*) and print
that out.
Sugguestions


import urllib, re
pattern = re.compile("""(.*)""", re.DOTALL)
page =
urllib.urlopen("http://www.startribune.com";).read()   
for headline in pattern.findall(page):
print headline




__ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Value Error message

2005-02-13 Thread Ron Nixon
Trying to scrape some headlines off a newspaper with
this code:

import urllib, re
pattern = re.compile("""(.*)""", re.DOTALL)
page =
urllib.urlopen("http://www.startribune.com";).read()   
for (headline, code, description) in
pattern.findall(page):
print (headline, code, description)

I'm getting the error below and can't find anything in
the documentation. Suggestions




Traceback (most recent call last):
  File "C:/Python24/Stribwebscrape.py", line 13, in ?
for (headline, code, description) in
pattern.findall(page):
ValueError: need more than 2 values to unpack



__ 
Do you Yahoo!? 
Meet the all-new My Yahoo! - Try it today! 
http://my.yahoo.com 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor