Python: Trying To Split Instream Into Bit Sized Chunks
Currently I'm reading an instream and splicing it into 3 byte chunks:
instream_chunks = [instream[i:i+3]for i in range (0, len(instream), 3)]
What I'm failing to do is to split this instream into 22 bit sized chunks. Is there a way to do that in Python?
Edit: The instream is created (for test purposes) like this:
instream = open('C:/xxx/test.txt', 'rb+')
And this instream is then being used in this function
def write(self, instream: typ.BinaryIO):
Which starts with what I described above.
Answer
Assuming you have enough memory, you can convert the instream to a list of bits and then slice it however you want.
def access_bit(data, num):
base = int(num/8)
shift = num % 8
return (data[base] & (1<<shift)) >> shift
def to_bits(instream):
ba = bytearray(instream.read())
return [access_bit(ba,i) for i in range(len(ba)*8)]
>>>to_bits(open('test.txt','rb'))
[0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0]
Otherwise you'll have to read smaller chunks in multiple of size you want, and then use the above method on each. For example, you read 22*4 = 88 bits, or 11 bytes, and then call to_bits on that, split the resulting array into 4 22bit chunks, and repeat.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module