Pickle Problem
MRAB
google at mrabarnett.plus.com
Thu Mar 5 13:02:26 EST 2009
Fab86 wrote:
> On Mar 4, 2:49 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
>> Fab86 wrote:
>>> On Mar 4, 1:40 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>>>> En Tue, 03 Mar 2009 23:11:30 -0200, Fab86 <fabien.h... at gmail.com> escribió:
>>>>> On Mar 4, 12:00 am, MRAB <goo... at mrabarnett.plus.com> wrote:
>>>>>> Fab86 wrote:
>>>>>>> On Mar 3, 8:59 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>>>>>>>> How to "spell" exactly the exception name should appear in the
>>>>>>>> documentation; might be yahoo.SearchError, or
>>>>>> yahoo.search.SearchError, or
>>>>>>>> yahoo.errors.SearchError, or similar.
>>>>>>> I have been trying except SearchError: however I get the error:
>>>>>>> Traceback (most recent call last):
>>>>>>> File "C:\Downloads\MoS\yws-2.12\Python\pYsearch-3.1\timeDelay.py",
>>>>>>> line 19, in <module>
>>>>>>> except SearchError:
>>>>>>> NameError: name 'SearchError' is not defined
>>>>>>> I have searched all documents for terms along the lines of searcherror
>>>>>>> but am finding nothing..
>>>>>> It's defined in the module you imported to get the search functionality.
>>>>> I imported:
>>>>> from yahoo.search.web import WebSearch
>>>>> However there is nothing re SearchError in that doc or in the .py.
>>>>> I can only find a reference to SearchError in the __init__ file as a
>>>>> class called SearchError
>>>> The __init__.py indicates a package
>>>> <http://docs.python.org/tutorial/modules.html#packages>
>>>> You didn't tell the package name (the name of the directory containing
>>>> __init__.py) so this is somewhat generic. If the package name is foo, use:
>>>> from foo import SearchError
>>>> If foo is a subpackage under bar, use:
>>>> from bar.foo import SearchError
>>>> It *might* be:
>>>> from yahoo.search.web import WebSearch
>>>> or perhaps:
>>>> from yahoo.search import WebSearch
>>>> You can enter those lines in the interactive interpreter to discover the
>>>> right form. (This really ought to have been documented)
>>>> --
>>>> Gabriel Genellina
>>> Ok, I managed to import the correct error class (was in a non expected
>>> place)
>>> Just as I thought I was finished, I encountered a final problem. I am
>>> running a while loop which is constantly adding search results to a
>>> file, like this (print >> f, res.total_results_available). I have put
>>> an exception in that if it times out, it simply tries again until its
>>> finished. The problem is that the loop re-write all new results
>>> continuing on from the previous searches. I would like to somehow
>>> delete all in that file and start again.
>>> I thought this could simply be achieved by putting f.close() in the
>>> exception and then it re-writes it however I am getting this error:
>>> Traceback (most recent call last):
>>> File "/home/csunix/scs5fjnh/FYProj/Python/pYsearch-3.1/test9.py",
>>> line 17, in <module>
>>> print >> f, res.total_results_available
>>> ValueError: I/O operation on closed file
>>> Is there another way rather than closing the file? Is it possible to
>>> delete all within the file?
>> You could do:
>>
>> f.seek(0)
>> f.truncate()
>>
>> I hope you don't just discard all the results you've got so far and then
>> start from the beginning again.
>
> MRAB, yes I do, thats the only way I can I get it to work. I wouldnt
> know how to make the program read the expection and then carry of
> from the last search term.
>
> Is that even possible?
>
Your code looks something like this:
srch = WebSearch(app_id=YahooKey)
for lang in langs:
srch.query = "avoir site:.%s" % lang
res = srch.parse_results()
print >> f, res.total_results_available
The exception is raise by, I think, srch.parse_results(), so you could
do something like this:
srch = WebSearch(app_id=YahooKey)
for lang in langs:
srch.query = "avoir site:.%s" % lang
# Use a loop in case we need to try again.
while True:
try:
res = srch.parse_results()
except SearchError:
# Failed. Wait a while before trying again.
time.sleep(SLEEP_IN_SECONDS)
else:
# No exception, so we have the results.
break
print >> f, res.total_results_available
I'm assuming that you can re-use the WebSearch instance for repeated
queries even if it raised the SearchError exception.
If not, just put another srch = WebSearch(app_id=YahooKey) line after
the sleep to create a new WebSearch instance.
More information about the Python-list
mailing list