sanely sortable Python data structures
Project description
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.
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 sortables-0.1.1.tar.gz
.
File metadata
- Download URL: sortables-0.1.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.2 Darwin/18.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | adc61b83ab36e6d759a792293d83501d4c0b2f17e6cd98229855e9af4eb3495c |
|
MD5 | c50c1d8b23af49cdcecb45cd0cbbdac8 |
|
BLAKE2b-256 | 83aaaac10d28a2d648f07fd6555cc7ff61f6a2838bd64d1d5fc8209ad10bd997 |
File details
Details for the file sortables-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: sortables-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.2 Darwin/18.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2780a1e195700e691e1783fca749562a1af6d88e7ff765279c862581f0079304 |
|
MD5 | 51d9f3bd958a3d704a151f1b9bf81515 |
|
BLAKE2b-256 | b7f20f285b7a6937c74b66687b48157dd1210dd1369707a726b137470eb0d883 |