Byte data type (Visual Basic)
4 stars based on
Enter search terms or a module, class or function name. These methods have been introduced in Python 3. If you ask for the hex, octal or binary representations of a bitstring then they simpler initialisation from binary and hexadecimal no longer be prefixed with 0x0o or 0b. This was done as it was noticed that the first thing a lot of user code does after getting these representations was to cut off the first two characters before further processing.
Previously this would have returned '0x', '0o', '0b'. Previously Bits was an alias for ConstBitStream for backward compatibility. This has now changed so that Bits and BitArray loosely correspond to the built-in types bytes and bytearray. No one seemed to use it though and so it has now reverted to the convential meaning for containers. If simpler initialisation from binary and hexadecimal are using step then recoding is simple: This method is a mix between a find and a read - it searches for a bitstring and then reads up to and including it.
Previously only a string was accepted as the format in the pack function. This was an oversight as it broke the symmetry between pack and unpack. Now you can use formats like this:. New bit interpretations for interleaved exponential-Golomb as used in the Dirac video codec are supplied via uie and sie:. These are pretty similar to the non-interleaved versions - see the manual for more details.
Credit goes to Paul Sargent for the patch. A number of methods take a bytealigned parameter to indicate that they should only work on byte boundaries e. Previously this parameter defaulted to False. Instead it now defaults to bitstring. Previously there were just two classes, the immutable Bits which was the base class for the mutable BitString class.
Both of these classes have the concept of a bit position, from which reads etc. As you can guess the former is an immutable version of the latter. The other classes have also been renamed to better reflect their capabilities.
The old names are kept as aliases for backward compatibility. Some things you might see are:. This is a major release, with a number of backwardly incompatible changes. The main change is the removal of many methods, all of which have simple alternatives.
Other changes are quite minor but may need some recoding. There are a few new features, most of which have been made to help the stream-lining of the API. As always there are performance improvements and some API changes were made purely with future performance in mind. About half of the class methods have been removed from the API. They all have simple alternatives, so what remains is more powerful and easier to remember. The removed methods are listed here on the left, with their equivalent replacements on the right:.
Many of these methods have been deprecated for the last few releases, but there are some new removals too. Any recoding needed should be quite straightforward, so while I apologise for the hassle, I had to take the opportunity to streamline and rationalise what was becoming a bit of an overblown API.
The all and any methods previously called allsetallunsetanyset and anyunset no longer accept a single bit position. The recommended way of testing a single bit is just to index it, for example instead of:. If you really want to you can of course use an iterable with a single element, such as s. If a read or peek goes beyond the end of the bitstring then a ReadError will be raised. The previous behaviour was that the rest of the bitstring would be returned and no exception raised.
The base class for errors in the bitstring module is now simpler initialisation from binary and hexadecimal Errorso it will likely appears in your code as bitstring.
Error instead of the rather repetitive bitstring. A single index slice such as s will now return a bool i. True or False rather than a single bit bitstring. This is partly to reflect the style of the bytearray type, which returns an integer for single items, but mostly to avoid common errors like:.
While the intent of this code snippet is quite clear i. Under the new rule s is True if s starts with a 1 bit and False if s starts with a 0 bit.
The change does not affect reads and peeks, so s. Previously a bitstring was False if it had no elements, otherwise it was True. The new behaviour means that the bitstring is False if it has no 1 bits. This means that code like this:. It also means that BitsBits 0x00 and Bits 'uint: If you need to check for the emptiness of a bitstring then instead check the len property:.
This has now been disallowed, and the offset and length parameters may only be used when initialising with bytes or a file. To replace the old behaviour you could instead use. Methods with a format parameter have had it renamed to fmtto prevent hiding the built-in format. Affects methods unpackreadpeekreadlistpeeklist and byteswap and the pack function. This means that for the affected methods unpackreadlist and peeklist you will need to use an iterable to specify multiple items.
This simpler initialisation from binary and hexadecimal easier to show than to describe, so instead of. Note that you could still use the single string 'uint: You can no longer use True and False to initialise single bit bitstrings. The reasoning behind this is simpler initialisation from binary and hexadecimal as bool is a subclass of int, it really is bad practice to have Bits False be different to Bits 0 and to have Bits True different to Bits 1.
If you have used bool auto-initialisation then you simpler initialisation from binary and hexadecimal have to be careful to replace it as the bools will simpler initialisation from binary and hexadecimal be interpreted as ints, so Bits False will be empty a bitstring of length simpler initialisation from binary and hexadecimaland Bits True will be a single zero bit a bitstring of length 1.
Sorry for the confusion, but I think this will prevent bigger problems in the future. There are a few alternatives for creating a single bit bitstring. My favourite is to use a list with a single item:. Previously if you created a bitstring from a file, either by auto-initialising with a file object or using the filename parameter, the file would not be read into memory unless you tried to modify it, at which point the whole file would be read. The new behaviour depends on whether you create a Bits or simpler initialisation from binary and hexadecimal BitString from the file.
If you create a Bits which is immutable then the file will never be read into memory. This allows very large files to be opened for examination even if they could simpler initialisation from binary and hexadecimal fit in memory. If however you create a BitString, the whole of the referenced file will be read to store in memory. If the file is very big this could take a long time, or fail, but the idea is that in saying you want the mutable BitString you are implicitly saying that you want to make changes and so for now we need to load it into memory.
The new strategy is a bit more predictable in terms of performance than the old. Just to be clear, in neither case will the contents of the file ever be changed - if you want to output the modified BitString then use the tofile method, for example. If a find is unsuccessful then an simpler initialisation from binary and hexadecimal tuple is returned which is False in a boolean sense otherwise a single item tuple with the bit position is returned which is True in a boolean sense.
This has allowed a number of methods to be removed from this release, so for example instead of:. The byteswap method now allows a format specifier of 0 the default to signify that all of the whole bytes should be reversed. For this reason this method should be used with some caution as it will make you code behave differently with the different major Python versions. As well as renaming BitStringError to just Error there are also new exceptions which use Error as a base class. These can be caught in preference to Error if you need finer control.
The simpler initialisation from binary and hexadecimal exceptions sometimes also derive from built-in exceptions:. Changes the endianness in-place according to a format string or integer s giving the byte pattern.
See the manual for simpler initialisation from binary and hexadecimal. The simpler initialisation from binary and hexadecimal and end parameters of many methods may now be negative, with the same meaning as for negative slice indices. Affects all methods with these parameters. The Bits class now derives from collections. Sequencewhile the BitString class derives from collections.
This is technically a backward incompatibility in the unlikely event that you are referring to the parameter by name.
This should have no effect, except that this is a small backward incompatibility if you use positional arguments when initialising with anything other than auto which would be rather unusual. The Bits class is the base class for the mutable BitString. The differences between Bits and BitStrings are:. The relationship between Bits and BitString is supposed to loosely mirror that between bytes and bytearray in Python 3.
A number of methods have been flagged for removal in version 2. Deprecation warnings will now be given, which simpler initialisation from binary and hexadecimal an alternative way to do the same thing.
All of the deprecated methods have simpler equivalent alternatives. The deprecated methods are: A possible problem outputting the final byte in tofile was fixed.
This version hopefully fixes an installation problem for platforms with case-sensitive file systems. There are no new features or other bug fixes.