Skip to main content

sortables

Not to be confused with sortable, which is a Grappelli admin UI sorter for Django.

Note: I have not yet sorted out (hee hee) the compatible Python versions, but this does use type hints, so it will end up being at least 3.5+. There are also some places in the code that depend on the new specification of retained order in dicts, being a Python 3.7+ specific feature. I have not yet decided whether to fix this for 3.5, 3.6 compatibility.

This initial (very alpha) state of the project was developed in Python 3.7.2.

Sanely sortable Python data structures

Sort Python data structures without awkward itemgetter, attrgettr, or lambda syntax.

Python's sorting syntax is not always expressive or easy to remember. Why do this:

sorted(d, key=operator.itemgetter(1))

when you could do this?

d.sorted(by_value=True)

Or how about sorting a named tuple. Instead of this:

sorted(t, key=operator.attrgetter('myproperty'))

we can do this:

t.sorted(key='myproperty')

... and more.

Sort a series of tuples by an internal index.

>>> t = ( ('apples', 3), ('oranges', 1), ('bananas', 2) )
>>> st = Sortable(t)
>>> st.sorted()
[('apples', 3), ('bananas', 2), ('oranges', 1)]
>>> st.sorted(key=1)
[('oranges', 1), ('bananas', 2), ('apples', 3)]
>>> st.sorted(key=1, reverse=True)
[('apples', 3), ('bananas', 2), ('oranges', 1)]

Sort a dict by value.

>>> sd = Sortable(dict(t))
>>> sd.sorted()
{'apples': 3, 'bananas': 2, 'oranges': 1}
>>> sd.sorted(by_value=True)
{'oranges': 1, 'bananas': 2, 'apples': 3}
>>> sd.sorted(by_value=True, reverse=True)
{'apples': 3, 'bananas': 2, 'oranges': 1}

Sort a series of named tuples by internal named property.

>>> from collections import namedtuple
>>> Fruit = namedtuple('Fruit', ['name', 'number'])
>>> sl = Sortable([ Fruit(name='apples', number=3), Fruit(name='bananas', number=2), Fruit(name='oranges', number=1) ])
>>> sl.sorted()
[Fruit(name='apples', number=3), Fruit(name='bananas', number=2), Fruit(name='oranges', number=1)]
>>> sl.sorted(key='number')
[Fruit(name='oranges', number=1), Fruit(name='bananas', number=2), Fruit(name='apples', number=3)]
>>> sl.sorted(key='number', reverse=True)
[Fruit(name='apples', number=3), Fruit(name='bananas', number=2), Fruit(name='oranges', number=1)]

Sort by method call.

>>> class Fruit(object):
...     def __init__(self, name, number):
...         self.name = name
...         self.number = number
...     def get_name(self):
...         return self.name
...     def get_number(self):
...         return self.number
...     def __repr__(self):
...         return '%s:%s' % (self.name, self.number)
...
>>> s = Sortable(set([Fruit('apples', 3), Fruit('bananas', 2), Fruit('oranges', 1)]))
>>> s.sorted(method='get_name')
[apples:3, bananas:2, oranges:1]
>>> s.sorted(method='get_number')
[oranges:1, bananas:2, apples:3]
>>> s.sorted(method='get_name', reverse=True)
[oranges:1, bananas:2, apples:3]

Goals & philsophy

  • create an intuitive and readable sorting syntax
  • be as unabtrusive as possible
  • don't break existing sort approaches
  • don't try to be all things to all sorting needs

Toward these ideals, sortables:

  • replaces awkward operator and lambda syntax with a simple readable syntax
  • uses a single Sortable factory for all series data structures and dicts
  • mimics (and internally utilizes) Python's sorted builtin, but does not replace it
  • focuses on the most common use cases. Fall back to the builtin sorted as needed.

sorted returns a Sortable

A Sortable's sorted method, as well as a Sortable dict's items method return a Sortable series. As with the sorted builtin, the series type is generally a list.

Regarding efficiency

In general, you can expect this to work as well as the sorted builtin since that is what it uses internally.

Sortable sorted methods always return a copy of the data. This also goes for the Sortable dict items method, which differs from the view-based dict_items of a regular dictionary.

I have used operator getters rather than lambda based key interpretations mainly because this was slightly faster for me in the few timeit tests that I ran to make the decision.

Metadata

Release files for sortables 0.1.1

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

Source distribution (sdist)

Source distribution for sortables 0.1.1
File Size Uploaded Provenance Actions
sortables-0.1.1.tar.gz 5.3 kB Without provenance Details for sortables-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sortables 0.1.1
File Interpreter ABI Platform Actions
sortables-0.1.1-py3-none-any.whl Python 3 none any Details for sortables-0.1.1-py3-none-any.whl

Total release size: 11.8 kB

Release files / sortables-0.1.1.tar.gz

Download URL sortables-0.1.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
adc61b83ab36e6d759a792293d83501d4c0b2f17e6cd98229855e9af4eb3495c
BLAKE2b-256 checksum
How to use checksums
83aaaac10d28a2d648f07fd6555cc7ff61f6a2838bd64d1d5fc8209ad10bd997
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.2 Darwin/18.7.0

Release files / sortables-0.1.1-py3-none-any.whl

Download URL sortables-0.1.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2780a1e195700e691e1783fca749562a1af6d88e7ff765279c862581f0079304
BLAKE2b-256 checksum
How to use checksums
b7f20f285b7a6937c74b66687b48157dd1210dd1369707a726b137470eb0d883
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.2 Darwin/18.7.0

Security for sortables 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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