Skip to content

Commit

Permalink
Merge 30955e4 into e612c14
Browse files Browse the repository at this point in the history
  • Loading branch information
shabazpatel committed Aug 20, 2018
2 parents e612c14 + 30955e4 commit 4667658
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
1 change: 1 addition & 0 deletions datmo/__init__.py
Expand Up @@ -24,4 +24,5 @@
log.info("handling command %s", config.home)

import datmo.snapshot
import datmo.logger
import datmo.config
19 changes: 18 additions & 1 deletion datmo/core/util/misc_functions.py
Expand Up @@ -9,6 +9,7 @@
import pytz
import tzlocal
import pytest
import collections
import platform
import tempfile
from io import open
Expand All @@ -28,6 +29,11 @@ def to_bytes(val):
return bytes(val, "utf-8")

to_bytes("test")
try:
basestring
except NameError:
basestring = str

from glob import glob

from datmo.core.controller.environment.driver.dockerenv import DockerEnvironmentDriver
Expand All @@ -52,10 +58,21 @@ def printable_dict(input_dictionary):
printable_output = ""
if input_dictionary:
for key, value in input_dictionary.items():
printable_output = printable_output + key + ": " + str(value) + "\n"
printable_output = printable_output + str(key) + ": " + str(value) + "\n"
return printable_output


def convert_keys_to_string(data):
if isinstance(data, basestring):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(convert_keys_to_string, data.items()))
elif isinstance(data, collections.Iterable):
return type(data)(map(convert_keys_to_string, data))
else:
return data


def printable_object(object, max_width=40):
if not object:
return ""
Expand Down
16 changes: 14 additions & 2 deletions datmo/core/util/tests/test_misc_functions.py
Expand Up @@ -27,7 +27,7 @@ def to_bytes(val):

from datmo.core.util.misc_functions import (
create_unique_hash, mutually_exclusive, is_project_dir, find_project_dir,
grep, prettify_datetime, format_table, parse_cli_key_value,
grep, prettify_datetime, format_table, parse_cli_key_value, convert_keys_to_string,
get_datmo_temp_path, parse_path, parse_paths, list_all_filepaths)

from datmo.core.util.exceptions import MutuallyExclusiveArguments, RequiredArgumentMissing, InvalidDestinationName, PathDoesNotExist, TooManyArgumentsFound
Expand Down Expand Up @@ -290,4 +290,16 @@ def test_parse_paths(self):
assert result[2] == [(os.path.join(default_source_prefix, "test.txt"),
"new_name.txt")]
assert result[3] == [(os.path.join(default_source_prefix, "test_dir"),
"new_dirname")]
"new_dirname")]

def test_convert_keys_to_string(self):
test_data = { u'spam': u'eggs', u'foo': frozenset([u'Gah!']), u'bar': { u'baz': 97 },
u'list': [u'list', (True, u'Maybe'), set([u'and', u'a', u'set', 1])]}

converted_data = convert_keys_to_string(test_data)

assert converted_data == {'bar': {'baz': 97},
'foo': frozenset(['Gah!']),
'list': ['list', (True, 'Maybe'),
set(['and', 'a', 'set', 1])],
'spam': 'eggs'}
66 changes: 66 additions & 0 deletions datmo/logger.py
@@ -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)
46 changes: 46 additions & 0 deletions datmo/tests/test_logger.py
@@ -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}

0 comments on commit 4667658

Please sign in to comment.