Skip to main content

Package Description

CI Documentation Status

This is a Python package that provides a simple class Pushable that creates "pushable" iterators by wrapping an inner iterator/iterable. Pushable iterators act like dynamically expanding queues, allowing you to peek ahead or push items back onto the queue.

Basic Usage

We can turn any iterable/iterator into a pushable iterator using the constructor.

count_up = Pushable( range( 0, 5 ) )

We can use it like an ordinary iterator:

print( next( count_up ) )
# Prints 0

Or we can look-ahead to see what is coming:

whats_up_next = count_up.peek()
print( whats_up_next )
# Print 1
print( next( count_up ) )
# Also prints 1 because peek does not remove the item from the internal queue.

We can even push back items onto it:

count_up.push("cat")
count_up.push("dog")
print( list( count_up ) )
# Prints 'dog', 'cat', 2, 3, 4

Examples

From an iterator such as a file-object, which will iterate over the lines in a file, create a peekable/pushable iterator. This can be useful for example when we want to know if the iterator still has contents or want a sneak peek at what is coming.

from pushable import Pushable

def read_upto_two_blank_lines( filename ):
    with open( filename ) as file:
        plines = Pushable( file )
        # Pushable iterators can be used as booleans in the natural way.
        while plines:
            line = next( plines )
            # peekOr makes it safe to look ahead.
            if line == '\n' and plines.peekOr() == '\n':
                # Two blank lines encountered.
                break
            else:
                yield line        

It is also useful to perform "macro-like" transformation.

from pushable import Pushable

def translate( text, translations ):
    ptokens = Pushable( text.split() )
    while ptokens:
        token = next(ptokens)
        if token in translations:
            ptokens.multiPush( *translations[token].split() )
        else:
            yield token

print( ' '.join( translate( 'My name is MYNAME', {'MYNAME':'Fred Bloggs'} ) ) ) 
# Prints: My name is Fred Bloggs

More Complex Uses

In addition to peeking and popping items, which risks raising a StopIteration exception if there's nothing left on the internal queue, we can utilise peekOr and popOr to deliver a default value instead. The default value is passed as an optional parameter and falls back to None.

We can also peek and pop multiple values using multiPeekOr and multiPopOr, which return generators. These support skipping over values so that you can get the 2nd and 3rd value without getting the first e.g.

(second, third) = Pushable("pqr").multiPop(skip=1, count=2)
print( second, third )
# Prints: q r

Lastly, we can push multiple items with multiPush:

count_up.multiPush("cat", "dog", "rabbit")
print( list( count_up ) )
# Prints: ['cat', 'dog', 'rabbit']

For a full set of available methods see the documentation.

Metadata

Release files for pushable 0.2.1

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

Source distribution (sdist)

Source distribution for pushable 0.2.1
File Size Uploaded Provenance Actions
pushable-0.2.1.tar.gz 54.3 kB Without provenance Details for pushable-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pushable 0.2.1
File Interpreter ABI Platform Actions
pushable-0.2.1-py3-none-any.whl Python 3 none any Details for pushable-0.2.1-py3-none-any.whl

Total release size: 71.4 kB

Release files / pushable-0.2.1.tar.gz

Download URL pushable-0.2.1.tar.gz
Size 54.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9943d0a9f2d682efc42d4137c733e81b1e3d72db9e068fad7f9f2877d36beb1d
BLAKE2b-256 checksum
How to use checksums
942a7642ab18db8fd47fe527ce6d209163bba3aa5a7f54e6bfc17a4c09ecb7dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pushable-0.2.1-py3-none-any.whl

Download URL pushable-0.2.1-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a7ef8d3c0a1a7786e7cd43a2d5776288ff1f0eba4322c3e324c3aa95ae239a9e
BLAKE2b-256 checksum
How to use checksums
5deec76117879b6ffbe2dfb7910449ee1501135c6736f4a7daafbe2ff7a966ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for pushable 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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