There are too many ways to work with binary data in Python. There are ints of ones and zeros, ints of ASCII values, strings of ones and zeros, strings of binary decoded into text, bytes (b""), lists of ints representing ASCII codes, lists of ints representing ones and zeros, lists of bools representing ones and zeros, and probably 10 more I can't think of while I'm writing this. There are also lots of ways to decode them: format, str.encode, bytes.decode, int.to_bytes, int.from_bytes, bin, ord, chr, and f-strings. Blobfish fixes that by having one object for creating, concatenating, reading, and writing binary.
pip install git+https://github.com/jackr4bbit/blobfishthe main class for binary data.
(alias blobfish.B)
- Parameters:
- You can pass a
strof ones and zeros.blobfish.Binary("1101")
- You can pass another
blobfish.Binaryinstance.blobfish.Binary(blobfish.One)
- You can pass multiple
blobfish.Binaryinstances.blobfish.Binary(blobfish.One, blobfish.Zero, blobfish.One)(same asblobfish.One+blobfish.Zero+blobfish.One)
- You can pass nothing.
blobfish.Binary()
- You can pass a
- Operations (supported Python built-ins):
+: concatenation of twoblobfish.Binaryinstancesblobfish.One + blobfish.Zeroresults in ablobfish.Binaryinstance representing "10".
sum: the data of multipleblobfish.Binaryinstances concatenated together (similarly to+, but with a list).sum([blobfish.One, blobfish.Zero, blobfish.One])results in ablobfish.Binaryinstance representing "101".
*: repetition of ablobfish.Binaryinstanceblobfish.One * 3results in ablobfish.Binaryinstance representing "111".
==: equality comparison between twoblobfish.Binaryinstancesblobfish.One == blobfish.Oneresults inTrue.blobfish.One == blobfish.Zeroresults inFalse.
!=: inequality comparison between twoblobfish.Binaryinstancesblobfish.One != blobfish.Zeroresults inTrue.blobfish.One != blobfish.Oneresults inFalse.
str: string representation of the binary datastr(blobfish.One)results in"1".
len: length of the binary data in bits or bytes depending onbyteIndexes(see below).len(blobfish.OneByte)results in1ifblobfish.byteIndexes == True.len(blobfish.OneByte)results in8ifblobfish.byteIndexes == False.
repr: a constructor-style representation of theblobfish.Binaryinstance, showing its binary data. This should be used only for debugging purposes.repr(blobfish.OneByte)results in"Binary('11111111')".
a method to write the binary data to a file.
You must pass an instance of io.BufferedIOBase with writing capabilities (io.BufferedRandom or io.BufferedWriter), such as generated from open(filename, "wb") or open(filename, "rb+").
Tip
Writes are byte-aligned and trailing zero padding may be added automatically. If your data isn't multiple of 8 bits, the last byte will be padded with zeros to make it a full byte.
import blobfish
binary = blobfish.Binary("011000010110001001100011")
with open("file.bin", "wb") as file:
binary.write(file)a method to encode the binary data into a string or list of ints
You must pass int or str to specify the encoding type.
import blobfish
binary = blobfish.Binary("011000010110001001100011")
print(binary.encode(str)) # Output: 'abc'
print(binary.encode(int)) # Output: [97, 98, 99]a function to create a blobfish.Binary instance from bytes.
You must pass a bytes object.
blobfish.fromBytes(b"abc") creates a blobfish.Binary instance representing "011000010110001001100011".
a function to create a blobfish.Binary instance from a string.
(alias blobfish.fromStr)
- Parameters:
- You must pass a
strobject. - Optionally, you can specify what encoding to use as a string. The default is utf-8.
blobfish.fromString("abc")creates ablobfish.Binaryinstance representing "011000010110001001100011".blobfish.fromString("abc", encoding="utf-16")creates ablobfish.Binaryinstance representing "1111111111111110011000010000000001100010000000000110001100000000".
- You must pass a
a function to create a blobfish.Binary instance from the contents of a binary file.
You must pass an instance of io.BufferedIOBase with reading capabilities (io.BufferedRandom or io.BufferedReader), such as generated from open(filename, "rb") or open(filename, "wb+").
Caution
This function reads the entire file into memory, so it may not be suitable for large files.
import blobfish
with open("file.bin", "rb") as file:
binary = blobfish.fromFile(file)
print(type(binary)) # Output: <class 'blobfish.Binary'>blobfish.One(aliasblobfish.O): an instance ofblobfish.Binarywith a single bit of value1.blobfish.Zero(aliasblobfish.Z): an instance ofblobfish.Binarywith a single bit of value0.blobfish.OneByte(aliasblobfish.OB): an instance ofblobfish.Binarywith a single byte of value0xFF(11111111).blobfish.ZeroByte(aliasblobfish.ZB): an instance ofblobfish.Binarywith a single byte of value0x00(00000000).
The length of a blobfish.Binary instance will be in bits if that instance's byteIndexes attributes is False and in bytes if it's True. If the instance's byteIndexes attribute is None, it will use the value of the global variable blobfish.byteIndexes instead.
It also affects the behavior of getting indexes or slices of a blobfish.Binary instance: if the instance's byteIndexes is True (or it's None and the global byteIndexes is True), getting an index or slice will return a blobfish.Binary instance representing the corresponding byte(s), and if not, getting an index or slice will return a blobfish.Binary instance representing the corresponding bit(s).
By default, blobfish.byteIndexes is set to True and blobfish.Binary.byteIndexes is set to None.
In short: If you want to change the behavior for all blobfish.Binary instances, change the value of blobfish.byteIndexes. If you want to change the behavior for a specific instance, set that instance's byteIndexes attribute to True or False.
import blobfish
binary = blobfish.Binary("1111111100000000")
print(len(binary)) # Output: 2
print(binary[0]) # Output: Binary('11111111')
binary.byteIndexes = False
print(len(binary)) # Output: 16
print(binary[0]) # Output: Binary('1')