efficient use of ustruct.unpack?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

efficient use of ustruct.unpack?

Post by mathieu » Wed Jan 31, 2018 11:17 pm

Dear all,

I've been trying for a while to read values from a binary file containing unsigned short integers ('H'), without much success beyond reading and appending each value in a for loop. There must be a better option...

What is the most efficient way to read from such a file and output an array('H')?

Thanks in advance,

- Mathieu

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: efficient use of ustruct.unpack?

Post by cefn » Thu Feb 01, 2018 1:57 am

Hard to know what you've done, what you're trying to do.

You say you are without much success, but then seem to go on and say that you are successfully reading values. What is it that you can't do?

No code or console output provided to give us a clue what you are doing.

Could you share a bit more about your situation with clarity, please?

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: efficient use of ustruct.unpack?

Post by OutoftheBOTS_ » Thu Feb 01, 2018 6:55 am

very easy you can unpack muitlpe items at once even if they r different types, for you it will be even easier as they r all the same types.

Something like this will unpack 100 integers into an array

Code: Select all

format_string = "H" * 100
array_of_intergers = ustruct.unpack (format_string, string_2b_unpacked)

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: efficient use of ustruct.unpack?

Post by deshipu » Thu Feb 01, 2018 8:04 am

If it was cpython, the most efficient way would be https://docs.python.org/2/library/array ... y.fromfile

User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

Re: efficient use of ustruct.unpack?

Post by mathieu » Thu Feb 01, 2018 9:44 am

cefn wrote:
Thu Feb 01, 2018 1:57 am
Hard to know what you've done, what you're trying to do.

You say you are without much success, but then seem to go on and say that you are successfully reading values. What is it that you can't do?

No code or console output provided to give us a clue what you are doing.

Could you share a bit more about your situation with clarity, please?
Sorry, I believed I was clear enough. I was trying to do something like this:

Code: Select all

import array, ustruct

with open('data.bin', 'wb') as f :
	for x in range(100) :
		f.write( ustruct.pack('H', x) )

with open('data.bin', 'rb') as f :
	data = array.array('H', ustruct.unpack('H', f.read()))
And of course I was not getting the expected results because of my incorrect ustruct format specifier ('H' instead of 'H'*100), as pointed out by OutoftheBOTS_. Thanks for taking the time to reply.
OutoftheBOTS_ wrote:
Thu Feb 01, 2018 6:55 am
very easy you can unpack muitlpe items at once even if they r different types, for you it will be even easier as they r all the same types.

Something like this will unpack 100 integers into an array

Code: Select all

format_string = "H" * 100
array_of_intergers = ustruct.unpack (format_string, string_2b_unpacked)
Duh. Of course! Thanks a lot.
deshipu wrote:
Thu Feb 01, 2018 8:04 am
If it was cpython, the most efficient way would be https://docs.python.org/2/library/array ... y.fromfile
Yes, that's what I tried first. I really wish that array.array.fromfile() were available in uPython!

Thanks again everyone,

- Mathieu

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: efficient use of ustruct.unpack?

Post by SpotlightKid » Thu Feb 01, 2018 11:12 am

OutoftheBOTS_ wrote:
Thu Feb 01, 2018 6:55 am

Code: Select all

format_string = "H" * 100
array_of_intergers = ustruct.unpack (format_string, string_2b_unpacked)
A better way is this:

Code: Select all

>>> import ustruct as struct
>>> struct.pack('10H', *range(10))
b'\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07\x00\x08\x00\t\x00'
>>> struct.unpack('10H', _)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
i.e. just prefix the format char with the number of elements.

Post Reply