Skip to content

Commit

Permalink
add open mode to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon de Oliveira committed Feb 5, 2017
1 parent 0a72fd8 commit 4a9f23e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions sacred/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def run_commandline(self, argv=None):
else:
print_filtered_stacktrace()

def open_resource(self, filename):
def open_resource(self, filename, mode='r'):
"""Open a file and also save it as a resource.
Opens a file, reports it to the observers as a resource, and returns
Expand All @@ -269,14 +269,16 @@ def open_resource(self, filename):
----------
filename: str
name of the file that should be opened
mode : str
mode that file will be open
Returns
-------
file
the opened file-object
"""
assert self.current_run is not None, "Can only be called during a run."
return self.current_run.open_resource(filename)
return self.current_run.open_resource(filename, mode)

def add_artifact(self, filename, name=None):
"""Add a file as an artifact.
Expand Down
6 changes: 4 additions & 2 deletions sacred/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, config, config_modifications, main_function, observers,
self._failed_observers = []
self._output_file = None

def open_resource(self, filename):
def open_resource(self, filename, mode='r'):
"""Open a file and also save it as a resource.
Opens a file, reports it to the observers as a resource, and returns
Expand All @@ -124,6 +124,8 @@ def open_resource(self, filename):
----------
filename : str
name of the file that should be opened
mode : str
mode that file will be open
Returns
-------
Expand All @@ -132,7 +134,7 @@ def open_resource(self, filename):
"""
filename = os.path.abspath(filename)
self._emit_resource_added(filename) # TODO: maybe non-blocking?
return open(filename, 'r') # TODO: How to deal with binary mode?
return open(filename, mode)

def add_artifact(self, filename, name=None):
"""Add a file as an artifact.
Expand Down

0 comments on commit 4a9f23e

Please sign in to comment.