SINTEF / Splipy

Spline modelling made easy.
GNU General Public License v3.0
102 stars 18 forks source link

Wierd behaviour when writing (g2) files that already exist #10

Closed VikingScientist closed 7 years ago

VikingScientist commented 7 years ago

Writing to a file that already exist will cause it to starting from the top to swap out previous lines with the new ones. At the end of writing, any trailing lines present in the previous file, but so far down that they were not overwritten, will be kept unchanged. This renders the result file unusable

from splipy import *                                                           
from splipy.IO import *                                                        

crv  = Curve()                                                                 
crv2 = crv + [2,0]                                                             
crv.refine(1)                                                                  

with G2('test.g2') as f:                                                       
  f.write(crv) # creates a valid 7-line file                                   

with G2('test.g2') as f:                                                       
  f.write(crv2) # should create a 6-line file, but the 7th line from above     
                # is kept unchanged from the previous write-operation
akva2 commented 7 years ago

i've had much fun with this one ;) the problem is wrong open mode in the G2 class from here https://github.com/sintefmath/Splipy/blob/master/splipy/IO/g2.py#L18

VikingScientist commented 7 years ago

I guess this makes sense. The reason for writing it the way it currently is, is to allow for read/write operations to take on a similar structure

with G2('test.g2') as f:
  my_spline_objects = f.read()

should return the objects in the file. This causes some problem on creation of the file-object, since you don't know a priori if you are going to read and/or write to it (actually the read AND write was discontinued in bf60f1a7c2e7f37b09806326b3625bf969a0e960).