Parse bitarray in python

265 views Asked by At

I am using bitarrays in one of my projects to store the bits(reading from a file which has "0"s and "1"s, and time information which is required. File can also have not necessary fields like comments etc.). Now I want to parse the bits. I filter out all the unnecessary things from the file while reading it. So I need a parser which can parse bits(bitarrays).

Currently, I am using parsimonious parser to parse the strings(i. e "0"s and "1"s), but seems like this parser just takes strings as an input. Is this the case for all parsers? Or can I write grammar rules which can match python bitarrays/bitstrings or list or any other non string data-structure?

So if I want to parse bits(bitarrays) what would be the best way to do it?

Example:

I have a string "011000111100011010" in a file. Since "0" in the string is a character, it takes 8 bits in memory. Since it is wastage of memory(because i need only a bit to store 0) i'm planning to store the bits in bitarrays.

Say, I want to match a frame(which is of length 18 bits) and they can be represented as following

frame(18bits) = field1(6bits) field2(2bits) field3(5bits) field4(5bits)

So how can I write a simple grammar rule which can match these bits.

This is how I'm doing this using parsimonious.(Here I consider a "0" as a character)

frame_matcher = field1 field2 field3 field4
field1 = ~"[01]{6}"
field2 = ~"[01]{2}"
field3 = ~"[01]{5}"
field4 = ~"[01]{5}"

This is just an example, in reality the scenarios is much more complex. And even the file size is too large(~1GB). So I am searching for a data-structure which can store bits(not characters) and a parser, which can parse bits(not characters) in python

0

There are 0 answers