Basically I have some classes like this:
############################################################################### # 0x01: ModeCommand ############################################################################### class ModeCommand: """This is the Mode Command Packet class.""" def __init__(self, mode, command, id=0x01): """The unspecial init function.""" self.mode = mode self.command = command self.id = id def RawData(self): return [self.id, self.mode, self.command] def __getitem__(self, index): """[] operator (read): indexes from the byte data.""" return self.RawData()[index] def __str__(self): """Print a nice thing.""" string = "Mode = %d\n" % self.mode + \ "Command = %d\n" % self.command + \ "ID = %d\n\n" % self.id return string ############################################################################### # 0x02: CallRequest ############################################################################### class CallRequest: """This is the Call Request Packet class. (Look familiar?)""" def __init__(self, service_number, id=0x02): """The unspecial init function.""" self.service_number = service_number self.id = id def RawData(self): return [self.id, self.service_number] def __getitem__(self, index): """[] operator (read): indexes from the byte data.""" return self.RawData()[index] def __str__(self): """Print a nice thing.""" string = "Service Number = %d\n" % self.service_number + \ "ID = %d\n\n" % self.id return string ############################################################################### # Test Code ############################################################################### x = ModeCommand(mode=1, command=0) print x[:] print x y = CallRequest(service_number=2001) print y[:] print y Which is ok but I had to do this over 100 times. It is difficult to maintain and no one else can understand what the heck I did here. So I was trying to look a metaclasses to do something fancy like this: ############################################################################### # 0x01: ModeCommand ############################################################################### PacketModeCommand = ( ('name', "ModeCommand"), ('id', 0x01), ('action', 8), # size in bytes ('command', 8), ) ############################################################################### # Add some kind of class factory here: (metaclass, type(name, bases, dict) -> a new type, ???) ############################################################################### """???""" ############################################################################### # Add some kind of class factory here ############################################################################### """Hey this would be cool if I had a class just by making the dictionary.""" x = ModeCommand(mode=1, command=0) print x[:] print x So that I could make a dictionary that would basically define my classes on the fly. This would be simple enough for everyone to maintain and would make changes to the format of all the classes relatively simple. (So far the only way that I have thought of doing this is will a parsing script but that would be lame.) Of course, this becomes more and more complicated as my classes (representing packets) start having objects of their own and functions that need to be defined on the fly too... but for now I would be nice to know if this idea is possible. Thanks in advance.
-- http://mail.python.org/mailman/listinfo/python-list