Provides functions for generating ordering strings
Project description
Fractional Indexing
This is based on Implementing Fractional Indexing by David Greenspan .
Fractional indexing is a technique to create an ordering that can be used for Realtime Editing of Ordered Sequences.
This implementation includes variable-length integers, and the prepend/append optimization described in David's article.
Installation
$ pip install fractional-indexing
Usage
Generate a single key
from fractional_indexing import generate_key_between
# Insert at the beginning
first = generate_key_between(None, None)
assert first == 'a0'
# Insert after 1st
second = generate_key_between(first, None)
assert second == 'a1'
# Insert after 2nd
third = generate_key_between(second, None)
assert third == 'a2'
# Insert before 1st
zeroth = generate_key_between(None, first)
assert zeroth == 'Zz'
# Insert in between 2nd and 3rd — midpoint
second_and_half = generate_key_between(second, third)
assert second_and_half == 'a1V'
Generate multiple keys
from fractional_indexing import generate_n_keys_between
# Insert 3 at the beginning
keys = generate_n_keys_between(None, None, n=3)
assert keys == ['a0', 'a1', 'a2']
# Insert 3 after 1st
keys = generate_n_keys_between('a0', None, n=3)
assert keys == ['a1', 'a2', 'a3']
# Insert 3 before 1st
keys = generate_n_keys_between(None, 'a0', n=3)
assert keys == ['Zx', 'Zy', 'Zz']
# Insert 3 in between 2nd and 3rd — midpoint
keys = generate_n_keys_between('a1', 'a2', n=3)
assert keys == ['a1G', 'a1V', 'a1l']
Validate a key
from fractional_indexing import validate_order_key, FIError
validate_order_key('a0')
try:
validate_order_key('foo')
except FIError as e:
print(e) # fractional_indexing.FIError: invalid order key: foo
Use custom base digits
By default, this library uses Base62 character encoding. To use a different set of digits, pass them in as the digits
argument to generate_key_between()
, generate_n_keys_between()
, and validate_order_key()
:
from fractional_indexing import generate_key_between, generate_n_keys_between, validate_order_key
BASE_95_DIGITS = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
assert generate_key_between(None, None, digits=BASE_95_DIGITS) == 'a '
assert generate_key_between('a ', None, digits=BASE_95_DIGITS) == 'a!'
assert generate_key_between(None, 'a ', digits=BASE_95_DIGITS) == 'Z~'
assert generate_n_keys_between('a ', 'a!', n=3, digits=BASE_95_DIGITS) == ['a"', 'a#', 'a$']
validate_order_key('a ', digits=BASE_95_DIGITS)
Other Languages
This is a Python port of the original JavaScript implementation by @rocicorp. That means that this implementation is byte-for-byte compatible with:
Language | Repo |
---|---|
JavaScript | https://github.com/rocicorp/fractional-indexing |
Go | https://github.com/rocicorp/fracdex |
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
File details
Details for the file fractional-indexing-0.1.3.tar.gz
.
File metadata
- Download URL: fractional-indexing-0.1.3.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59e25f441c213bf7f921aa329a6bf3e5930ee8d8852df0b4e930aaf39a39ac5e |
|
MD5 | fb07f5c80aa31e951d617bed0bf75ee9 |
|
BLAKE2b-256 | 769e2c14b6dc812defa58e9d515b44b4d126b48338545e14089413df50dadda3 |
File details
Details for the file fractional_indexing-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: fractional_indexing-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 389abb9da24add388af3f0146ee4fcf2ebec5816ee728fa672657e394d9a7824 |
|
MD5 | a5192353b128579784e9b5821a434302 |
|
BLAKE2b-256 | 02d1627b86b1eb95ec2c7e012f0036e28a9335f3177f6c7e15047df6ee544bbc |