summaryrefslogtreecommitdiff
path: root/kafka/protocol/struct.py
diff options
context:
space:
mode:
authorTyler Lubeck <tyler@coffeemeetsbagel.com>2020-02-06 12:27:09 -0800
committerGitHub <noreply@github.com>2020-02-06 12:27:09 -0800
commit209515bf9dcdd9e03bc286035641af3ae72fcbf9 (patch)
tree647c19c40da7ff78fdcc485fd790cee82838940d /kafka/protocol/struct.py
parent3d98741be0e9608a352221b476cf3aa2d86777be (diff)
downloadkafka-python-209515bf9dcdd9e03bc286035641af3ae72fcbf9.tar.gz
Implement methods to convert a Struct object to a pythonic object (#1951)
Implement methods to convert a Struct object to a pythonic object
Diffstat (limited to 'kafka/protocol/struct.py')
-rw-r--r--kafka/protocol/struct.py6
1 files changed, 6 insertions, 0 deletions
diff --git a/kafka/protocol/struct.py b/kafka/protocol/struct.py
index 693e2a2..e9da6e6 100644
--- a/kafka/protocol/struct.py
+++ b/kafka/protocol/struct.py
@@ -30,6 +30,7 @@ class Struct(AbstractType):
# causes instances to "leak" to garbage
self.encode = WeakMethod(self._encode_self)
+
@classmethod
def encode(cls, item): # pylint: disable=E0202
bits = []
@@ -48,6 +49,11 @@ class Struct(AbstractType):
data = BytesIO(data)
return cls(*[field.decode(data) for field in cls.SCHEMA.fields])
+ def get_item(self, name):
+ if name not in self.SCHEMA.names:
+ raise KeyError("%s is not in the schema" % name)
+ return self.__dict__[name]
+
def __repr__(self):
key_vals = []
for name, field in zip(self.SCHEMA.names, self.SCHEMA.fields):