Get Best Python Programming Assignment with Huffman

Python Programming Assignment HelpCreate a file bitstream.py with the following contents.

class Bitstream():

def __init__(self, filename : str, mode : str):

self.filename = filename

self.mode = mode + ‘b’

self.open_file = None

self.index = 0

self.mask = 128

self.data = None

self.bytes = bytearray(4096)

def __enter__(self):

self.open_file = open(self.filename, self.mode)

if self.mode == “rb”:

self.data = self.open_file.read(4096)

return self

def __exit__(self, exc_type, exc_val, exc_tb):

if self.mode == “wb”:

if self.mask != 128:

self.index += 1

self.open_file.write(self.bytes[:self.index])

self.open_file.close()

def __bool__(self):

if self.data and self.index < len(self.data):

return True

return False

def readbit(self):

value = self.data[self.index] & self.mask != 0

self.mask >>= 1

if not self.mask:

self.mask = 128

self.index += 1

if self.index == 4096:

self.data = self.open_file.read(4096)

self.index = 0

return value

def read(self, size : int):

value = 0

mask = 1 << size

for _ in range(size):

mask >>= 1

if self.readbit():

value |= mask

return value

def writebit(self, bit : int):

if bit:

self.bytes[self.index] |= self.mask

self.mask >>= 1

if not self.mask:

self.mask = 128

self.index += 1

if self.index == 4096:

self.open_file.write(self.bytes)

self.index = 0

self.bytes[self.index] = 0

def write(self, value : int, size : int):

if value.bit_length() > size:

raise IOError(“Attempt to write {value} to field of size {size}”.format(**locals()))

mask = 1 << size – 1

while mask:

self.writebit(value & mask)

mask >>= 1

You use it as as context manager like a normal file, but you can write either a single bit, or an integer with a fixed number of bits.

with Bitstream(“bitstream.dat”, “w”) as f:

for value in range(4096):

f.write(value, 12)

When reading you can either read based on a particular file format, or just use if to determine if the file has reached the end.

with Bitstream(“bitstream.dat”, “r”) as f:

while f:

print(f.read(12))

We will be using the library to finish off the Huffman compression, as it will be responsible for reading and writing the data. This library abstracts the details of writing to a file, and uses an internal 4k buffer to avoid multiple reads or writes.

If you are looking for help with any Python project, we can help providing documented library code that can be reused in future projects. One of the advantages of Python over other programming languages is the ease of extending code, which you will see when we incorporate this library that we just wrote into the previous code for Huffman compression and decompression. We can help you with assignments ranging from the simple introductory level homeworks, all the way up to the advanced end of year projects that incorporate all of the elements you have covered in your course. We don’t just do Python, but almost every programming language in common use and even ones that are out of date (some assembly languages courses cover the older processors, or older Dos versions as they are simpler). We can even provide non programming assignments such as essays in related fields.

You can rely on Programming Homework Assignment to provide any Python programming homework help you may have.