×
Namespaces

Variants
Actions

Simple deserializer for Python

From Nokia Developer Wiki
Jump to: navigation, search


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


Introduction

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.__file.close()
self.__open = False
This page was last modified on 24 June 2011, at 11:56.
71 page views in the last 30 days.
×