from collections import abc
s = Bits()
isinstance(s, abc.Sequence) # False
Would it be a good idea to support Sequence and MutableSequence ABCs? In general yes, as we are representing sequences, but the additional methods we'd need to support are all a bit pointless.
Note that you don't need to subclass the ABCs you can just do abc.Sequence.register(Bits) but then you don't get the mixin methods for free.
For Sequence:
index(value) : Return index of first occurrence of value. As the only thing contained are zeros and ones this is not helpful.
For MutableSequence:
extend(iterable): Append elements from iterable.
pop(index) : Remove and return item a index (default to end).
remove(value): Remove first occurrence of value.
All of these are a bit useless when dealing with just 0 or 1.
Interestingly bitarray does implement all of these methods (and I had wondered why as they aren't all useful) but then doesn't actually register itself with the abc as supporting the interface! But at least it would be simple for bitstring to support these.
Perhaps we could also go above and beyond to make the methods more useful...
remove(value, start=None, end=None, count=1) -> int
Search for all sub-bitstrings equal to value from start to end and remove the first count of them.
I think this might just be a partial function of replace :)
pop(index=-1, length=1) Remove length bits that end at index and return them.
extend(iterable). With a binary container extend and append are pretty much the same thing in many ways. Not sure how to make this one more exciting.
index(value). If we allow value to be a sub-bitstring then this just becomes a find that raises a ValueError instead of returning -1. Still seems pointless - just use find instead.
The more I consider this the more pointless it seems. I think the answer is that we deliberately decide not to support those ABCs as sequences of just bits are a bit of a special case.
Would it be a good idea to support Sequence and MutableSequence ABCs? In general yes, as we are representing sequences, but the additional methods we'd need to support are all a bit pointless.
https://docs.python.org/3/library/collections.abc.html
Note that you don't need to subclass the ABCs you can just do
abc.Sequence.register(Bits)
but then you don't get the mixin methods for free.For Sequence:
index(value)
: Return index of first occurrence of value. As the only thing contained are zeros and ones this is not helpful.For MutableSequence:
extend(iterable)
: Append elements from iterable.pop(index)
: Remove and return item a index (default to end).remove(value)
: Remove first occurrence of value.All of these are a bit useless when dealing with just 0 or 1.
Interestingly
bitarray
does implement all of these methods (and I had wondered why as they aren't all useful) but then doesn't actually register itself with the abc as supporting the interface! But at least it would be simple for bitstring to support these.Perhaps we could also go above and beyond to make the methods more useful...
remove(value, start=None, end=None, count=1) -> int
Search for all sub-bitstrings equal to value from start to end and remove the first count of them.I think this might just be a partial function of
replace
:)pop(index=-1, length=1)
Remove length bits that end at index and return them.extend(iterable)
. With a binary container extend and append are pretty much the same thing in many ways. Not sure how to make this one more exciting.index(value)
. If we allow value to be a sub-bitstring then this just becomes afind
that raises a ValueError instead of returning -1. Still seems pointless - just use find instead.