Safely sort heterogeneous collections.
Project description
safesort does what it says on the box: guarantees safe sorting of arbitrary heterogeneous lists across Python 2 and Python 3:
>>> list(sorted(["a", 1, None])) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str() >>> list(safesort(["a", 1, None)) [None, 1, 'a']
Three ordering keys are attempted for each comparison:
Object: objA > objB
Type-and-object: (type(objA).__mro__, objA) > (type(objB).__mro__, objB)
Type-and-identity: (type(objA).__mro__, id(objA)) > (type(objB).__mro__, id(objB))
This guarantees a total ordering which is:
As consistent as possible
Broadly sensible: objects with similar types will be grouped together
For example:
>>> from safesort import safesort >>> input = ['a', set([]), [], {}, 1, None] >>> list(safesort(input)) [None, 1, {}, [], set([]), 'a']
Installation
safesort can be installed with Python 2 or Python 3 using pip or easy_install:
$ pip install safesort - OR - $ easy_install safesort
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
safesort-0.2.0.tar.gz
(2.5 kB
view hashes)
Built Distribution
Close
Hashes for safesort-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e76cb7a4ab51c71229aa00798c67f27d8495f696506604a8ad2e2a6870162122 |
|
MD5 | 04ba0242af6a0ee4e0a176219af9bee6 |
|
BLAKE2b-256 | 3d882a1ae0ab8cfdcdd22e2a33b1562eade6da8c5758153f8260be08314d216e |