codec for html/xml entities!?

Stefan Behnel stefan_ml at behnel.de
Sun Apr 20 10:20:54 EDT 2008


Martin Bless wrote:
> [Stefan Behnel] wrote & schrieb:
>>> def entity2uc(entity):
>>>     """Convert entity like { to unichr.
>>>
>>>     Return (result,True) on success or (input string, False)
>>>     otherwise. Example:
>>>         entity2cp('€')   -> (u'\u20ac',True)
>>>         entity2cp('€') -> (u'\u20ac',True)
>>>         entity2cp('€')  -> (u'\u20ac',True)
>>>         entity2cp('&foobar;') -> ('&foobar;',False)
>>>     """
>> Is there a reason why you return a tuple instead of just returning the
>> converted result and raising an exception if the conversion fails?
> 
> Mainly a matter of style. When I'll be using the function in future
> this way it's unambigously clear that there might have been
> unconverted entities. But I don't have to deal with the details of how
> this has been discovered. And may be I'd like to change the algorithm
> in future? This way it's nicely encapsulated.

The normal case is that it could be replaced, and it is an exceptional case
that it failed, in which case the caller has to deal with the problem in one
way or another. You are making the normal case more complicated, as the caller
*always* has to check the result indicator to see if the return value is the
expected result or something different. I don't think there is any reason to
require that, except when the conversion really failed.

Stefan



More information about the Python-list mailing list