No project description provided
Project description
# pyorderby
Python key functions for multi-field ordering in SQL ORDER BY fashion
[![image]]
Meant to be used with built-in `sorted()` *key function*.
Supports also `list.sort()` doing *in-place sorting*.
Implementation uses `operator.itemgetter()` + some internal helper classes to allow descending sorting order.
So far this is tested and used on *lists of dictionaries*. Adding support for named tuples and others would be possible (using `operator.attrgetter()`).
## Usage
- SQL-like: `orderby('foo ASC, bar DESC')`
- eval like: `orderby('$foo - $bar * 10')`
- chained: `asc('foo').desc('bar')` usage
- multiple fields at once: `asc('foo', 'bar')`
## Examples
#### `orderby()` string syntax:
``` python
>>> from orderby import orderby
>>> import json
>>> files = [
... {'size': 1234, 'path': 'foo/bar.txt'},
... {'size': 0, 'path': '/dev/null'},
... {'size': 1234, 'path': 'foo/abc.bin'},
... ]
>>> print(json.dumps(sorted(files, key=orderby('size DESC, path')), indent=2))
[
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 0,
"path": "/dev/null"
}
]
```
#### Chained asc() and desc() usage:
``` python
>>> from orderby import asc, desc
>>> print(json.dumps(sorted(files, key=desc('size').asc('path')), indent=2))
[
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 0,
"path": "/dev/null"
}
]
```
#### In-place sorting of a list:
``` python
>>> files.sort(key=desc('path'))
>>> print(json.dumps(files, indent=2))
[
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 0,
"path": "/dev/null"
}
]
>>> files.sort(key=desc('size').asc('path'))
>>> print(json.dumps(files, indent=2))
[
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 0,
"path": "/dev/null"
}
]
```
#### Works also with [SortedContainers]:
``` python
>>> from sortedcontainers import SortedList
>>> from orderby import desc
>>> mylist = SortedList(key=desc('value'))
>>> mylist
SortedListWithKey([], key=<orderby.sorter.OrderBy object at 0x108f65978>, load=1000)
>>> mylist.add({'value': 13})
>>> mylist.add({'value': 2})
>>> mylist.add({'value': 1000})
>>> mylist
SortedListWithKey([{'value': 1000}, {'value': 13}, {'value': 2}], key=<orderby.sorter.OrderBy object at 0x108f65978>, load=1000)
```
[image]: https://img.shields.io/pypi/v/orderby.svg
[![image]]: https://pypi.python.org/pypi/orderby
[SortedContainers]: http://www.grantjenks.com/docs/sortedcontainers/
Python key functions for multi-field ordering in SQL ORDER BY fashion
[![image]]
Meant to be used with built-in `sorted()` *key function*.
Supports also `list.sort()` doing *in-place sorting*.
Implementation uses `operator.itemgetter()` + some internal helper classes to allow descending sorting order.
So far this is tested and used on *lists of dictionaries*. Adding support for named tuples and others would be possible (using `operator.attrgetter()`).
## Usage
- SQL-like: `orderby('foo ASC, bar DESC')`
- eval like: `orderby('$foo - $bar * 10')`
- chained: `asc('foo').desc('bar')` usage
- multiple fields at once: `asc('foo', 'bar')`
## Examples
#### `orderby()` string syntax:
``` python
>>> from orderby import orderby
>>> import json
>>> files = [
... {'size': 1234, 'path': 'foo/bar.txt'},
... {'size': 0, 'path': '/dev/null'},
... {'size': 1234, 'path': 'foo/abc.bin'},
... ]
>>> print(json.dumps(sorted(files, key=orderby('size DESC, path')), indent=2))
[
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 0,
"path": "/dev/null"
}
]
```
#### Chained asc() and desc() usage:
``` python
>>> from orderby import asc, desc
>>> print(json.dumps(sorted(files, key=desc('size').asc('path')), indent=2))
[
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 0,
"path": "/dev/null"
}
]
```
#### In-place sorting of a list:
``` python
>>> files.sort(key=desc('path'))
>>> print(json.dumps(files, indent=2))
[
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 0,
"path": "/dev/null"
}
]
>>> files.sort(key=desc('size').asc('path'))
>>> print(json.dumps(files, indent=2))
[
{
"size": 1234,
"path": "foo/abc.bin"
},
{
"size": 1234,
"path": "foo/bar.txt"
},
{
"size": 0,
"path": "/dev/null"
}
]
```
#### Works also with [SortedContainers]:
``` python
>>> from sortedcontainers import SortedList
>>> from orderby import desc
>>> mylist = SortedList(key=desc('value'))
>>> mylist
SortedListWithKey([], key=<orderby.sorter.OrderBy object at 0x108f65978>, load=1000)
>>> mylist.add({'value': 13})
>>> mylist.add({'value': 2})
>>> mylist.add({'value': 1000})
>>> mylist
SortedListWithKey([{'value': 1000}, {'value': 13}, {'value': 2}], key=<orderby.sorter.OrderBy object at 0x108f65978>, load=1000)
```
[image]: https://img.shields.io/pypi/v/orderby.svg
[![image]]: https://pypi.python.org/pypi/orderby
[SortedContainers]: http://www.grantjenks.com/docs/sortedcontainers/
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
pyorderby-1.0.0.tar.gz
(5.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyorderby-1.0.0.tar.gz.
File metadata
- Download URL: pyorderby-1.0.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aab25f809454f23c37cc016453b8acbd9962d1c7a37659ca25a2ccb866cfa406
|
|
| MD5 |
9cf825907c086ecac067152050640495
|
|
| BLAKE2b-256 |
4fb8a77c9a773a4fe17a69d3866bf2de259f72a9afef6393cf8a660fd4ccb558
|
File details
Details for the file pyorderby-1.0.0-py2.py3-none-any.whl.
File metadata
- Download URL: pyorderby-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8a8a2bd8caed9a08590c22d7c3c1d69a6099b89d0f73cf0b6d84347baea3248
|
|
| MD5 |
f40b12274b77bd1d98cae4e22e9862d1
|
|
| BLAKE2b-256 |
b01ef38586266a517043c0daf9461b6f713f5fb4fff4865bcd74c1f79239076f
|