By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

Extending struct.unpack to produce nested tuples

P: n/a
I was wondering if there would be any interest in extending the
struct.unpack format notation to be able to express groups of data
with parenthesis.

For example:
data = struct.pack('iiii', 1, 2, 3, 4)
struct.unpack('i(ii)i', data) # Note the parentheses

(1, (2, 3), 4)

Use Case: I have a program written in C that contains a bunch of
aggregate data structures (arrays of structs, structs containing
arrays, etc.) and I'm transmitting these structures over a socket
connection to a Python program that then unpacks the data using the
struct module. Problem is that I have to unpack the incoming data as
a flat sequence of data elements, and then repartition the sequence
into nested sequences to better reflect how the data is structured in
the C program. It would be more convenient to express these groupings
as I'm unpacking the raw data.

I'm sure there are plenty of other use cases for such a feature.

Matthew Barnes
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.