Simple deserializer for Python

From Nokia Developer Wiki
Jump to: navigation, search

Article Metadata
Tested with
Devices(s): Nokia N95 8GB
Platform(s): S60 3rd Edition
Platform Security
Capabilities: )
Keywords: struct
Created: lpvalente (03 Mar 2009)
Last edited: hamishwillee (24 Jun 2011)


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

Source Code

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.__open = False
This page was last modified on 24 June 2011, at 11:56.
54 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.