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.1.tar.gz (6.9 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.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sequenceutils-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sequenceutils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 879fcfd752a036ee6bc3adf8986ad644f6e9c306fa8b341732e96c381d13ec2d
MD5 ba3a0fc6321538f7d32e6792fce05b67
BLAKE2b-256 5b570cfd796544b2907f6b1618789ed17fa9b4b8f4d7e9332cdc3e8a1263e4fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for sequenceutils-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: sequenceutils-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sequenceutils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 95da4a3224d0fd5118b537cf90ee5a83edecb9fec36860182e2b98012c7043bf
MD5 ae95c42ef7272568c09ab836ff3b3a71
BLAKE2b-256 6bd19afeadbd9204b85bca2cc14860770aced54027e292186a334d1782b8d5f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sequenceutils-0.1.1-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