@@ -469,7 +469,7 @@ def read_metadata(self):
469469 return
470470 self .formatter .read_metadata (self )
471471
472- def write (self , write_metadata = False , only_complete = True ):
472+ def write (self , write_metadata = False , only_complete = True , filename = None ):
473473 """
474474 Writes updates to the DataSet to storage.
475475 N.B. it is recommended to call data_set.finalize() when a DataSet is
@@ -480,15 +480,26 @@ def write(self, write_metadata=False, only_complete=True):
480480 only_complete (bool): passed on to the match_save_range inside
481481 self.formatter.write. Used to ensure that all new data gets
482482 saved even when some columns are strange.
483+ filename (Optional[str]): The filename (minus extension) to use.
484+ The file gets saved in the usual location.
483485 """
484486 if self .location is False :
485487 return
486488
487- self .formatter .write (self ,
488- self .io ,
489- self .location ,
490- write_metadata = write_metadata ,
491- only_complete = only_complete )
489+ # Only the gnuplot formatter has a "filename" kwarg
490+ if isinstance (self .formatter , GNUPlotFormat ):
491+ self .formatter .write (self ,
492+ self .io ,
493+ self .location ,
494+ write_metadata = write_metadata ,
495+ only_complete = only_complete ,
496+ filename = filename )
497+ else :
498+ self .formatter .write (self ,
499+ self .io ,
500+ self .location ,
501+ write_metadata = write_metadata ,
502+ only_complete = only_complete )
492503
493504 def write_copy (self , path = None , io_manager = None , location = None ):
494505 """
@@ -562,21 +573,28 @@ def save_metadata(self):
562573 self .snapshot ()
563574 self .formatter .write_metadata (self , self .io , self .location )
564575
565- def finalize (self ):
576+ def finalize (self , filename = None , write_metadata = True ):
566577 """
567578 Mark the DataSet complete and write any remaining modifications.
568579
569580 Also closes the data file(s), if the ``Formatter`` we're using
570581 supports that.
582+
583+ Args:
584+ filename (Optional[str]): The file name (minus extension) to
585+ write to. The location of the file is the usual one.
586+ write_metadata (bool): Whether to save a snapshot. For e.g. dumping
587+ raw data inside a loop, a snapshot is not wanted.
571588 """
572589 log .debug ('Finalising the DataSet. Writing.' )
573590 # write all new data, not only (to?) complete columns
574- self .write (only_complete = False )
591+ self .write (only_complete = False , filename = filename )
575592
576593 if hasattr (self .formatter , 'close_file' ):
577594 self .formatter .close_file (self )
578595
579- self .save_metadata ()
596+ if write_metadata :
597+ self .save_metadata ()
580598
581599 def snapshot (self , update = False ):
582600 """JSON state of the DataSet."""
0 commit comments