Skip to content

Commit

Permalink
[ntuple] Pythonize context manager for RNTupleWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnjo committed Nov 27, 2024
1 parent 046da80 commit 830435f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ def _RNTupleWriter_Fill(self, *args):
return self._Fill(*args)


def _RNTupleWriter_exit(self, *args):
self.CommitDataset()
return False


@pythonization("RNTupleWriter", ns="ROOT::Experimental")
def pythonize_RNTupleWriter(klass):
klass._Fill = klass.Fill
klass.Fill = _RNTupleWriter_Fill

klass.__enter__ = lambda writer: writer
klass.__exit__ = _RNTupleWriter_exit
9 changes: 4 additions & 5 deletions tree/ntuple/v7/test/ntuple_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ def test_write_read(self):

model = RNTupleModel.Create()
model.MakeField["int"]("f")
writer = RNTupleWriter.Recreate(model, "ntpl", "test_ntuple_py_write_read.root")
entry = writer.CreateEntry()
entry["f"] = 42
writer.Fill(entry)
del writer
with RNTupleWriter.Recreate(model, "ntpl", "test_ntuple_py_write_read.root") as writer:
entry = writer.CreateEntry()
entry["f"] = 42
writer.Fill(entry)

reader = RNTupleReader.Open("ntpl", "test_ntuple_py_write_read.root")
self.assertEqual(reader.GetNEntries(), 1)
Expand Down

0 comments on commit 830435f

Please sign in to comment.