CCSDSPy / ccsdspy

I/O interface and utilities for CCSDS binary spacecraft data in Python. Library used in flight missions at NASA, NOAA, and SWRI
https://ccsdspy.org
BSD 3-Clause "New" or "Revised" License
78 stars 19 forks source link

Make the load method of FixedLength and VariableLength return the file stream argument at the location where it was before the call #111

Closed tloubrieu-jpl closed 7 months ago

tloubrieu-jpl commented 7 months ago

That would be easy to implement in the _load function.

It is needed when the same stream is parsed multiple times, in my case, for 1. decision_packet (when different packets definitions can be used with the same APID), 2. calculate a CRC (see discussion https://github.com/CCSDSPy/ccsdspy/discussions/108#discussioncomment-8425180)

That could be optional with an argument 'return_file_where_it_was' default to False to make the code backward compatible ?

Let me know, your thoughts on that.

ddasilva commented 7 months ago

Can you try this? This is the Pythonic way to do it, I believe.

pos = file.tell()
results = pkt.load(fh)
file.seek(pos)

I haven't seen any other APIs support the type of option you're proposing.. and it should be pretty easy without it so I'm hesitant to add a new keyword argument right now.

tloubrieu-jpl commented 7 months ago

Ok, I understand your point. In my code I am parsing 3 times the same stream which made it easier to have that feature in ccsdspy, but I can as well encapsulate the lines you gave in a function to make my code lighter. That will work for me. Thanks @ddasilva

ddasilva commented 7 months ago

Np. It's not a huge deal either way, if it's useful for you to have it this way then I'll accept a pull request. Just call it load(..., reset_file_obj=False) and make sure it has proper doc + tests.