Skip to content

rbroderi/StringDataDeque

Repository files navigation

StringDataDeque

Generic badge Code style: black Generic badge Generic badge Generic badge Generic badge Dynamic TOML Badge

Useful when building a string from data that can be converted into a string, in parts.

Layout

  • src/stringdatadeque/ – The pure-Python package containing the public API and implementation.
  • examples/ – Small scripts that show common usage patterns.
  • tests/ – Integration and regression tests that guard the API surface.

import stringdatadeque exposes a USING_PURE_PYTHON flag for backwards compatibility. It is always set to True now that the package no longer ships a native extension.

Installation

https://pypi.org/project/StringDataDeque/

pip install StringDataDeque

Local development

This project is pure Python, so the usual editable install flow works everywhere:

python -m venv .venv
.venv\Scripts\activate  # PowerShell: .venv\Scripts\Activate.ps1
pip install -e .[dev,optional,docs]

To build a distributable wheel:

uv run python -m build

Uses

This is designed to be a drop-in replacement for when you might want to append to a string in a loop.

Benefits

  • Around 5 times faster than the naive implementation of appending to a string, such as
    x = ""
    for x in collection:
        x+="new string"
  • Provides many extra features that help simply code.

Examples

sd = StringDeque(sep="\n")
for x in collection:
    sd += x
# StringDeque is a specialization of StringDataDeque where conversion func is "str"
# this allows any datatype to be used which can convert to str
sd += 1
print(sd)

You can also pipe data into the StringDeque

sd = StringDeque()
sd = [1,2,3,4,5] | sd
# or
sd |= [1,2,3,4,5]

StringDataDeque implements the "contains" method so you can search within it

sd = StringDeque(["line_one","line_two"],sep="\n")
if "line_one" in sd:
    print("yes")

If you need more control over how data is added to the deque either use StringDataDeque or one of its subclasses.

# convert_func is called when data is added, and format_func is called when data is printed.
int_sdd =StringDataDeque(data="test", convert_func=int, format_func=str,sep=" ")
int_sdd |= ["1","2","3","4","5"]
assert int_sdd[0] == 1
assert str(int_sdd) == "1 2 3 4 5"

About

faster more useful replacement to string appends

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •