Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describing a fixed length field #39

Open
ocurero opened this issue May 19, 2016 · 3 comments
Open

Describing a fixed length field #39

ocurero opened this issue May 19, 2016 · 3 comments

Comments

@ocurero
Copy link

ocurero commented May 19, 2016

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!

@posborne
Copy link
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.

@ocurero
Copy link
Author

ocurero commented May 20, 2016

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.

@v3gard
Copy link

v3gard commented Oct 5, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants