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
Built Distribution
File details
Details for the file safesort-0.2.0.tar.gz
.
File metadata
- Download URL: safesort-0.2.0.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6b37e6708d39072dd27316d3146d1c32fb1babdb1fd01d90961c7dc2e00dcce |
|
MD5 | 8a56385656729ce58e1d48990f2a5d34 |
|
BLAKE2b-256 | 1212acc06ea4f237a8009d09b22f42bf69f9543e116e1ae87e92dd52b7c75e35 |
Provenance
File details
Details for the file safesort-0.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: safesort-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e76cb7a4ab51c71229aa00798c67f27d8495f696506604a8ad2e2a6870162122 |
|
MD5 | 04ba0242af6a0ee4e0a176219af9bee6 |
|
BLAKE2b-256 | 3d882a1ae0ab8cfdcdd22e2a33b1562eade6da8c5758153f8260be08314d216e |