Re: [Tutor] Checking for custom error codes

2007-08-08 Thread Alan Gauld

"wormwood_3" <[EMAIL PROTECTED]> wrote

>def lookup(self):
> ...
>for potdomain in self.potdomains:
>try:
>who.whois(potdomain)
>self.availdomains.append(potdomain)
>except 'NoSuchDomain':
>pass

> This may, however, be something else wrong with my code, or the 
> rwhois module, not the try, except check.

You couldcheck that the except is working by replacing the pass
statement with a print statement. If the print statement shows
up you know the except worked so you can put the pass back in.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread Tiger12506
> This is exactly what I needed, awesome! Looks like this is what you were 
> saying to do?:
> http://docs.python.org/tut/node10.html#SECTION001050

Why ~ exactly! A link tells a thousand words. (Or maybe more.) So does that 
mean that a link is inherently more valuable than a picture? ;-)

JS 

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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread wormwood_3
> Traceback (most recent call last):
>  File "domainspotter.py", line 150, in 
>runMainParser()
>  File "domainspotter.py", line 147, in runMainParser
>td.run()
>  File "domainspotter.py", line 71, in run
>checkdomains.lookup()
>  File "domainspotter.py", line 108, in lookup
>from rwhois import WhoisRecord, NoSuchDomain
> ImportError: cannot import name NoSuchDomain
>
> Maybe I need to import something else to be able to throw it.
>
> I think if someone can explain a more general form of this I would be on 
> better footing: To use a custom error code (from a module) in a loop or 
> anywhere else, do I need to import the code itself? I had assumed that 
> once I imported the module that defined the error code, I could catch it 
> just like a core Python error code.

>>The problem is evident. The name NoSuchDomain is not defined. It is not a
>>variable. It is just a string. When you call an error like this raise
>>'NoSuchDomain', it's called something - don't know what (string
>>exception?) - but as you found out, that method of raising errors is now
>>frowned upon (deprecated). The way you are supposed to raise errors is to
>>create a new class, subclassing Exception. Then you would be able to catch
>>it by variable name as you are trying to do with the import. 

This is exactly what I needed, awesome! Looks like this is what you were saying 
to do?:
http://docs.python.org/tut/node10.html#SECTION001050

>>But you certainly can't import a constant string! It's like trying to import 
>>the
>>contents of a string variable, instead of the variable itself. Again, the
>>problem is deprecation, the rwhois will eventually have to be re-written so
>>that it uses exception classes, instead of just raising string errors.

That would seem best. I will see if they have this in the works.

Thanks again!






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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread Tiger12506
> Traceback (most recent call last):
>  File "domainspotter.py", line 150, in 
>runMainParser()
>  File "domainspotter.py", line 147, in runMainParser
>td.run()
>  File "domainspotter.py", line 71, in run
>checkdomains.lookup()
>  File "domainspotter.py", line 108, in lookup
>from rwhois import WhoisRecord, NoSuchDomain
> ImportError: cannot import name NoSuchDomain
>
> Maybe I need to import something else to be able to throw it.
>
> I think if someone can explain a more general form of this I would be on 
> better footing: To use a custom error code (from a module) in a loop or 
> anywhere else, do I need to import the code itself? I had assumed that 
> once I imported the module that defined the error code, I could catch it 
> just like a core Python error code.


The problem is evident. The name NoSuchDomain is not defined. It is not a 
variable. It is just a string. When you call an error like this raise 
'NoSuchDomain', it's called something - don't know what (string 
exception?) - but as you found out, that method of raising errors is now 
frowned upon (deprecated). The way you are supposed to raise errors is to 
create a new class, subclassing Exception. Then you would be able to catch 
it by variable name as you are trying to do with the import. But you 
certainly can't import a constant string! It's like trying to import the 
contents of a string variable, instead of the variable itself. Again, the 
problem is deprecation, the rwhois will eventually have to be re-written so 
that it uses exception classes, instead of just raising string errors.

JS 

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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread wormwood_3
>>Probably you need to import NoSuchDomain from rwhois:
>>from rwhois import WhoisRecord, NoSuchDomain
>>
>>then use
>>   except NoSuchDomain:

I tried adding:
from rwhois import WhoisRecord, NoSuchDomain
who = WhoisRecord()
self.totalchecked = 0
self.availdomains = []
for potdomain in self.potdomains:
try: 
who.whois(potdomain)
self.availdomains.append(potdomain)
except NoSuchDomain:
pass
self.totalchecked+=1

