Giter VIP home page Giter VIP logo

python-orderby's Introduction

orderby

Python key functions for multi-field ordering in SQL ORDER BY fashion

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')
  • chained: asc('foo').desc('bar') usage
  • multiple fields at once: asc('foo', 'bar')

Examples

orderby() string syntax:

>>> 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:

>>> 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:

>>> 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:

>>> 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)

Internals

To be explained here later...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.