jmalkin commented on issue #405:
URL:
https://github.com/apache/datasketches-cpp/issues/405#issuecomment-1799866565
You can define something like this and use it as the serde instead of a
built-in one:
```
import struct
class MixedItemSerDe(PyObjectSerDe):
def get_size(self, item):
if (type(item).__name__ == 'str'):
# type (1 char), length (4 bytes), string
return int(5 + len(item))
else:
# type (1 char), value (8 bytes, whether int or float)
return int(9)
def to_bytes(self, item):
b = bytearray()
item_type = type(item).__name__
print(item_type)
match item_type:
case 'str':
b.extend(b's')
b.extend(len(item).to_bytes(4, 'little'))
b.extend(map(ord,item))
case 'int':
b.extend(b'q')
b.extend(struct.pack('<q', item))
case 'float':
b.extend(b'd')
b.extend(struct.pack('<d', item))
case _:
raise Exception(f'Only str, int, and float are supported. Found
{item_type}')
return bytes(b)
def from_bytes(self, data: bytes, offset: int):
item_type = chr(data[offset])
match item_type:
case 's':
num_chars = int.from_bytes(data[offset+1:offset+4], 'little')
if (num_chars < 0 or num_chars > offset + len(data)):
raise IndexError(f'num_chars read must be non-negative and not
larger than the buffer. Found {num_chars}')
val = data[offset+5:offset+5+num_chars].decode()
return (val, 5+num_chars)
case 'q':
val = struct.unpack_from('<q', data, offset+1)[0]
return (val, 9)
case 'd':
val = struct.unpack_from('<d', data, offset+1)[0]
return (val, 9)
case _:
raise Exception('Unknown item type found')
```
Note that I did *NOT* do extensive testing on this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]