Jul-23-2021, 01:08 AM
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
note, i am not knowledgeable about how CPython has implemented slice assignments. i'm just imagining ways to do it in C.
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.