Skip to main content

Simple implementation of circular buffer

Project description

  • License : MIT

Summary

Simple circular buffer written in python extension.

CircularBuffer will allocate requested size + 2 bytes for two sections of null terminated strings.

May temporary allocate another half of the allocated bytes if you used buffer protocol, I mostly used them for regex.

Installation

Simply pip install pycircularbuffer, compilation from downloaded source is probably needed.

Using

from circularbuffer import CircularBuffer

buf = CircularBuffer(1024)

buf.write(b'some text')
while len(buf) > 0:
    buf.read(1)

from re import match

buf.write(b'hallo')

# python2
with buf:
    match_found = match(br'^ha', buf)

# python3
match_found = match(br'^ha', buf)

# use `result` immediately because regex didn't make memory copy of the
# internal buffer, or run another `match()` on a memory copy, for example:

match_found_str = buf.read(len(match_found.group(0)))
independent_match_found = match(br'^ha', match_found_str)

Warning

Don’t to this in python 3: ‘a’ in buf, instead: b’a’ in buf.

Check the return value of write() which is the actual length that was written into the buffer and also the exception that might be raised.

API

Regular methods:

  • clear()

  • read()

  • resize()

  • write()

  • write_available()

  • make_contiguous()

String methods:

  • count()

  • startswith()

  • find()

  • index()

Sequence methods:

  • __contains__()

  • __getitem__()

  • __len__()

  • __setitem__()

Magic methods:

  • __repr__()

  • __str__()

    Note: while string representation makes thing easier it always creates memory copy.

  • __enter__()

  • __exit__()

Buffer protocol:

Note: for python version < 3, you need to use context manager, the with statement, to let CircularBuffer know when you are releasing the buffer.

Using buffer protocol will throw ReservedError exception for CircularBuffer.read().

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycircularbuffer-0.0.9.tar.gz (11.4 kB view details)

Uploaded Source

File details

Details for the file pycircularbuffer-0.0.9.tar.gz.

File metadata

  • Download URL: pycircularbuffer-0.0.9.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for pycircularbuffer-0.0.9.tar.gz
Algorithm Hash digest
SHA256 189bc8793a8e9ec6de83332a5480613c63d48a5faf73235d75669aafd00504dc
MD5 7d3a8dd902e1a1ccb935f858510cf5a9
BLAKE2b-256 0012d37a1cb39f2388ba0d75b7a7b3f4144988605cf570bed8ddef7b810f56ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page