Skip to main content

safesort: safely sort heterogeneous collections on Python 2 and 3

https://travis-ci.org/wolever/safesort.svg?branch=master

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:

  1. Object: objA > objB

  2. Type-and-object: (type(objA).__mro__, objA) > (type(objB).__mro__, objB)

  3. Type-and-identity: (type(objA).__mro__, id(objA)) > (type(objB).__mro__, id(objB))

This guarantees a total ordering which is:

  1. As consistent as possible

  2. 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

Release files for safesort 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for safesort 0.2.0
File Size Uploaded
safesort-0.2.0.tar.gz 2.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for safesort 0.2.0
File Interpreter ABI Platform
safesort-0.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 6.6 kB

Release files / safesort-0.2.0.tar.gz

Download URL safesort-0.2.0.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a6b37e6708d39072dd27316d3146d1c32fb1babdb1fd01d90961c7dc2e00dcce
BLAKE2b-256 checksum
How to use checksums
1212acc06ea4f237a8009d09b22f42bf69f9543e116e1ae87e92dd52b7c75e35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / safesort-0.2.0-py2.py3-none-any.whl

Download URL safesort-0.2.0-py2.py3-none-any.whl
Size 4.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e76cb7a4ab51c71229aa00798c67f27d8495f696506604a8ad2e2a6870162122
BLAKE2b-256 checksum
How to use checksums
3d882a1ae0ab8cfdcdd22e2a33b1562eade6da8c5758153f8260be08314d216e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page