Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -24,4 +24,5 @@ | |
log.info("handling command %s", config.home) | ||
|
||
import datmo.snapshot | ||
import datmo.logger | ||
import datmo.config |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import print_function | ||
|
||
import os | ||
try: | ||
basestring | ||
except NameError: | ||
basestring = str | ||
|
||
from datmo.core.util.json_store import JSONStore | ||
from datmo.core.util.exceptions import InvalidArgumentType | ||
|
||
|
||
class Logger(): | ||
"""Logger is a class to enable user to store properties | ||
Attributes | ||
---------- | ||
config : dict | ||
dictionary containing input or output configs from the run | ||
results : dict | ||
dictionary containing output results from the run | ||
Methods | ||
------- | ||
log_config(config) | ||
Saving the configuration dictionary for the run | ||
log_results(results) | ||
Saving the result dictionary for the run | ||
Raises | ||
------ | ||
InvalidArgumentType | ||
""" | ||
|
||
def __init__(self, task_dir="/task"): | ||
|
||
self.task_dir = task_dir | ||
|
||
@classmethod | ||
def __save_dictionary(self, dictionary, path): | ||
json_obj = JSONStore(path) | ||
data = json_obj.to_dict() | ||
data.update(dictionary) | ||
json_obj.to_file(data) | ||
return data | ||
|
||
def log_config(self, config): | ||
|
||
if not isinstance(config, dict): | ||
raise InvalidArgumentType() | ||
|
||
config_path = os.path.join(self.task_dir, "config.json") \ | ||
if os.path.isdir(self.task_dir) else\ | ||
os.path.join(os.getcwd(), "config.json") | ||
|
||
return self.__save_dictionary(config, config_path) | ||
|
||
def log_result(self, results): | ||
if not isinstance(results, dict): | ||
raise InvalidArgumentType() | ||
|
||
results_path = os.path.join(self.task_dir, "stats.json") \ | ||
if os.path.isdir(self.task_dir) else\ | ||
os.path.join(os.getcwd(), "stats.json") | ||
|
||
return self.__save_dictionary(results, results_path) |
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,46 @@ | ||
#!/usr/bin/python | ||
""" | ||
Tests for snapshot module | ||
""" | ||
import os | ||
import tempfile | ||
import platform | ||
try: | ||
to_unicode = unicode | ||
except NameError: | ||
to_unicode = str | ||
|
||
from datmo.logger import Logger | ||
|
||
|
||
class TestLoggerModule(): | ||
def setup_method(self): | ||
# provide mountable tmp directory for docker | ||
tempfile.tempdir = "/tmp" if not platform.system( | ||
) == "Windows" else None | ||
test_datmo_dir = os.environ.get('TEST_DATMO_DIR', | ||
tempfile.gettempdir()) | ||
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) | ||
self.logger = Logger(task_dir=self.temp_dir) | ||
|
||
def teardown_method(self): | ||
pass | ||
|
||
def test_log_config(self): | ||
config = {'a': 1} | ||
saved_config = self.logger.log_config(config) | ||
assert saved_config == config | ||
|
||
config = {'b': 2} | ||
saved_config = self.logger.log_config(config) | ||
assert saved_config == {'a': 1, 'b': 2} | ||
|
||
|
||
def test_log_results(self): | ||
result = {'a': 1} | ||
saved_result = self.logger.log_result(result) | ||
assert saved_result == result | ||
|
||
result = {'b': 2} | ||
saved_result = self.logger.log_result(result) | ||
assert saved_result == {'a': 1, 'b': 2} |