PyObject *data - access to raw data?

Thomas Gagne tgagne at ix.netcom.com
Tue Aug 8 07:26:35 EDT 2000


Donn Cave wrote:

> Quoth Thomas Gagne <tgagne at ix.netcom.com>:
> <snip>
> | If a Python module wants to send a C-like structure to another program written in a
> | a different language (binary representation aside - assume they've already
> | considered it) how then would I access the data of, say, the struct?  Hmm.  I'll
> | take a look at the struct module to see if that answers my question.
>
> Good, by the time you read this you will already have some of the answers.

It would seem the pack and unpack methods are great for creating a structure all at
once, but not so great at accessing members of a structure for purposes of
manipulation.  I've decided to create a class that uses a dictionary to maintain the
values for the members.  It uses a method called asBytes() to return the string produced
by struct.pack().  I should probably call it asString() since I'll document the
arguments to send and recv and string arguments.

Of course, right not its all buried inside my IsdHeader class, but I'm thinking I should
create a more general class called CStructure that defines all the methods for such
things, then subclassing it to create IsdHeader.
-------------- next part --------------
import isdio
import struct

class IsdHeader:
	def __init__(self):
		self.data = { 
			'len':0,
			'sequence':0,
			'reply':0,
			'error':0,
			'command':0,
			'version':0,
			'workerid':0,
			'more':0 }

	def setVal(self, key, value):
		self.data[key] = value
		return self.getVal(key)

	def getVal(self, key):
		return self.data[key]

	def len(self):
		return self.getVal('len')

	def len(self, anInteger):
		return self.setVal('len', anInteger)

	def sequence(self):
		return self.getVal('sequence')

	def sequence(self, anInteger):
		return self.setVal('sequence', anInteger)

	def more(self):
		return self.getVal('more')

	def more(self, anInteger):
		return self.setVal('more', anInteger)

	def asBytes(self):
		return struct.pack('llhhhhlh21h',
			self.data['len'],
			self.data['sequence'],
			self.data['reply'],
			self.data['error'],
			self.data['command'],
			self.data['version'],
			self.data['workerid'],
			self.data['more'],
			0, 0, 0, 0, 0,
			0, 0, 0, 0, 0,
			0, 0, 0, 0, 0,
			0, 0, 0, 0, 0,
			0)


More information about the Python-list mailing list