Skip to content

Commit 5affde1

Browse files
committed
nicer repr for Cluster object
1 parent f589961 commit 5affde1

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

ipyparallel/cluster/cluster.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import asyncio
88
import inspect
99
import logging
10+
import os
1011
import random
1112
import socket
1213
import string
@@ -24,9 +25,7 @@
2425
from traitlets import Float
2526
from traitlets import Integer
2627
from traitlets import List
27-
from traitlets import Type
2828
from traitlets import Unicode
29-
from traitlets import validate
3029
from traitlets.config import LoggingConfigurable
3130

3231
from . import launcher
@@ -106,7 +105,7 @@ def _default_profile_dir(self):
106105
If you are using one of IPython's builtin launchers, you can specify just the
107106
prefix, e.g:
108107
109-
c.IPClusterStart.controller_launcher_class = 'SSH'
108+
c.Cluster.controller_launcher_class = 'SSH'
110109
111110
or:
112111
@@ -145,7 +144,7 @@ def _default_profile_dir(self):
145144
If you are using one of IPython's builtin launchers, you can specify just the
146145
prefix, e.g:
147146
148-
c.IPClusterEngines.engine_launcher_class = 'SSH'
147+
c.Cluster.engine_launcher_class = 'SSH'
149148
150149
or:
151150
@@ -206,6 +205,26 @@ def _default_log(self):
206205
_controller = Any()
207206
_engine_sets = Dict()
208207

208+
def __repr__(self):
209+
profile_dir = self.profile_dir
210+
home_dir = os.path.expanduser("~")
211+
if profile_dir.startswith(home_dir + os.path.sep):
212+
# truncate $HOME/. -> ~/...
213+
profile_dir = "~" + profile_dir[len(home_dir) :]
214+
215+
fields = {
216+
"cluster_id": repr(self.cluster_id),
217+
"profile_dir": repr(profile_dir),
218+
}
219+
if self._controller:
220+
fields["controller"] = "<running>"
221+
if self._engine_sets:
222+
fields["engine_sets"] = list(self._engine_sets)
223+
224+
fields_str = ', '.join(f"{key}={value}" for key, value in fields.items())
225+
226+
return f"<{self.__class__.__name__}({fields_str})>"
227+
209228
@classmethod
210229
def from_json(self, json_dict):
211230
"""Construct a Cluster from serialized state"""

ipyparallel/tests/test_cluster.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,18 @@ def test_sync_with(Cluster):
143143
def test_cluster_abbreviations(classname, expected_class):
144144
c = cluster.Cluster(engine_launcher_class=classname)
145145
assert c.engine_launcher_class is expected_class
146+
147+
148+
async def test_cluster_repr(Cluster):
149+
c = Cluster(cluster_id="test", profile_dir='/tmp')
150+
assert repr(c) == "<Cluster(cluster_id='test', profile_dir='/tmp')>"
151+
await c.start_controller()
152+
assert (
153+
repr(c)
154+
== "<Cluster(cluster_id='test', profile_dir='/tmp', controller=<running>)>"
155+
)
156+
await c.start_engines(1, 'engineid')
157+
assert (
158+
repr(c)
159+
== "<Cluster(cluster_id='test', profile_dir='/tmp', controller=<running>, engine_sets=['engineid'])>"
160+
)

0 commit comments

Comments
 (0)