[SciPy-user] fromfile, item, what other output than stdout / stderr is used ?

Robert Kern robert.kern@gmail....
Mon May 11 15:07:25 CDT 2009

On Mon, May 11, 2009 at 14:57, Christopher Barker <Chris.Barker@noaa.gov> wrote:
> Robert Kern wrote:
>> There is a long history of returning what bytes you can without
>> raising an error. This helps a lot when writing code that reads a
>> chunk at a time. E.g. file.read(nbytes) will return nbytes or fewer if
>> you get within nbytes of the end of the file.
> maybe so, but fromfile() already supports "read 'till the end of the
> file", if you don't know how many items you have.

But that doesn't let you express, "read at most this number of items,"
which is really useful and is very common in file-reading APIs.

> maybe a flag? I'm still convinced that we're setting up the users for
> bugs if they get fewer items than expected and don't get an exception. I
> see fromfile() as fundamentally higher level than file.read().

Just because it is "higher level" in one respect doesn't mean that it
applies any or all particular "higher level" semantics you might want.
In fact, I would suggest the opposite, that it should do precisely
*one* thing higher level, which is to deal with dtypes.

If it makes you feel better, you may consider the warnings mechanism
to be just such a flag, only it uses a Python-standard mechanism for
controlling such behavior.

>> I suggest using the warnings mechanism. This lets you either silence
>> the warning or turn it into an exception depending on your use case.
> The other question is what gets returned, I'm pretty sure that when you
> call fromfile() with a count, it pre-allocates the array, then fills it,
> so if you don't have enough items in your file, you'll get an array that
> is the size you expect, but with partially junk in it -- another way to
> ask for bugs.

Currently, it just returns you an array sized for the bytes it could
read; no junk. It's really easy to try it out instead of guessing.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the SciPy-user mailing list