[Python-ideas] CSV api with conversion
MRAB
python at mrabarnett.plus.com
Thu Apr 15 03:45:23 CEST 2010
Bruce Leban wrote:
> Sure I could do that. I just this think if you're making a convenience
> function like this you should strive to make it more convenient. :-)
>
If you're passing in 'int' as a conversion function then it should do
what 'int' does, which is to raise an exception.
I could quote the Zen of Python:
Errors should never pass silently.
Unless explicitly silenced.
It shouldn't be so convenient that it leads to sloppy coding. :-)
> --- Bruce
> (via android)
>
>> On Apr 14, 2010 4:03 PM, "MRAB" <python at mrabarnett.plus.com
>> <mailto:python at mrabarnett.plus.com>> wrote:
>>
>> Bruce Leban wrote:
>> >
>> > If you do this, you'll probably want to support mapping empty
>> values. That is...
>>
>> The actual values read from the CSV file are strings and you're passing
>> them to functions:
>>
>> int("1"), str("")
>> int(""), str("2")
>>
>> If you want it to return a default value instead of raising an exception
>> on an empty field then you should pass a conversion function which does
>> that, for example:
>>
>> def int_or_none(field):
>> if field.strip():
>> return int(field)
>> else:
>> return None
>>
More information about the Python-ideas
mailing list