But I get back:

Traceback (most recent call last):
  File "domainspotter.py", line 150, in 
runMainParser()
  File "domainspotter.py", line 147, in runMainParser
td.run()
  File "domainspotter.py", line 71, in run
checkdomains.lookup()
  File "domainspotter.py", line 108, in lookup
from rwhois import WhoisRecord, NoSuchDomain
ImportError: cannot import name NoSuchDomain

Maybe I need to import something else to be able to throw it. 

I think if someone can explain a more general form of this I would be on better 
footing: To use a custom error code (from a module) in a loop or anywhere else, 
do I need to import the code itself? I had assumed that once I imported the 
module that defined the error code, I could catch it just like a core Python 
error code.

>>In general, when you ask a question here, "I tried X and it did not 
>>work" is not very informative and makes it difficult to give a good 
>>answer. It is very helpful to show the actual code you tried and the 
>>actual error message, including the full traceback. The error message 
>>and traceback include a lot of very helpful information; including them 
>>will greatly improve your chance of a correct answer.

Commented on this with the warning text in last response. Will endeavour to do 
better:-)

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



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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread wormwood_3
>>Examining rwhois.py reveals

>>raise 'NoSuchDomain'

>>>which is a string exception. Which should work even tho deprecated.

>>>When you say it did not work what is the evidence?


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC
___
I did speak too soon on this. When I run the script using the code at the end, 
I get:

rwhois.py:327: DeprecationWarning: raising a string exception is deprecated
  raise 'NoSuchDomain', self.domain

===
def lookup(self):
"""
Looks up each word in our list with whois
"""
"""
Using rwhois, which is either broken or lacks most data:
"""
from rwhois import WhoisRecord
who = WhoisRecord()
self.totalchecked = 0
self.availdomains = []
for potdomain in self.potdomains:
try: 
who.whois(potdomain)
self.availdomains.append(potdomain)
except 'NoSuchDomain':
pass
self.totalchecked+=1
def printresults(self):
"""
Prints the ones that do not seem to be taken, with some stats.
"""
print "Results are in! "
print "%s total words checked, as .com and .net domains." % 
(self.totalchecked / 2)
print "--"
print "Domains that seem to be available: \n"
for availdomain in self.availdomains:
print availdomain 
print "\n"
print "--"


Then, I get back items in the list such as:

Domains that seem to be available: 

AA.com
AARP.com
AARP.net

I confirmed in spotchecking that some of these are taken, such as with whois -H 
AARP.com:
   Domain Name: AARP.COM
   Registrar: NETWORK SOLUTIONS, LLC.
   Whois Server: whois.networksolutions.com

This may, however, be something else wrong with my code, or the rwhois module, 
not the try, except check.




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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread Alan Gauld
"wormwood_3" <[EMAIL PROTECTED]> wrote

> Recently I was trying to do a "try: X except Y: Z" statement, 
> checking for a custom error code

>for potdomain in self.potdomains:
>try:
>who.whois(potdomain)
>self.availdomains.append(potdomain)
>except 'NoSuchDomain':
>pass
>self.totalchecked+=1
>
> got a warning that throwing a string exception is deprecated,
> but the check did not work anyway.

So what did happen? Your code simply ignores the error so, for it not
to work, I assume you are still getting an error report and traceback?
If so what does it say?

Python error messages may look arcane to start with but they usually
contain enough information to identify a problem without any further
debugging - but only if we can see them! :-)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread wormwood_3
>>Probably you need to import NoSuchDomain from rwhois:
>>from rwhois import WhoisRecord, NoSuchDomain
>>
>>then use
>>   except NoSuchDomain:

That sounds right, I will give it a shot soon.

>>In general, when you ask a question here, "I tried X and it did not 
>>work" is not very informative and makes it difficult to give a good 
>>answer. It is very helpful to show the actual code you tried and the 
>>actual error message, including the full traceback. The error message 
>>and traceback include a lot of very helpful information; including them 
>>will greatly improve your chance of a correct answer.

Sorry about that, I knew the code would be helpful, that's why I linked to my 
blog post that included the full code. 

