Simple deserializer for Python

Submitted by:David Villa

Date added:04 December, 2011

Category:Python

This snippet represents a simple deserializer of some basic Python types.

Tags: deserializer

Code Snippet:

import struct

class Deserializer:
__file = None
__open = False

def __readBasicDataType (self, which):
assert self.__open
d = self.__file.read (struct.calcsize(which))
n = struct.unpack (which, d)
return n [0]

def __init__ (self, filename):
self.__file = file (filename, "rb")
self.__open = True

def readLong (self):
return self.__readBasicDataType ('l')

def readUlong (self):
return self.__readBasicDataType ('L')

def readByte (self):
return self.__readBasicDataType ('b')

def readFloat (self):
return self.__readBasicDataType ('f')

def readDouble (self):
return self.__readBasicDataType ('d')

def readBytesAsString (self, num):
assert self.__open
return self.__file.read (num)

def close (self):
assert self.__open
self.__file.close()
self.__open = False
 
 

Comments