Skip to main content

Safe comparisons (total ordering) for any object in Python 3

Project description

In Python 2, it was possible to compare any object:

>>> None > 2
False
>>> [] < object()
True

But this is no longer true in Python 3:

>>> None > 2
...
TypeError: '>' not supported between instances of 'NoneType' and 'int'
>>> [] < object()
...
TypeError: '<' not supported between instances of 'list' and 'object'

But there are many cases where it is useful to, for example:

  • Sort heterogeneous lists (ie, lists that contain many types of object)

  • Compare objects to None (for example, to find the max(...) of a list where some items may be None)

  • Write generic functions which will have robust, deterministic behaviour on arbitrary input

safe_cmp provides functions for safely sorting and ordering any value in Python 3. In fancy math terms, safe_cmp implements a total ordering of all values in Python 3 [1].

safe_cmp implements Python 2 compatible safe versions of the ordering functions:

  • safe_cmp: a Python 2 compatible implementation of cmp for Python 3

  • safe_sorted: a safe version of sorted(...)

  • safe_min: a safe version of min(...)

  • safe_max: a safe version of max(...)

And provides a wrapper - safe_order - which defines a total ordering for any object (for example, heterogeneous_list.sort(key=safe_order)).

Examples

Sorting a heterogeneous list:

>>> from safe_cmp import safe_sorted, safe_order
>>> items = [1, None, "foo", {}, object]
>>> list(safe_sorted(items)) # Using "safe_sorted"
[None, {}, 1, 'foo', object]
>>> items.sort(key=safe_order) # Using "safe_order" with key=
>>> items
[None, {}, 1, 'foo', object]

Finding the max of a list which includes nulls:

>>> from safe_cmp import safe_max
>>> safe_max([1, None, 3, None, 4])
4

The rare situation where Python 2 style cmp is useful:

>>> from safe_cmp import safe_cmp
>>> safe_cmp(None, 1)
-1
>>> safe_cmp(None, None)
0
>>> safe_cmp(1, None)
1

Note: safe_cmp will produce Python 2 compatible results when called with nan:

>>> from safe_cmp import safe_cmp
>>> nan = float("NaN")
>>> safe_cmp(nan, 1)
-1
>>> safe_cmp(1, nan)
1
>>> safe_cmp(nan, nan)
0

As will safe_sorted:

>>> from safe_cmp import safe_sorted
>>> list(safe_sorted([nan, 2, nan, 1]))
[nan, 2, nan, 1]

Performance

Currently safe_cmp methods are currently implemented in Python (in contrast to their unsafe builtin counterparts, which are implemented in C), so performance will notable worse for large comparisons:

In [1]: %timeit safe_max(range(10000000))
2.8 s ± 42 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [2]: %timeit max(range(10000000))
345 ms ± 6.23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

For smaller comparisons, though, the difference will be negligible:

In [1]: %timeit safe_max(1, 2)
682 ns ± 7.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [2]: %timeit max(1, 2)
218 ns ± 6.87 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

If there is interest in performant implementations, however, they will be straight forward to provide.

Additionally, where obvious, performance optimizations have been implemented (for example, caching the result of key= functions).

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

safe_cmp-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

safe_cmp-0.1.0-py2-none-any.whl (2.8 kB view details)

Uploaded Python 2

File details

Details for the file safe_cmp-0.1.0.tar.gz.

File metadata

  • Download URL: safe_cmp-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for safe_cmp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e236a340512c1d45c2bcdb4bb57d9c8923a1574b965637d8c9e0bbbee1b5aa16
MD5 139014dddab279a62e0af2696ffdc3e0
BLAKE2b-256 75e50c60ac672f64c3b415d266ebc656dfe2f77cafe4bd52d1b27a555d096adb

See more details on using hashes here.

File details

Details for the file safe_cmp-0.1.0-py2-none-any.whl.

File metadata

  • Download URL: safe_cmp-0.1.0-py2-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for safe_cmp-0.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 c69a1f63121514bf62fc6b3df9b77bc7cdb8bfc3900fa0783e82482f0ae7176c
MD5 81197a7cf30cb32839f7a7bc9c25d265
BLAKE2b-256 5edd2f8335e70a432c1f66982467fc7ecd8ffb94aa8decdf4352539667ca7536

See more details on using hashes here.

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