Originally Posted by
Devil's Sunrise
Oh, I know. But dict[key] should return null/nil when key's not found! :(
You could create your own wrapper class to make it perform how you want. For example, I created this wrapper class for file objects and I use it EVERYWHERE:
Code:
class IOStream:
__slots__ = ['stream', 'mode', 'IODescriptor', 'EOF']
def __init__(self, stream = '', mode = ''):
self.name = ''
self.mode = ''
self.EOF = 0
if stream != '' and mode != '':
self.fopen(stream, mode)
def fopen(self, stream, mode=None):
if mode == None:
raise Exception("Mode must be explicitly stated.")
if mode == 'string':
self.IODescriptor = cStringIO.StringIO(stream)
self.name = 'cStringIO instance'
else:
self.IODescriptor = open(stream, mode)
self.name = stream
self.mode = mode
self.GetEOF()
def GetEOF(self):
Remember = self.IODescriptor.tell()
self.IODescriptor.seek(0, 2)
self.EOF = self.IODescriptor.tell()
self.IODescriptor.seek(Remember, 0)
def tell(self):
return self.IODescriptor.tell()
def seek(self, offset, whence = 0):
self.IODescriptor.seek(offset, whence)
def read(self, numbytes = 0):
if numbytes != 0:
return self.IODescriptor.read(numbytes)
else:
return self.IODescriptor.read()
def readfile(self, name):
self.IODescriptor = open(name, 'rb')
text = self.IODescriptor.read()
self.IODescriptor.close()
return text
def write(self, whattowrite):
self.IODescriptor.write(whattowrite)
def close(self):
self.IODescriptor.close()
#READ BINARY NUMBER
def rBool(self):
return struct.unpack('?', self.IODescriptor.read(1))[0]
def rU8(self):
return struct.unpack('B', self.IODescriptor.read(1))[0]
def rS8(self):
return struct.unpack('b', self.IODescriptor.read(1))[0]
def rU16(self):
return struct.unpack('H', self.IODescriptor.read(2))[0]
def rS16(self):
return struct.unpack('h', self.IODescriptor.read(2))[0]
def rU32(self):
return struct.unpack('I', self.IODescriptor.read(4))[0]
def rS32(self):
return struct.unpack('i', self.IODescriptor.read(4))[0]
def rU64(self):
return struct.unpack('Q', self.IODescriptor.read(8))[0]
def rS64(self):
return struct.unpack('q', self.IODescriptor.read(8))[0]
def rF32(self):
return struct.unpack('f', self.IODescriptor.read(4))[0]
def rF64(self):
return struct.unpack('d', self.IODescriptor.read(8))[0]
#BIG ENDIAN
def rBool(self):
return struct.unpack('?', self.IODescriptor.read(1))[0]
def rbU8(self):
return struct.unpack('!B', self.IODescriptor.read(1))[0]
def rbS8(self):
return struct.unpack('!b', self.IODescriptor.read(1))[0]
def rbU16(self):
return struct.unpack('!H', self.IODescriptor.read(2))[0]
def rbS16(self):
return struct.unpack('!h', self.IODescriptor.read(2))[0]
def rbU32(self):
return struct.unpack('!I', self.IODescriptor.read(4))[0]
def rbS32(self):
return struct.unpack('!i', self.IODescriptor.read(4))[0]
def rb64(self):
return struct.unpack('!Q', self.IODescriptor.read(8))[0]
def rb64(self):
return struct.unpack('!q', self.IODescriptor.read(8))[0]
def rPackNum(self):
value = self.rS8()
if value == -128:
return self.rS32()
else:
return value
And Spaz... checked exceptions? You serious? Java requires that programmers have to code against certain exceptions when performing certain acts? Seems like unneeded boilerplate. I guess that's why I prefer C# over Java. Java does not play nice with binary, unsigned types, and now checked exceptions. Ugh.
Bookmarks