[Numpy-discussion] loadtxt broken if file does not end in newline

Robert Kern robert.kern@gmail....
Wed Feb 27 18:58:16 CST 2008


On Wed, Feb 27, 2008 at 6:31 PM, Christopher Barker
<Chris.Barker@noaa.gov> wrote:
> Robert Kern wrote:
>  > Fixed in r4827.
>
>  Thanks Robert. For the record, this is the fixed version:
>
>         comment_start = line.find(comments)
>          if comment_start > 0:
>              line = line[:comments_start].strip()
>          else:
>              line = line.strip()
>
>  Just as a matter of interest, why this, rather than line.index()? Are
>  exceptions slower than an if test?

Yes.

>  Also,
>
>  I don't see any io tests in:
>
>  numpy/lib/tests
>
>  Is that where they should be? It seems like a good idea to have a few...

Yes.

>  If I did find the time to write some tests -- how does one go about it
>  for this sort of thing? Do I put a couple sample input files in SVN? Or
>  does the test code write out the sample files, then read them in to
>  test? Or maybe do it all in memory with sStringIO or something.

Any of the above depending on the situation. Use cStringIO if you can.
Put files into numpy/lib/tests/data/ otherwise. Locate them using
os.path.join(os.path.dirname(__file__), 'data', 'mytestfile.dat').
Write things out at runtime *only* if you use tempfile correctly and
are sure you clean up properly after yourself whether the test passes
or fails.

>  Are
>  there any examples of tests of file reading code that I could borrow from?

numpy/lib/tests/test_format.py

Unfortunately, they have been written for nose, which we haven't moved
to, yet, for numpy itself.

-- 
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 Numpy-discussion mailing list