一种简单的循环缓冲区,用于处理字节流(例如从串行端口),主要基于enque。
bringbuf的Python项目详细描述
bringbuf是非常基本的循环缓冲实现。其目的是处理字节流(例如从串行端口)。它基于enque,这是一种在python中处理队列的有效方法。写入缓冲区的第一个字节,是从缓冲区读取的第一个字节。如果缓冲区已满,则溢出并覆盖最旧的数据。
代码可在github找到。
安装
要求
没有要求。bringbuf具有视图依赖项(collections,itertools和warnings),它们是核心模块。
设置
使用以下命令安装bringbuf:
$ [sudo] pip install bringbuf
用法
#!/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())
许可证
版权所有(c)2016 Andreas GSchossmann
兹免费准许任何人取得 本软件和相关文档文件(“软件”),用于处理 不受限制的软件,包括但不限于 使用、复制、修改、合并、发布、分发、再授权和/或销售副本 以及允许向其提供软件的人员 因此,必须满足以下条件:
上述版权公告及本许可公告须包括在 软件的拷贝或大部分。
本软件按“原样”提供,无任何形式的保证,明示或 默示的,包括但不限于适销性保证, 适合特定目的和不侵权。在任何情况下 作者或版权所有者应对任何索赔、损害或其他 责任,无论是在合同诉讼、侵权诉讼或其他诉讼中, 不属于或与本软件有关,或使用或与本软件的其他交易有关。 软件。