Added ability to both read and write vectors*

This change affects the BinaryReader and BinaryWriter
Note that it only allows to write vectors of **TLObjects**,
not any other type
This commit is contained in:
Lonami 2016-09-26 16:10:07 +02:00
parent 36b8a9026f
commit ded655911e
2 changed files with 19 additions and 0 deletions

View File

@ -128,6 +128,14 @@ class BinaryReader:
result.on_response(self) result.on_response(self)
return result return result
def tgread_vector(self):
"""Reads a vector (a list) of Telegram objects"""
if 0x1cb5c415 != self.read_int(signed=False):
raise ValueError('Invalid constructor code, vector was expected')
count = self.read_int()
return [self.tgread_object() for _ in range(count)]
# endregion # endregion
def close(self): def close(self):

View File

@ -95,6 +95,17 @@ class BinaryWriter:
value = 0 if datetime is None else int(datetime.timestamp()) value = 0 if datetime is None else int(datetime.timestamp())
self.write_int(value) self.write_int(value)
def tgwrite_object(self, tlobject):
"""Writes a Telegram object"""
tlobject.on_send(self)
def tgwrite_vector(self, vector):
"""Writes a vector of Telegram objects"""
self.write_int(0x1cb5c415, signed=False) # Vector's constructor ID
self.write_int(len(vector))
for item in vector:
self.tgwrite_object(item)
# endregion # endregion
def flush(self): def flush(self):