Skip to content

Commit ceee715

Browse files
committed
Add proper string representations for each class
1 parent 92d70e7 commit ceee715

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

kafka/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _send_broker_aware_request(self, payloads, encoder_fn, decoder_fn):
174174
except ConnectionError, e: # ignore BufferUnderflow for now
175175
log.warning("Could not send request [%s] to server %s: %s" % (request, conn, e))
176176
failed_payloads += payloads
177-
self.topics_to_brokers = {} # reset metadata
177+
self.topics_to_brokers = {} # reset metadata
178178
continue
179179

180180
for response in decoder_fn(response):
@@ -186,6 +186,9 @@ def _send_broker_aware_request(self, payloads, encoder_fn, decoder_fn):
186186
# Order the accumulated responses by the original key order
187187
return (acc[k] for k in original_keys) if acc else ()
188188

189+
def __repr__(self):
190+
return '<KafkaClient client_id=%s>' % (self.client_id)
191+
189192
#################
190193
# Public API #
191194
#################

kafka/conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, host, port, bufsize=4096):
2929
self._sock.settimeout(10)
3030
self._dirty = False
3131

32-
def __str__(self):
32+
def __repr__(self):
3333
return "<KafkaConnection host=%s port=%d>" % (self.host, self.port)
3434

3535
###################

kafka/consumer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ def __init__(self, client, group, topic, auto_commit=True, partitions=None,
230230
auto_commit_every_n=auto_commit_every_n,
231231
auto_commit_every_t=auto_commit_every_t)
232232

233+
def __repr__(self):
234+
return '<SimpleConsumer group=%s, topic=%s, partitions=%s>' % \
235+
(self.group, self.topic, str(self.offsets.keys()))
236+
233237
def provide_partition_info(self):
234238
"""
235239
Indicates that partition info must be returned by the consumer
@@ -473,6 +477,10 @@ def __init__(self, client, group, topic, auto_commit=True,
473477
proc.start()
474478
self.procs.append(proc)
475479

480+
def __repr__(self):
481+
return '<MultiProcessConsumer group=%s, topic=%s, consumers=%d>' % \
482+
(self.group, self.topic, len(self.procs))
483+
476484
def _consume(self, partitions):
477485
"""
478486
A child process worker which consumes messages based on the

kafka/producer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ def send_messages(self, *msg):
198198
partition = self.next_partition.next()
199199
return super(SimpleProducer, self).send_messages(partition, *msg)
200200

201+
def __repr__(self):
202+
return '<SimpleProducer topic=%s, batch=%s>' % (self.topic, self.async)
203+
201204

202205
class KeyedProducer(Producer):
203206
"""
@@ -239,3 +242,6 @@ def send(self, key, msg):
239242
partitions = self.client.topic_partitions[self.topic]
240243
partition = self.partitioner.partition(key, partitions)
241244
return self.send_messages(partition, msg)
245+
246+
def __repr__(self):
247+
return '<KeyedProducer topic=%s, batch=%s>' % (self.topic, self.async)

0 commit comments

Comments
 (0)