A lexicographical ranking system for python
Project description
Plexorank
Plexorank is a python implementation of a lexicographical ranking system.
Find on / install from PyPI
A perfect back-end pair for a front-end user based sorting preference, such as drag and drop operations, Plexorank was inspired by Jira's Lexorank.
Why though?
When a user drags an item before or after another item and expects that order to be maintained, we'll need some sort of storage mechanism to ensure persistent order, even after the user logs out. We'll also need something performant enough to allow a user to drag and drop rapidly without triggering loads of back-end computation.
Enter Plexorank - when a user manually alters the order of a list or table, a new rank must be calculated. Using alphabetical rather than numerical sorting, we're able to perform only a single operation on the entity that was moved. To do this, when an item is moved in an order, the front end needs to send three pieces of information: the ids of the previous_item
, the current_item
, and the next_item
. Previous and next may be ignored if an item is moved to the top or bottom of a list. We send ids rather than ranks themselves to ensure eventual consistency.
On the back end, the ids of the previous_item
and next_item
will be used to fetch those corresponding ranks and calculate the mean rank between the two.
- if
previous_item
isnull
or unsupplied, we'll decrement the rank from thenext_item
- if
next_item
isnull
or unsupplied, we'll increment the rank from theprevious_item
- if both
previous_item
andnext_item
arenull
or unsupplied, we'll assign an initial rank, as this is the only item in the list.
The Math
Plexorank uses a base-26 cipher:
- Convert a string rank to a list of integers
- Calculate a base-10 numerical value from the list of integers
- Perform mathematical operations to increment or decrement a rank, or find the mean of two ranks
- Split the new base-10 numerical value back out into a list of integers
- Convert the list of integers back to a string rank
What about conflicts?
Plexorank uses what I call "n-tacking" to solve conflicts. If a new rank conflicts with another rank in the same subset of entities, simply addind the letter "n" to the end of the new rank solves the problem in two wonderful ways:
- First, thanks to sorting alphabetically using strings rather than sorting numbers, tacking a letter on to the end of a rank allows us to add a new layer of sorting possibilities that maintains the relative order of the existing ranks. For instance, if you needed a rank between
aaaa
andaaab
, we simpy take the upper rankaaaa
and tack on an "n" to getaaaan
, which sits squarely betweenaaaa
andaaab
. - Second, the letter "n" sits snug in the middle of the alphabet. We've not only solved the conflict, but we've provided 13 slots between
aaaa
andaaaan
and 12 slots betweenaaaan
andaaab
before we'll need to tack on another "n"
Does it scale?
Yeah, pretty well. Let's say you've allocated the rank
column of your database to allow for strings up to 255 characters. That means you have 26^255 ranks available to you. If you need more or start to hit a "hot spot", you can simply rebalance the table by reassigning initial ranks. This will maintain the order of the list while essentially "restarting" the ranking system with nice low-length ranks.
Usage
create_bulk_ranks
takes a single integer - the number of ranks you need - and returns a list of string ranks, evenly spaced from bbbbbb
to ffffff
.
create_mean_rank
takes two string ranks (e.g. "abgkskjhg"
and "ajdhfjhrt"
) and returns a new rank squarely in the middle (e.g. "afevywvmm"
)
increment_rank
takes a single string rank (e.g. "aacbd"
) and returns a new rank incremented by a single value of the "second" position of the rank (in this case, b -> c) (e.g. "aaccd"
)
- "carrying" is implemented, i.e.
"aaza"
returns"abaa"
decrement_rank
takes a single string rank (e.g. "aacbd"
) and returns a new rank decremented by a single value of the "second" position of the rank (in this case, b -> a) (e.g. "aacad"
)
- "carrying" is implemented, i.e.
"acaa"
returns"abza"
Project details
Release history Release notifications | RSS feed
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 plexorank-0.0.9.tar.gz
.
File metadata
- Download URL: plexorank-0.0.9.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6247cb774d899afb91d98842456ea36e71f369dd7c0750e33df5c3e39fbd6b8 |
|
MD5 | ffd9d0d57d1f60ae123114e541878189 |
|
BLAKE2b-256 | 38bb9e7429bc99f65f560019d1408302b6fa817238e6e61e89e8f226dfaa9299 |
File details
Details for the file plexorank-0.0.9-py3-none-any.whl
.
File metadata
- Download URL: plexorank-0.0.9-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d83b5aafb1cb779aa260a0ea1c89cf5e6c2c70f8b6d4b983e3f90f7904beefbd |
|
MD5 | 7e1542b45841b49ffafc95780befb89e |
|
BLAKE2b-256 | f32e7a11bc8c0f6850293698b3f0422d6a73e1cca6d08e9fdcc08a54d0a5a216 |