|
From: Michael F. <fuz...@vo...> - 2006-02-02 22:55:18
|
Nicola Larosa wrote:
>>> My preference would be the ability to specify a file object to
>>> cfg_obj.write().
>>>
>
>
>> So you'd like to be able to pass a file like object to ``write()``...
>> hmmm...
>>
>> Again, this is something that you used to be able to do with ConfigObj
>> 3, but I removed to reduce the level of complexity.
>>
>> To be symmetric with initialisation, you ought then be able to pass in a
>> filename as well. Seeing as you can already achieve this just be setting
>> the ``filename`` attribute (which ok sounds a little weird for this
>> purpose), I decided not to do it.
>>
>> Hmmm... I guess it wouldn't hurt being able to pass an optional file
>> object to write, it's just that the API keeps growing...
>>
>> Nicola, do you have any take on this ?
>>
>
> Well, it actually feels a little awkward that ConfigObj is in the business
> of opening files, and having to store the filename in an attribute. It
> seems best that ``write`` just receives a file-like object, and be done
> with it.
>
> This seems a worthy addition to me. At this point you probably have to keep
> the filename attribute for compatibility anyway, but maybe you could even
> find a way to deprecate it? :-)
>
I realise that in Python it is more common to pass around file like
objects than filenames. I guess this is more object oriented because of
the "like" bit. However it has always felt more awkward to me, because
>90% of the time I use "file-like" objects, they are file objects.
Invariably when I write a config file, it is to the same one that was
opened. My current idiom (based on having a hardwired config filename,
or retrieving from the user) is :
retrieve filename
instantiate ConfigObj
..
do stuff
..
write
The alternative becomes :
get filename
open file
instantiate ConfigObj (whilst keeping a separate reference to the
filename)
close file
..
do stuff
..
get filename again
open file
write
close file
It just seems a lot more awkward. ConfigObj *already* supports passing
it an open file on instantiation. I guess passing an open file to
``write`` is a logical extension of this, but I won't deprecate
``filename``.
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
|