Package: bringbuf
bringbufis 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 atgithub.InstallRequirementsThere are no requirements.bringbufhas view dependencies (collections,itertoolsandwarnings), which are core modules.SetupInstallbringbufusing the following command:$ [sudo] pip install bringbufUsage#!/usr/bin/env python#coding=utf-8# The following example shows the basic usage of bRingBuf classfrombringbuf.bringbufimportbRingBuf# define a ring buffer with the size of 5 bytesbuf=bRingBuf(5)# the buffers length initially is zeroprint(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 bytesprint(buf.read(buf.len))# read and remove three bytes from the bufferprint(buf.dequeue(3))# read content of the whole buffer without removing any bytesprint(buf.read(buf.len))# enque four new bytesbuf.enqueue([0x07,0x08,0x09])# read content of the whole buffer without removing any bytesprint(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 checkprint(buf.contains([0x02,0x08]),buf.contains([0x07,0x08,0x09]))# index of patternsprint(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 offsetprint(buf.read(3,3))# enqueue a single bytebuf.enqueue_byte(0xff)print(buf.read(buf.len))# dequeue a single byteprint(buf.dequeue_byte())LicenseCopyright (c) 2016 Andreas GschossmannPermission 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.