A simple circular buffer to handle byte streams (for instance from a serial port), heavily based on enque.
bringbuf is very basic circular buffer implementation. Its purpose is to handle byte streams (for instance from a serial port). It is based on enque, which is a efficient way to handle queues in Python. First bytes written to the buffer, are first bytes read from the buffer. If the buffer is full it overflows and overwrites the oldest data.
The code is available at github.
There are no requirements. bringbuf has view dependencies (collections, itertools and warnings), which are core modules.
Install bringbuf using the following command:
$ [sudo] pip install bringbuf
#!/usr/bin/env python #coding=utf-8 # The following example shows the basic usage of bRingBuf class from bringbuf.bringbuf import bRingBuf # define a ring buffer with the size of 5 bytes buf = bRingBuf(5) # the buffers length initially is zero print(buf.len) # check if buffer is empty (True) print(buf.is_empty()) # enqueue 6 bytes to the buffer # (note that the buffer will overflow) buf.enqueue([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) # now five bytes are in the buffer (maximal number of bytes) print(buf.len) # check if buffer is empty (False) print(buf.is_empty()) # read content of the whole buffer without removing any bytes print(buf.read(buf.len)) # read and remove three bytes from the buffer print(buf.dequeue(3)) # read content of the whole buffer without removing any bytes print(buf.read(buf.len)) # enque four new bytes buf.enqueue([0x07, 0x08, 0x09]) # read content of the whole buffer without removing any bytes print(buf.read(buf.len)) # check whether buffer contains a pattern? # this might be solved by buf.index, which also returns the index, # but this is a more descriptive way to check print(buf.contains([0x02, 0x08]), buf.contains([0x07, 0x08, 0x09])) # index of patterns print(buf.index([0x02, 0x08]), buf.index([0x07, 0x08, 0x09])) # contains pattern (with offset)? print(buf.contains([0x02, 0x08], 2), buf.contains([0x07, 0x08, 0x09], offset=2)) # index of patterns (with offset) print(buf.index([0x02, 0x08], 2), buf.index([0x07, 0x08, 0x09], 2)) # read with offset print(buf.read(3, 3)) # enqueue a single byte buf.enqueue_byte(0xff) print(buf.read(buf.len)) # dequeue a single byte print(buf.dequeue_byte())
Copyright (c) 2016 Andreas Gschossmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size bringbuf-0.1.2-py2-none-any.whl (6.9 kB)||File type Wheel||Python version py2||Upload date||Hashes View|
|Filename, size bringbuf-0.1.2.tar.gz (4.4 kB)||File type Source||Python version None||Upload date||Hashes View|