python - Django: Class to log a mail error -
i'm trying follow code django unleashed , (by mean often) have problems understand code in full. here exaple of function suppose log errors when mail not sent. part of login system.
def log_mail_error(self, **kwargs): msg_list = [ 'activation email did not send.\n', 'from_email: {from_email}\n' 'subject: {subject}\n' 'message: {message}\n', ] recipient_list = kwargs.get( 'recipient_list', []) recipient in recipient_list: msg_list.insert( 1, 'recipient: {r}\n'.format( r=recipient)) if 'error' in kwargs: level = error error_msg = ( 'error: {0.__class__.__name__}\n' 'args: {0.args}\n') error_info = error_msg.format( kwargs['error']) msg_list.insert(1, error_info) else: level = critical msg = ''.join(msg_list).format(**kwargs) logger.log(level, msg)
i have hard time 1 part:
error_msg = ( 'error: {0.__class__.__name__}\n' 'args: {0.args}\n') error_info = error_msg.format( kwargs['error'])
i don't see 0.class.name comes from. 0 suppose mean? there no object or class called 0 in project. dont understand formatting of error_msg. potentially have 2 fields {} {} fit "kwargs['error']" have 1 value fit 2 places string formatting. agian, why pain {0.__class [...]} if meant placeholder? there going on dont quite understand. can help?
if read format mini language spec you'll notice formatting string can allow passing order of received parameters. example
'{0} , {1}'.format('foo', 'bar') # outputs foo , bar '{1} , {0}'.format('foo', 'bar') # outputs bar , foo
this means code specifying parameter used expand format expression should first 1 passed
since actual error
object not defined in code, can assume it's of type has args
property attached it. in other words, string outputs type of error , parameters built with
wiki
Comments
Post a Comment