We often have to parse a string to work out the format from it. This can be expensive, but can also be cached. Another method would be to have a proper object to represent the format, which could maybe later become part of the public interface.
So instead of either a str or list(str) we also accept a single BitFormat for Array initialisation, read, readlist, unpack etc. The BitFormat can contain the partial function it needs to read and interpret bits of the correct length, similar to what we use internally in Arrays.
Does the BitFormat actually need to contain anything other than the partial read method? Well it's nice to have a str and int for readability, plus we could also have a write method:
a = pack(dim1, 100)
b = Bits.from_format(dim1, 100)
We often have to parse a string to work out the format from it. This can be expensive, but can also be cached. Another method would be to have a proper object to represent the format, which could maybe later become part of the public interface.
So instead of either a
str
orlist(str)
we also accept a singleBitFormat
forArray
initialisation,read
,readlist
,unpack
etc. The BitFormat can contain the partial function it needs to read and interpret bits of the correct length, similar to what we use internally in Arrays.So we can then do something like an
unpack
with[self.read_method() for read_method in bf.read_methods]
It would be good to allow the user to create and use the BitFormat:
So we'd need to give a human readable representation of the BitFormat:
Which leads us to
When creating with a sequence the strings should be simple dictionary keys with no further processing, for example
uint
but notu
oruint:
.Having partial methods to read the format follows on from how we're doing it in the Array class. It's probably a good place to trial it.
Possibly it's better to only allow a single item in a BitFormat. It would certainly cache better.
if length is None then we parse the interpretation to find it, otherwise the interpretation must be in our dict.
Does the BitFormat actually need to contain anything other than the partial read method? Well it's nice to have a str and int for readability, plus we could also have a write method:
or even make it callable???