A simple circular buffer to handle byte streams (for instance from a serial port), heavily based on enque.
Project description
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.
Install
Requirements
There are no requirements. bringbuf has view dependencies (collections, itertools and warnings), which are core modules.
Setup
Install bringbuf using the following command:
$ [sudo] pip install bringbuf
Usage
#!/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())
License
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bringbuf-0.1.2.tar.gz
.
File metadata
- Download URL: bringbuf-0.1.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 134fb386f02ad39d8b850b836b2b4fe833e00faaec916d4b07cab8f4743f0ee6 |
|
MD5 | b9cd1979150b0e780fed2111faa674e6 |
|
BLAKE2b-256 | d8ac6fce3c3a3afdac13c5fa819b11c6476658f7f831190820860773667f6365 |
File details
Details for the file bringbuf-0.1.2-py2-none-any.whl
.
File metadata
- Download URL: bringbuf-0.1.2-py2-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 070491565fbd4e4347b29cadb54dda057081a95c20f90e4f492f327a2a9db9d8 |
|
MD5 | fcbe905ca2a2372fc18f0c2ce875bbc1 |
|
BLAKE2b-256 | ad1fee127a8551e9fe94d2b25d421c686c9a98c6d450e491840a02fd0ed70f8a |