Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Repeat an exception

0 views
Skip to first unread message

mf

unread,
Feb 3, 2010, 9:33:33 PM2/3/10
to
I'm translating a db from english to spanish with the Google
translator API. The problem is when a TranslationError occurs(usually
because of connection problems). I can
except the first one, but I don't know how to except again. I "solved"
the problem by saving temp db's and then joining them, but it must be
a pythonic way to do it.

Here's a snippet from the code:

english_field = oldDb[i].fieldData[0] #oldDb is a db filled
with english words
try:
spanish_field = translate(english_field, lang_to='es',
lang_from='en')
except TranslationError:
spanish_field = translate(english_field, lang_to='es',
lang_from='en')

#Then I save the fields into a new db

Alf P. Steinbach

unread,
Feb 3, 2010, 10:00:45 PM2/3/10
to
* mf:

> I'm translating a db from english to spanish with the Google
> translator API. The problem is when a TranslationError occurs(usually
> because of connection problems). I can
> except the first one, but I don't know how to except again. I "solved"
> the problem by saving temp db's and then joining them, but it must be
> a pythonic way to do it.
>
> Here's a snippet from the code:
>
> english_field = oldDb[i].fieldData[0] #oldDb is a db filled
> with english words

Is this actual code? I may be mistaken, but at least in Python 3.x it seems
there should be a comma between each 'with' expression. And there should
certainly be a colon at the end.

It's best to copy and paste real code.

Made-up code can just be misleading for those who'd like to help.


> try:
> spanish_field = translate(english_field, lang_to='es',
> lang_from='en')
> except TranslationError:
> spanish_field = translate(english_field, lang_to='es',
> lang_from='en')
>
> #Then I save the fields into a new db

Possibly I'm misunderstanding what you mean, but have you thought of using a loop?


Cheers & hth.,

- Alf

MRAB

unread,
Feb 3, 2010, 10:17:43 PM2/3/10
to pytho...@python.org
Alf P. Steinbach wrote:
> * mf:
>> I'm translating a db from english to spanish with the Google
>> translator API. The problem is when a TranslationError occurs(usually
>> because of connection problems). I can
>> except the first one, but I don't know how to except again. I "solved"
>> the problem by saving temp db's and then joining them, but it must be
>> a pythonic way to do it.
>>
>> Here's a snippet from the code:
>>
>> english_field = oldDb[i].fieldData[0] #oldDb is a db filled
>> with english words
>
> Is this actual code? I may be mistaken, but at least in Python 3.x it
> seems there should be a comma between each 'with' expression. And there
> should certainly be a colon at the end.
>
> It's best to copy and paste real code.
>
> Made-up code can just be misleading for those who'd like to help.
>
It looks to me like the line wrapped during posting, ie the 'with' part
belongs to the comment.

>
>> try:
>> spanish_field = translate(english_field, lang_to='es',
>> lang_from='en')
>> except TranslationError:
>> spanish_field = translate(english_field, lang_to='es',
>> lang_from='en')
>>
>> #Then I save the fields into a new db
>
> Possibly I'm misunderstanding what you mean, but have you thought of
> using a loop?
>

In other words:

for attempt in range(2):


try:
spanish_field = translate(english_field, lang_to='es',
lang_from='en')

break
except TranslationError:
pass
else:
# Didn't break out of the loop, therefore not successful.
print "Translation failed"

Alf P. Steinbach

unread,
Feb 3, 2010, 10:44:00 PM2/3/10
to
* MRAB:

> Alf P. Steinbach wrote:
>> * mf:
>>> I'm translating a db from english to spanish with the Google
>>> translator API. The problem is when a TranslationError occurs(usually
>>> because of connection problems). I can
>>> except the first one, but I don't know how to except again. I "solved"
>>> the problem by saving temp db's and then joining them, but it must be
>>> a pythonic way to do it.
>>>
>>> Here's a snippet from the code:
>>>
>>> english_field = oldDb[i].fieldData[0] #oldDb is a db filled
>>> with english words
>>
>> Is this actual code? I may be mistaken, but at least in Python 3.x it
>> seems there should be a comma between each 'with' expression. And
>> there should certainly be a colon at the end.
>>
>> It's best to copy and paste real code.
>>
>> Made-up code can just be misleading for those who'd like to help.
>>
> It looks to me like the line wrapped during posting, ie the 'with' part
> belongs to the comment.

*banging me head*

thx

What one gets from conditioning oneself to ignore comments in code.


>>> try:
>>> spanish_field = translate(english_field, lang_to='es',
>>> lang_from='en')
>>> except TranslationError:
>>> spanish_field = translate(english_field, lang_to='es',
>>> lang_from='en')
>>>
>>> #Then I save the fields into a new db
>>
>> Possibly I'm misunderstanding what you mean, but have you thought of
>> using a loop?
>>
> In other words:
>
> for attempt in range(2):
> try:
> spanish_field = translate(english_field, lang_to='es',
> lang_from='en')
> break
> except TranslationError:
> pass
> else:
> # Didn't break out of the loop, therefore not successful.
> print "Translation failed"

Cheers,

- Alf

Jean-Michel Pichavant

unread,
Feb 4, 2010, 7:59:52 AM2/4/10
to MRAB, pytho...@python.org
MRAB wrote:
> In other words:
>
> for attempt in range(2):
> try:
> spanish_field = translate(english_field, lang_to='es',
> lang_from='en')
> break
> except TranslationError:
> pass
> else:
> # Didn't break out of the loop, therefore not successful.
> print "Translation failed"

What the hell is this 'for else' loop !! :D First time I see this
statement for years.
I'd never thought I'd still learn something that basic.

My first impression is that the mechansim is not that obvious. MRAB's
need for a comment tends to confirm this.
I'll try to remember that anyway. Nice addition.

JM


MRAB

unread,
Feb 4, 2010, 11:36:09 AM2/4/10
to pytho...@python.org
Jean-Michel Pichavant wrote:
> MRAB wrote:
>> In other words:
>>
>> for attempt in range(2):
>> try:
>> spanish_field = translate(english_field, lang_to='es',
>> lang_from='en')
>> break
>> except TranslationError:
>> pass
>> else:
>> # Didn't break out of the loop, therefore not successful.
>> print "Translation failed"
>
> What the hell is this 'for else' loop !! :D First time I see this
> statement for years.
> I'd never thought I'd still learn something that basic.
>
> My first impression is that the mechansim is not that obvious. MRAB's
> need for a comment tends to confirm this.
> I'll try to remember that anyway. Nice addition.
>
The comment is there for the OP. _I_ don't need it! :-)
0 new messages