Skip to content

Describing a fixed length field #39

Open

Description

@ocurero

Hi,

How do I describe a fixed length character field that is always 8 bytes long?

Payload() works if the field is variable length but in my case I have a lot of fields that are fixed (padded with blanks if necessary).

Thanks!

Activity

  1. posborne commented on May 19, 2016

    @posborne
    Contributor

    I think UBInt8Sequence should work for this. It appears that it doesn't show up in the docs as it is generated by a factory (https://digidotcom.github.io/python-suitcase/latest/_modules/suitcase/fields.html#BaseFixedByteSequence). It expects a sequence of bytes (e.g. [1, 2, 3, 4, 5, 6, 7, 8]), so some conversion will be required if you have an actual bytes payload.

  2. ocurero commented on May 20, 2016

    @ocurero
    Author

    Thanks Paul!

    It works, but something is still wrong. If I try the way you say:

    IRM_RACF_GRNAME = BaseFixedByteSequence(lambda x: str(x) + 's', 8)

    I get the following error:

      File "./testproto.py", line 153, in client
        printb(a.pack())
      File "/home/oscar/env/lib/python2.7/site-packages/suitcase/structure.py", line 303, in pack
        return self._packer.pack()
      File "/home/oscar/env/lib/python2.7/site-packages/suitcase/structure.py", line 28, in pack
        self.write(sio)
      File "/home/oscar/env/lib/python2.7/site-packages/suitcase/structure.py", line 41, in write
        field.pack(stream)
      File "/home/oscar/env/lib/python2.7/site-packages/suitcase/fields.py", line 889, in pack
        raise SuitcasePackStructException(e)
    suitcase.exceptions.SuitcasePackStructException: pack requires exactly 1 arguments
    

    To make it work I need to change the way self._value is passed to pack:

    Original version not working (line 887 in fields.py):
    stream.write(struct.pack(self.format, *self._value))

    Modified version working (same line 887 in fields.py):
    stream.write(struct.pack(self.format, self._value))

    If the fix is ok, BaseVariableByteSequence has the same problem.

  3. v3gard commented on Oct 5, 2016

    @v3gard

    ocurero's suggestion is a nice workaround, but it would be great if you could implement a FixedByteSequence(number_of_bytes) field in the next version of suitcase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions