{"id":16646672,"url":"https://github.com/alexdelorenzo/buffer","last_synced_at":"2026-05-27T18:32:31.771Z","repository":{"id":71776048,"uuid":"303244227","full_name":"alexdelorenzo/buffer","owner":"alexdelorenzo","description":"⌛ A stream buffer backed by tempfile.SpooledTemporaryFile","archived":false,"fork":false,"pushed_at":"2021-12-24T02:16:19.000Z","size":77,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T09:37:27.514Z","etag":null,"topics":["buffer","python3","tempfile"],"latest_commit_sha":null,"homepage":"https://alexdelorenzo.dev","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexdelorenzo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["alexdelorenzo"]}},"created_at":"2020-10-12T01:07:56.000Z","updated_at":"2024-01-18T08:42:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"75782ab1-554b-42e1-adae-10d4c9e05c1b","html_url":"https://github.com/alexdelorenzo/buffer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/alexdelorenzo/buffer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Fbuffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Fbuffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Fbuffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Fbuffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexdelorenzo","download_url":"https://codeload.github.com/alexdelorenzo/buffer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Fbuffer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33579665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-27T02:00:06.184Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["buffer","python3","tempfile"],"created_at":"2024-10-12T08:42:46.619Z","updated_at":"2026-05-27T18:32:31.756Z","avatar_url":"https://github.com/alexdelorenzo.png","language":"Python","funding_links":["https://github.com/sponsors/alexdelorenzo"],"categories":[],"sub_categories":[],"readme":"# buffer\n\nA stream buffer backed by `tempfile.SpooledTemporaryFile`. Here's an [article that goes into more detail about `buffer`](https://alexdelorenzo.dev/programming/2019/04/14/buffer).\n\nThere's a [Rust version of this library](https://github.com/alexdelorenzo/buffers-rs), too.\n\n## About\nIn Python, you cannot `seek()` or `slice` into an iterable like you can with `list` and other ordered collections.\n\nSometimes streaming and asynchronous data transfers are modeled as iterables in Python. In the case of HTTP responses, the amount of streamed data can vary from a small amount to a relatively large amount.\n\nWhen you're working with a stream, you might want to go back in the stream, or skip ahead, without losing any streaming data in the process. To do that, you'd need a caching buffer.\n\nA buffer can exist in memory, or it can exist on persistent storage, or both. `buffer` does a mix of both. Using a set memory buffer limit, small streams can remain in memory, and longer streams can buffer on the disk. You can wrap an iterable with `buffer.StreamBuffer` and treat it like a persistent file, and not a stream.\n\nA `StreamBuffer` object is temporary, so when you're done using it, it will automatically clean up its memory and disk caches for you. \n\n# Example\n\n```python3\nfrom typing import Iterable\n\nfrom buffer import StreamBuffer\nfrom requests import get\n\n\nBIG_FILE: str = \"https://releases.ubuntu.com/20.04.1/ubuntu-20.04.1-desktop-amd64.iso\"\nLEN_KEY: str = 'Content-Length'\n\nKB: int = 1_024\nSTART: int = 0\n\n\nwith get(BIG_FILE, stream=True) as response:\n  length = int(response.headers[LEN_KEY])\n  stream: Iterable[bytes] = response.iter_content()\n\n  buffer = StreamBuffer(stream, length)\n\n  # read from start of stream\n  first_kb: bytes = buffer.read(START, size=KB)\n  \n  # skip ahead to any arbitrary location in the stream\n  arbitrary_kb = buffer.read(20 * KB, size=KB)\n  \n  # skip back to any arbitrary location in the stream\n  first_again = buffer.read(START, size=KB)\n  \n  assert first_again == first_kb\n```\n\n# Installation\n```bash\npython3 -m pip install buffer\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdelorenzo%2Fbuffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexdelorenzo%2Fbuffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdelorenzo%2Fbuffer/lists"}