base.py
1 """ 2 Encoder module 3 """ 4 5 from io import BytesIO 6 7 8 class Encoder: 9 """ 10 Encodes and decodes object content. The base encoder works only with byte arrays. It can be extended to encode different datatypes. 11 """ 12 13 def encode(self, obj): 14 """ 15 Encodes an object to a byte array using the encoder. 16 17 Args: 18 obj: object to encode 19 20 Returns: 21 encoded object as a byte array 22 """ 23 24 return obj 25 26 def decode(self, data): 27 """ 28 Decodes input byte array into an object using this encoder. 29 30 Args: 31 data: encoded data 32 33 Returns: 34 decoded object 35 """ 36 37 return BytesIO(data) if data else None