Skip to main content

Safely sort heterogeneous collections.

Project description

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

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

safesort-0.2.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

safesort-0.2.0-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2 Python 3

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

Hashes for safesort-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a6b37e6708d39072dd27316d3146d1c32fb1babdb1fd01d90961c7dc2e00dcce
MD5 8a56385656729ce58e1d48990f2a5d34
BLAKE2b-256 1212acc06ea4f237a8009d09b22f42bf69f9543e116e1ae87e92dd52b7c75e35

See more details on using hashes here.

Provenance

File details

Details for the file safesort-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for safesort-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e76cb7a4ab51c71229aa00798c67f27d8495f696506604a8ad2e2a6870162122
MD5 04ba0242af6a0ee4e0a176219af9bee6
BLAKE2b-256 3d882a1ae0ab8cfdcdd22e2a33b1562eade6da8c5758153f8260be08314d216e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page