[Tutor] Program review

Ricardo Aráoz ricaraoz at gmail.com
Sun Jan 6 12:28:42 CET 2008


Jeff Younker wrote:
> The procesar function is complicated.   It tries to do several things
> that that are better done elsewhere.  It needs to be broken up into
> several functions and methods.   This also clarifies and isolates the
> exception handling.
> 
>> def procesar(mensaje):
>>    try :
> The enclosing try block isn't needed.  More on this later.
>>
>>        try :
>>            fIncl = open(mensaje.direcciones)
>>        except Exception, e :
>>            logging.error('Error!!! No pude abrir "%s" : %s',
>>                            mensaje.direcciones,
>>                            e.strerror)
>>            raise
> 
> This is one function.  It has the argument filename.
>>
>>        try :
>>            fExcl = open(mensaje.excluidas)
>>        except Exception, e :
>>            logging.error('No pude abrir "%s" : %s',
>>                            mensaje.excluidas,
>>                            e.strerror)
>>            fIncl.close()
>>            raise
> 
> This is the same function with a different argument.
> 
>>
>>    except : pass
>>    else :
> 
> The outer exception block here does nothing.   If an exception
> is raised then the following code won't be executed anyway.
> 
>>        mails = enumerate(
>>                        set(addr.strip() for addr in fIncl)
>>                        - set(excl.strip() for excl in fExcl))
>>        fIncl.close()
>>        fExcl.close()
> 

Maybe it is my poor understanding of exception handling. My intention
here is to open the first file, if error then report in logging and
finish normally, else open the 2nd file, if error then report in logging
close the 1st file and finish normally. If no error then process.



More information about the Tutor mailing list