Created
May 24, 2016 16:03
-
-
Save themperek/4042f2d20218cf8813143a057e814052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def simple_ser(data, meta = {}): | |
buffer = array('B', []) | |
buffer.fromstring(data.data) | |
meta['data_meta'] = {'dtype': str(data.dtype), 'shape': data.shape} | |
meta_json = jsonapi.dumps(meta) | |
meta_json_buffer = array('B', []) | |
meta_json_buffer.fromstring(meta_json) | |
meta_len = len(meta_json) | |
meta_len_byte = struct.unpack("4b", struct.pack("I", meta_len)) | |
buffer.extend(meta_json_buffer) | |
buffer.extend(meta_len_byte) | |
return buffer | |
def simple_des(buffer): | |
len_buffer = buffer[-4:] | |
len = struct.unpack("I", len_buffer.tostring() )[0] | |
meta = jsonapi.loads(buffer[-4-len:-4].tostring()) | |
dtype = np.dtype(ast.literal_eval(meta['data_meta']['dtype'])) | |
data = np.frombuffer(buffer[:-4-len], dtype).reshape(meta['data_meta']['shape']) | |
return data, meta | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment