Conversion of perl unpack code to python - something odd

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Robert

    Conversion of perl unpack code to python - something odd

    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("A4A4A48 A48A64A4A256A12 8A128A48", $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\p ython
    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.cal csize(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.un pack(format,dat a)
    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\scrip ts\format.py", line 32, in ?
    d1, d2 = struct.unpack(f ormat,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.

  • Fredrik Lundh

    #2
    Re: Conversion of perl unpack code to python - something odd

    Andrew Robert wrote:
    [color=blue]
    > # From the language declarations in manual, the following format was derived
    >
    > format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'[/color]

    that's 10 specifiers.
    [color=blue]
    > d1,d2=struct.un pack(format,dat a)[/color]

    that's two variables.
    [color=blue]
    > 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\scrip ts\format.py", line 32, in ?
    > d1, d2 = struct.unpack(f ormat,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.[/color]

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

    print struct.unpack(f ormat,data)

    </F>

    Comment

    • Peter Otten

      #3
      Re: Conversion of perl unpack code to python - something odd

      Andrew Robert wrote:
      [color=blue]
      > format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'[/color]

      You are trying to squeeze 10 items into just
      [color=blue]
      > d1,d2=struct.un pack(format,dat a)[/color]

      two variables (d1 and d2)
      [color=blue]
      > ValueError: too many values to unpack[/color]

      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
      [color=blue][color=green][color=darkred]
      >>> a, b = "ab"
      >>> a, b = "abc"[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      ValueError: too many values to unpack


      Peter

      Comment

      • Andrew Robert

        #4
        Re: Conversion of perl unpack code to python - something odd

        Peter Otten wrote:[color=blue]
        > Andrew Robert wrote:
        >[color=green]
        >> format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'[/color]
        >
        > You are trying to squeeze 10 items into just
        >[color=green]
        >> d1,d2=struct.un pack(format,dat a)[/color]
        >
        > two variables (d1 and d2)
        >[color=green]
        >> ValueError: too many values to unpack[/color]
        >
        > 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
        >[color=green][color=darkred]
        >>>> a, b = "ab"
        >>>> a, b = "abc"[/color][/color]
        > Traceback (most recent call last):
        > File "<stdin>", line 1, in ?
        > ValueError: too many values to unpack
        >
        >
        > Peter
        >[/color]
        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 :)

        Comment

        Working...