Skip to content

Commit 6420227

Browse files
0bsearchhonzakral
authored andcommitted
adds dynamic class-based repr (elastic#713)
1 parent eaa67a1 commit 6420227

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

elasticsearch/client/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ def __repr__(self):
183183
try:
184184
# get a list of all connections
185185
cons = self.transport.hosts
186-
# truncate to 10 if there are too many
186+
# truncate to 5 if there are too many
187187
if len(cons) > 5:
188188
cons = cons[:5] + ['...']
189-
return '<Elasticsearch(%r)>' % cons
189+
return '<{cls}({cons})>'.format(cls=self.__class__.__name__, cons=cons)
190190
except:
191191
# probably operating on custom transport and connection_pool, ignore
192192
return super(Elasticsearch, self).__repr__()

test_elasticsearch/test_client/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@ def test_from_in_search(self):
7575
def test_repr_contains_hosts(self):
7676
self.assertEquals('<Elasticsearch([{}])>', repr(self.client))
7777

78+
def test_repr_subclass(self):
79+
class OtherElasticsearch(Elasticsearch): pass
80+
self.assertEqual('<OtherElasticsearch([{}])>', repr(OtherElasticsearch()))
81+
7882
def test_repr_contains_hosts_passed_in(self):
7983
self.assertIn("es.org", repr(Elasticsearch(['es.org:123'])))
8084

81-
def test_repr_truncates_host_to_10(self):
82-
hosts = [{"host": "es" + str(i)} for i in range(20)]
83-
self.assertNotIn("es5", repr(Elasticsearch(hosts)))
85+
def test_repr_truncates_host_to_5(self):
86+
hosts = [{"host": "es" + str(i)} for i in range(10)]
87+
es = Elasticsearch(hosts)
88+
self.assertNotIn("es5", repr(es))
89+
self.assertIn('...', repr(es))
8490

8591
def test_index_uses_post_if_id_is_empty(self):
8692
self.client.index(index='my-index', doc_type='test-doc', id='', body={})

0 commit comments

Comments
 (0)