Skip to main content

No project description provided

Project description

sequenceutils

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install sequenceutils

Usage

ComputedSequence

ComputedSequence can be used to create a sequence from a function and a given length. Upon indexing, the function is called to compute the value at the given index.

from sequenceutils import ComputedSequence

# Create a sequence from a function and a given length
seq = ComputedSequence(lambda i: i + 1, length=10)

# Access elements from the sequence
print(seq[0])  # 1
print(seq[5])  # 6
print(seq[-1])  # 10

# Slicing is fully supported
print(list(seq[1:4]))  # [2, 3, 4]
print(list(seq[::2]))  # [1, 3, 5, 7, 9]

After turning a function (over the non-negative integers) into a ComputedSequence, you can use any method that operates on a Sequence. For example, this makes it easy to implement the inverse of a monotonically increasing function using bisect:

from sequenceutils import ComputedSequence
import bisect

def floor_root(y):
    """Computes the floor of the square root of y (up to 10^9)"""
    seq = ComputedSequence(lambda x: x * x, length=10**9)
    return bisect.bisect_right(seq, y) - 1

print(floor_root(4))  # 2
print(floor_root(5))  # 2
print(floor_root(16))  # 4

Item assignment is not supported.

ConcatenatedSequence

With ConcatenatedSequence, you can create a single sequence from multiple sequences. Rather than creating a new, larger container in memory, ConcatenatedSequence determines which sequence to index based on the index of the requested element. Thus, this class is suitable for creating (very) large sequences in situations where doing so in memory is impractical.

from sequenceutils import ConcatenatedSequence

# Create a sequence from multiple sequences
seq = ConcatenatedSequence([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Access elements from the concatenated sequence
print(seq[0])  # 1
print(seq[5])  # 6
print(seq[-1])  # 9

# Slicing is fully supported
print(seq[1:4])  # [2, 3, 4]
print(seq[::2])  # [1, 3, 5, 7, 9]

In conjunction with ComputedSequence, this can be used to create the concatenation of lazily computed sequences:

from sequenceutils import ConcatenatedSequence, ComputedSequence

seq = ConcatenatedSequence([
    ComputedSequence(lambda i: i + 1, length=5),
    ComputedSequence(lambda i: i + 10, length=5)
])

print(list(seq))  # [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]

Like with ComputedSequence, item assignment is not supported.

License

sequenceutils is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sequenceutils-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sequenceutils-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file sequenceutils-0.1.0.tar.gz.

File metadata

  • Download URL: sequenceutils-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for sequenceutils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0151907f6b9987c9a1a5436e8d84c35040e4b6d6cdd355a691c64c76dc2c613d
MD5 c546776b93a9e42bd8a03bb1368b13e2
BLAKE2b-256 154696bf80538c526e5b507e8241536a9485ad2e6f0c394ca1ce09a9504fb8ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for sequenceutils-0.1.0.tar.gz:

Publisher: release.yml on brandjas/sequenceutils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sequenceutils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sequenceutils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for sequenceutils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e602fd24b1bade48cec507c2f296671b633de74408ef5fd9e82c7a1bc8305e1
MD5 c54258b964d6ec21d1cd406cac7dbfbc
BLAKE2b-256 3552174aad315753b93259c45187ed6ff2e05cc7bd5b6257c43cf273c2f17500

See more details on using hashes here.

Provenance

The following attestation bundles were made for sequenceutils-0.1.0-py3-none-any.whl:

Publisher: release.yml on brandjas/sequenceutils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page