Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
chopping a bytearray
#1
i have a large bytearray that i am writing to disk. i expect in most cases it will write the entire array in one call to binaryoutputfile.write(mybytearray). but, sometimes, there can be system conditions (it might be on a networked file system) that cause an incomplete write. simple code to do that could be:
# write the entire buffer
        while outbuf:
            rem=outfile.write(outbuf)
            if not rem:
                break
            outbuf[:-rem]=bytearray()
this code causes byte values in outbuf to be shifted by the slice assignment. it could be made to operate faster by calculating more numbers to slice out just what remains and not do a slice assignment. i don't have code for that, yet. but i am wondering if more complicated code to improve performance in rare cases could be considered reasonably pythonic.

note, i am not knowledgeable about how CPython has implemented slice assignments. i'm just imagining ways to do it in C.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
chopping a bytearray - by Skaperen - Jul-23-2021, 01:08 AM
RE: chopping a bytearray - by Gribouillis - Jul-23-2021, 07:08 AM
RE: chopping a bytearray - by Skaperen - Jul-25-2021, 11:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  bytearray (and bytes) in Python2 Skaperen 3 2,934 Jun-06-2019, 05:43 AM
Last Post: heiner55
  os.path.join, bytearray, python2 vs python3 Skaperen 0 2,037 Jul-06-2018, 01:19 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020