-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR enables using G3Writer and G3MultiFileWriter as a python context object, so that the object can be created and cleanly closed using the python `with` statement. For example: with core.G3Writer(filename="path/to/file.g3") as writer: writer(frame1) writer(frame2) This will open the output file, write the two frames to it, then properly close the file by appending an EndProcessing frame.
- Loading branch information
Showing
6 changed files
with
102 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from spt3g import core | ||
|
||
__all__ = ["G3File"] | ||
|
||
|
||
class G3File(object): | ||
"""Iterable class for G3 files, as created by G3Writer. Loop through frames by doing something like: | ||
with core.G3File("/path/to/file.g3") as f: | ||
for frame in f: | ||
print(frame) | ||
An entire file can also be read into an indexable list by doing: | ||
f = list(core.G3File("/path/to/file.g3")) | ||
""" | ||
|
||
def __init__(self, path): | ||
self.reader = core.G3Reader(path) | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def next(self): | ||
if self.reader is None: | ||
raise StopIteration("Reader closed") | ||
frames = self.reader.Process(None) | ||
if len(frames) == 0: | ||
raise StopIteration("No more frames in file") | ||
if len(frames) > 1: | ||
raise ValueError("Too many frames returned by reader") | ||
return frames[0] | ||
|
||
__next__ = next | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, *args, **kwargs): | ||
del self.reader | ||
self.reader = None | ||
|
||
|
||
# writer context methods | ||
|
||
|
||
def writer_enter(self): | ||
return self | ||
|
||
|
||
def writer_exit(self, *args, **kwargs): | ||
fr = core.G3Frame(core.G3FrameType.EndProcessing) | ||
self(fr) | ||
|
||
|
||
core.G3Writer.__enter__ = writer_enter | ||
core.G3Writer.__exit__ = writer_exit | ||
core.G3MultiFileWriter.__enter__ = writer_enter | ||
core.G3MultiFileWriter.__exit__ = writer_exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters