eerimoq / bincopy

Mangling of various file formats that conveys binary information (Motorola S-Record, Intel HEX, TI-TXT, Verilog VMEM, ELF and binary files).
MIT License
109 stars 38 forks source link

Chunk.__len__ should return length of data #36

Closed bessman closed 1 year ago

bessman commented 1 year ago
import bincopy
hexdata = bincopy("myfile.hex")
print(len(next(hexdata.segments.chunks(size=32))))
2

I find this surprising. Anyone doing len(chunk) probably wants the length of its data member, not the fact that it's a two-tuple. I propose implementing Chunk like so:

class Chunk(namedtuple("Chunk", ["address", "data"])):  # Or _Chunk, depending on outcome of #35
    def __len___(self):
        return len(self.data)

While this would technically change the API, I argue that it is extremely unlikely to break anything; anyone doing len(chunk) almost certainly means len(chunk.data).

eerimoq commented 1 year ago

Good suggestion. Part of version 18.0.0.