Skip to main content

A Python package for managing a sorted, fractionally-indexed set of COWList containing elements within a given alphabet.

Project description

sorted-fractionally-indexed-cowlist-set

A Python package for managing a sorted, fractionally-indexed set of COWList containing elements within a given alphabet.

Motivation

Many applications (real-time collaborative editors, distributed ledgers, dynamic sorted tables) require the ability to insert an unlimited number of unique items "in between" others in a totally ordered collection. This library brings this power to Python, modeled after the "fractional indexing" algorithms used in CRDTs and scalable databases.

Features

  • Infinite-density insertion: Always able to create new COWList between or after existing members (fractional indexing).
  • Efficient membership, indexing, and iteration.
  • Fully typed.
  • Supports Python 2 and 3.
  • No non-Python dependencies.

Perfect for collaborative editing, database indices, and any system requiring dynamic totally ordered unique keys.

Installation

pip install sorted-fractionally-indexed-cowlist-set

Usage

# coding=utf-8
from __future__ import print_function
from cowlist import COWList
from sorted_fractionally_indexed_cowlist_set import SortedFractionallyIndexedCOWListSet

BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

# Initialize with base58 (or your own custom alphabet)
s = SortedFractionallyIndexedCOWListSet(alphabet=BASE58_ALPHABET)

# Synthesize an initial COWList
initial = s.synthesize(index=0)

# Cannot reliably synthesize COWList smaller than all others
try:
    s.synthesize(index=0)
except ValueError:
    pass

# Add existing COWList
s.add(COWList('4fj8AiZ'))
s.add(COWList('X'))

# Edge: try to add an invalid COWList
# Notice uppercase I is NOT in BASE58!
try:
    s.add(COWList('I'))
except ValueError:
    pass

# Check membership
assert COWList('X') in s

# List all COWLists (iteration is in sorted order)
assert list(s) == [
    initial,
    COWList('4fj8AiZ'),
    COWList('X')
]

# Support for sequence protocol
assert len(s) == 3
assert s[0] == initial
assert s[1] == COWList('4fj8AiZ')

# Fractional insertion
# Insert a new COWList "between" the first and second elements
mid = s.synthesize(index=1)
assert list(s) == [
    initial,
    mid,
    COWList('4fj8AiZ'),
    COWList('X')
]

# You can insert at back
back = s.synthesize(index=len(s))
assert list(s) == [
    initial,
    mid,
    COWList('4fj8AiZ'),
    COWList('X'),
    back
]

# Reverse iteration (sorted high-to-low)
assert list(reversed(s)) == [
    back,
    COWList('X'),
    COWList('4fj8AiZ'),
    mid,
    initial
]

# Discarding COWLists
s.discard(COWList('4fj8AiZ'))
s.discard(COWList('notpresent'))

assert list(s) == [
    initial,
    mid,
    COWList('X'),
    back
]

# Slicing like a sequence
# Returns a List[COWList]
assert s[1:4] == [
    mid,
    COWList('X'),
    back
]

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under 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

Built Distribution

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

File details

Details for the file sorted_fractionally_indexed_cowlist_set-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for sorted_fractionally_indexed_cowlist_set-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 071cf6fe7473c67813105b4c0f6b8ab3bd779a22324511ce45866ab237adf215
MD5 fa5c70eb45626f8d9cf744ecc8151c08
BLAKE2b-256 aa57362bd8d04e915d771acbbe906f5e256ceb134b79b560e1c2ca86092a6dbe

See more details on using hashes here.

File details

Details for the file sorted_fractionally_indexed_cowlist_set-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sorted_fractionally_indexed_cowlist_set-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c9a80cdef62470451fef0343e94244e846e0d0c3b5416ab540643fed5fb10f97
MD5 6a51d77e52ce3b2da2ec7b8db2df959f
BLAKE2b-256 c1dfb92601ea1dba4fea905ff5dcdd92a4bf386f1338cccc46ecadc8bdcee281

See more details on using hashes here.

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