Added __str__ to ClusterSpec. This will improve the logs users are attaching to github issues.

For example : #14942

PiperOrigin-RevId: 178296636
This commit is contained in:
Mustafa Ispir 2017-12-07 14:34:25 -08:00 committed by TensorFlower Gardener
parent daab44f7af
commit 2d4c29cd6a
2 changed files with 17 additions and 0 deletions

View File

@ -307,6 +307,12 @@ class ClusterSpec(object):
def __ne__(self, other): def __ne__(self, other):
return self._cluster_spec != other return self._cluster_spec != other
def __str__(self):
key_values = self.as_dict()
string_items = [
repr(k) + ": " + repr(key_values[k]) for k in sorted(key_values)]
return "ClusterSpec({" + ", ".join(string_items) + "})"
def as_dict(self): def as_dict(self):
"""Returns a dictionary from job names to their tasks. """Returns a dictionary from job names to their tasks.

View File

@ -421,6 +421,17 @@ class ServerDefTest(test.TestCase):
class ClusterSpecTest(test.TestCase): class ClusterSpecTest(test.TestCase):
def testStringConversion(self):
cluster_spec = server_lib.ClusterSpec({
"ps": ["ps0:1111"],
"worker": ["worker0:3333", "worker1:4444"]
})
expected_str = (
"ClusterSpec({'ps': ['ps0:1111'], 'worker': ['worker0:3333', "
"'worker1:4444']})")
self.assertEqual(expected_str, str(cluster_spec))
def testProtoDictDefEquivalences(self): def testProtoDictDefEquivalences(self):
cluster_spec = server_lib.ClusterSpec({ cluster_spec = server_lib.ClusterSpec({
"ps": ["ps0:2222", "ps1:2222"], "ps": ["ps0:2222", "ps1:2222"],