I will post the traceback tonight when I can run it.



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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread Bob Gailer
wormwood_3 wrote:
> Hi all,
>
> I am new to Python programming and this list, looks like a great place so far!
>
> Recently I was trying to do a "try: X except Y: Z" statement, checking for a 
> custom error code that the rwhois.py module throws. Some details on the 
> exercise and the full code can be found on this post ( 
> http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html
>  ). So, here is what I tried:
>
> for potdomain in self.potdomains:
> try: 
> who.whois(potdomain)
> self.availdomains.append(potdomain)
> except 'NoSuchDomain':
> pass
> self.totalchecked+=1
>
> If you need more context, the whole program is here ( 
> http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html#code
>  ). I first tried simply "except NoSuchDomain", since that was what I saw 
> rwhois throw when I sent it a domain that was not registered. That did not 
> work, so I quoted it. I got a warning that throwing a string exception is 
> deprecated, but the check did not work anyway. Am I trying to check for this 
> custom error wrongly? How should it be done?
>   
Examining rwhois.py reveals

raise 'NoSuchDomain'

which is a string exception. Which should work even tho deprecated.

When you say it did not work what is the evidence?
>
>   


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


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


Re: [Tutor] Checking for custom error codes

2007-08-07 Thread Kent Johnson
wormwood_3 wrote:
> Hi all,
> 
> I am new to Python programming and this list, looks like a great place so far!
> 
> Recently I was trying to do a "try: X except Y: Z" statement, checking for a 
> custom error code that the rwhois.py module throws. Some details on the 
> exercise and the full code can be found on this post ( 
> http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html
>  ). So, here is what I tried:
> 
> for potdomain in self.potdomains:
> try: 
> who.whois(potdomain)
> self.availdomains.append(potdomain)
> except 'NoSuchDomain':
> pass
> self.totalchecked+=1
> 
> If you need more context, the whole program is here (
http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html#code
). I first tried simply "except NoSuchDomain", since that was what I saw
rwhois throw when I sent it a domain that was not registered. That did
not work, so I quoted it. I got a warning that throwing a string
exception is deprecated, but the check did not work anyway. Am I trying
to check for this custom error wrongly? How should it be done?

Probably you need to import NoSuchDomain from rwhois:
from rwhois import WhoisRecord, NoSuchDomain

then use
   except NoSuchDomain:

In general, when you ask a question here, "I tried X and it did not 
work" is not very informative and makes it difficult to give a good 
answer. It is very helpful to show the actual code you tried and the 
actual error message, including the full traceback. The error message 
and traceback include a lot of very helpful information; including them 
will greatly improve your chance of a correct answer.

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


[Tutor] Checking for custom error codes

2007-08-07 Thread wormwood_3
Hi all,

I am new to Python programming and this list, looks like a great place so far!

Recently I was trying to do a "try: X except Y: Z" statement, checking for a 
custom error code that the rwhois.py module throws. Some details on the 
exercise and the full code can be found on this post ( 
http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html 
). So, here is what I tried:

for potdomain in self.potdomains:
try: 
who.whois(potdomain)
self.availdomains.append(potdomain)
except 'NoSuchDomain':
pass
self.totalchecked+=1

If you need more context, the whole program is here ( 
http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html#code
 ). I first tried simply "except NoSuchDomain", since that was what I saw 
rwhois throw when I sent it a domain that was not registered. That did not 
work, so I quoted it. I got a warning that throwing a string exception is 
deprecated, but the check did not work anyway. Am I trying to check for this 
custom error wrongly? How should it be done?

Thanks for any assistance!

-Sam





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


[Tutor] Checking for custom error codes

2007-08-07 Thread wormwood_3
Hi all,

I am new to Python programming and this list, looks like a great place so far!

Recently I was trying to do a "try: X except Y: Z" statement, checking for a 
custom error code that the rwhois.py module throws. Some details on the 
exercise and the full code can be found on this post ( 
http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html 
). So, here is what I tried:

for potdomain in self.potdomains:
try: 
who.whois(potdomain)
self.availdomains.append(potdomain)
except 'NoSuchDomain':
pass
self.totalchecked+=1
If you need more context, the whole program is here ( 
http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html#code
 ). I first tried simply "except NoSuchDomain", since that was what I saw 
rwhois throw when I sent it a domain that was not registered. That did not 
work, so I quoted it. I got a warning that throwing a string exception is 
deprecated, but the check did not work anyway. Am I trying to check for this 
custom error wrongly? How should it be done?

Thanks for any assistance!

-Sam


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