Conversion of perl unpack code to python - something odd

A

Andrew Robert

Hey everyone,


Maybe you can see something I don't.

I need to convert a working piece of perl code to python.

The perl code is:

sub ParseTrig {
# unpack the ibm struct that triggers messages.
my $struct = shift;

my %data;
@data{qw/StructID Version QName ProcessName TriggerData ApplType
ApplId EnvData UserData QMgrName/} =
unpack("A4A4A48A48A64A4A256A128A128A48", $struct);

return undef unless $data{StructID} eq 'TMC';

return \%data;


Taking away the fact that it is a function, the base code sets a 732
element structure with the ascii pattern derived above.

I wrote a simple test script that accepts the command argument at
position 1.



#!C:\Python24\python
import sys, string, struct

# From the language declarations in manual, the following format was derived

format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
size=struct.calcsize(format)

# Extract list item out for convenience/readability
data=sys.argv[1]

# Calculated size of the struct format is 732
# Length of data string is 732

print len(data)
print size

d1,d2=struct.unpack(format,data)
print d1
sys.exit(0)


When I run it, it says there are too many values to unpack.



Traceback ( most recent call last)
File "m:\mq\mq\scripts\format.py", line 32, in ?
d1, d2 = struct.unpack(format,data)
ValueError: too many values to unpack
Error starting triggered application




I checked the manual on the structure used to pack the code and it
appears correct.

#
# MQTMC2 Language declarations as defined chapt 22 of programmers ref
# http://publibfp.boulder.ibm.com/epubs/pdf/csqzak09.pdf
#
# CHAR4 Struct ID
# CHAR4 Version
# CHAR48 QName
# CHAR48 ProcessName
# CHAR64 TriggerData
# CHAR4 ApplType
# CHAR256 ApplID
# CHAR128 EnvData
# CHAR128 UserData
# CHAR48 QMgrName


Any help you can provide on this would be greatly appreciated.
 
F

Fredrik Lundh

Andrew said:
# From the language declarations in manual, the following format was derived

format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'

that's 10 specifiers.
d1,d2=struct.unpack(format,data)

that's two variables.
print d1
sys.exit(0)

When I run it, it says there are too many values to unpack.

Traceback ( most recent call last)
File "m:\mq\mq\scripts\format.py", line 32, in ?
d1, d2 = struct.unpack(format,data)
ValueError: too many values to unpack
Error starting triggered application

I checked the manual on the structure used to pack the code and it
appears correct.

try printing the return value from struct.unpack, and see if you can
figure out what's wrong with your code:

print struct.unpack(format,data)

</F>
 
P

Peter Otten

Andrew said:
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'

You are trying to squeeze 10 items into just
d1,d2=struct.unpack(format,data)

two variables (d1 and d2)
ValueError: too many values to unpack

and Python is quite explicit that it doesn't like that once you realize that
'unpack' doesn't refer to struct.unpack() but to tuple unpacking like
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: too many values to unpack


Peter
 
A

Andrew Robert

Peter said:
You are trying to squeeze 10 items into just


two variables (d1 and d2)


and Python is quite explicit that it doesn't like that once you realize that
'unpack' doesn't refer to struct.unpack() but to tuple unpacking like

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: too many values to unpack


Peter
Now I feel like a first class idiot.

Thanks for the help.

Added the extra eight variables and things worked perfectly.

Going to go stand in the corner for an hour now :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top