Closed scott-griffiths closed 2 months ago
Maybe don't just append an r. An R might be clearer. Or how about a Я - that would be cool 😉 . Or perhaps _r separates it better.
Wouldn't _r
work, e.g.:
It is PEP8 compliant and looks clear.
Yes, I agree that the _r
looks the best.
I'll take a look at this after I get version 4.1 released. I'm still not sure it will make the cut - it's possible for example that it impacts the more usual use case and I don't want it to be a performance hit.
My rough plan I think:
_r
then reverse the bits as soon as they are read. E.g. s.read('uint:14_r')
s += 'float16_r=5.2'
x = s.uint_r
s = Bits(ue_r=501)
for example.reverse
option in __init__
. It's probably fine.read
methods. Too confusing if nothing else....and I don't want it to be a performance hit.
Not quite sure how that would be a performance hit. Python's [::-1]
is quite fast.
Also, since the initializer list is getting quite big - would you look into accepting/making a lexer for the tokens? Would be a hit on the runtime performance in the initializing of objects, though would make it much easier to add/parse the fmt string in question.
The performance problem isn't with the reverse itself (we have to do the [::-1]
ourselves and it's a few orders of magnitude faster than the naive implementation). The possible performance hit is more with object creation and token parsing, but the keyword initialisers can be omitted from the first version. Plus I know how much test code will have to be written if I make them more general!
I'm in the process of a refactor that would make it much easier to add new types - probably including allowing the end user to add their own types. This feature should be much easier to implement at that point, but I need to get the rest of the work done first so I'm moving this to the 4.3 release.
This is one of the changes I will instead prioritise for the bitformat library. The reworking of the Dtype class will make the bit reversal a peer of endian byte reversals, so much easier to do.
Migrating idea from issue #226.
It would be useful to some to be able to interpret bitstrings in the opposite direction. This would be equivalent to performing a bitwise reverse before interpreting the bits.
This wouldn't work on exponential Golomb codes if we are in MSB0 bit numbering, but strangely enough should work if we're in LSB0 bit numbering. Which I quite like the symmetry of.
So let's say we construct a bitstring like this:
So to read this back we do something like
where the
r
means to reverse the bits before doing the interpretation. So following this notation, to create the original bitstring we could do:or maybe
Though I don't really like this last method. There would be a whole bunch of new keyword initialisers. It could be a flag:
which is slightly nicer. So maybe we should allow
but then what would this do?
(answer: raise an exception). Also the name
reverse
here is ambiguous and could confuse the user.For interpretation do we allow properties like this:
which would interpret
s
as an int but with the bits reversed. That would imply allowings.intr = 14
as well, which impliess = Bits(int=14, length=100, reverse=True)
should also be allowed.Let's ignore creation and go back to just reading for a bit. Tokens like
float32r
look fine, but what about others. We could havebfloatler
which looks hard to parse (for a human). I'd rather not reinvent regular expressions!Maybe don't just append an
r
. AnR
might be clearer. Or how about aЯ
- that would be cool 😉 . Or perhaps_r
separates it better.