On 2018-09-07 15:39, MRAB wrote:
> On 2018-09-07 20:51, Michael F. Stemper wrote:
>> On 2018-09-06 16:00, MRAB wrote:

>>> A word of advice: don't use a "bare" except, i.e. one that doesn't
>>> specify what exception(s) it should catch.
>>
>> Given that I moved the first line ("P_0s = ...") out of the "try"
>> clause, does this align with your advice?
>>
>>   # If pre-validation has been run, guaranteed to have RatedPower
>>   P_0s = xmlmodel.findall( 'RatedPower' )[0].text
>>   try:
>>     self.P_0 = float( P_0s )
>>   except ValueError:
>>     Utility.DataErr( "Power for %s load, '%s', not parsable" \
>>       % (id,P_0s) )
>>   if self.P_0<=0.0:
>>     Utility.DataErr( "Power for %s load must be postive, not %s" \
>>       % (id,P_0s) )
>>
> That's better.
> 
> However, if P_0s can't be parse as a float, then self.P_0 will be
> unchanged (assuming that it already has a value, of course).

This is the unique place where self.P_0 might have a value assigned.

> Is it OK that if there's no rated power, or it's invalid, it'll report
> it and continue with whatever value, if any, that it already has?

No. If the data file doesn't have the right data, the simulation can't
run. That's why DataErr reports the problem and invokes sys.exit(71).


>> In another case where I had a "bare exception", I was using it to see if
>> something was defined and substitute a default value if it wasn't. Have
>> I cleaned this up properly?
>>
>>   try
>>     id = xmlmodel.attrib['name']
>>   except KeyError:
>>     id = "constant power"
>>
>> (Both changes appear to meet my intent, I'm more wondering about how
>> pythonic they are.)
>>
> There's an alternative that's recommended when the key is often absent:
> 
>     id = xmlmodel.attrib.get('name', "constant power")

Oh, I like that much better than what I showed above, or how I "fixed"
it cross-thread. Thanks!

-- 
Michael F. Stemper
You can lead a horse to water, but you can't make him talk like Mr. Ed
by rubbing peanut butter on his gums.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to