Skip to main content
  • 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().

Release files for pycircularbuffer 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycircularbuffer 0.0.9
File Size Uploaded
pycircularbuffer-0.0.9.tar.gz 11.4 kB Details

Release files / pycircularbuffer-0.0.9.tar.gz

Download URL pycircularbuffer-0.0.9.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
189bc8793a8e9ec6de83332a5480613c63d48a5faf73235d75669aafd00504dc
BLAKE2b-256 checksum
How to use checksums
0012d37a1cb39f2388ba0d75b7a7b3f4144988605cf570bed8ddef7b810f56ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.0.9 This release

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page