Giter Club home page Giter Club logo

byte-fifo's Introduction

BytesFIFO

Introduction

This module contains exactly one class, BytesFIFO. The purpose of BytesFIFO is to provide a stream-like interface to a fixed-size byte-oriented FIFO in Python. This should be significantly faster than using a deque, list, or manually manipulating str/bytearray/bytes.

The API only accepts bytes and bytearray, and will read data back as bytes. It is a non-blocking stream.

Performance

Preliminary performance analysis shows that Python 2.7 is about 25% faster than Python 3.4 when reading and writing large chunks of data to the FIFO.

Examples

Creating a FIFO

A FIFO is created with a fixed size. The internal buffer is completely pre-allocated.

# Create a 5 kB FIFO
f = fifo.BytesFIFO(5*1024)

Filling the FIFO

f.write(b"Here's some data")
f.write(b"More data!")
# Consume all data
d = f.read(len(f))

Querying the FIFO state

f.full()     # Is the FIFO completely filled?
f.empty()    # Is the FIFO completely empty?
f.free()     # How much data is left in the FIFO?
f.capacity() # How much data can the FIFO hold?
len(f)       # How much data is filled in the FIFO?
bool(f)      # Is the FIFO non-empty?

Resizing

The FIFO may be expanded or contracted. All data is retained after the resize operation, but a copy operation may be occur. ValueError is raised if the resize operation would cause data loss.

f = fifo.BytesFIFO(10)
f.write(b"Testing")
f.resize(20)
# returns b"Testing"
f.read(len(f))

Writing more data than allocated

f = fifo.BytesFIFO(5)
# Only b"Lorem" is written, since the FIFO depth is 5 bytes.
bytes_written = f.write(b"Lorem ipsum dolor sit amet, consectetur adipiscing elit")
data = f.read(5)

byte-fifo's People

Contributors

hbock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

byte-fifo's Issues

License

Could you please add a License?

Buffer Overflow

Hey,
I am trying to implement your class on realtime binary data, The problem is that data written is greater than data being read, Which eventually will cause buffer-overflow. Can you help me with it?

Thread-safe?

Is the implementation thread-safe? There doesn't seem to be a test for thread-safety either.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.