Skip to main content

Retrieve all classes derived from a class and sort them by a given priority and order, making it easy to draw up and use plugin-like classes.

Project description

Makes classes sortable by precedence and priority. The order of precedence of classes and their priority is defined de-centrally and at runtime.

Copyright 2016, 2017, 2018 Odin Kroeger

Use case

sortableclasses makes classes sortable. This is useful if all you want to do is take some input, apply a set of transformations, and output the result (i.e., are writing what on UNIX-ish systems is called a ‘filter’). Of course, you may just write, e.g., return t1(t2(t3(...(tn(input)))))) but the more transformations you need to add, the harder this is to maintain. sortableclasses allows you to turn the transformations into classes, assign each of them a priority (or a list of predecessors and successors) and then sort them using sort sorted.

>>> import sortableclasses
>>> import abc
>>> import functools
>>>
>>> class Transform(sortableclasses.Pluggable):
...     @staticmethod
...     @abc.abstractmethod
...     def transform(input):
...         pass
...
>>> class MakeContent(Transform):
...     @staticmethod
...     def transform(input):
...         if input == ':-(':
...             return ':-|'
...         return input
...
>>> class MakeHappy(Transform):
...     successorof = (MakeContent,)
...     @staticmethod
...     def transform(input):
...         if input == ':-|':
...             return ':-)'
...         return input
...
>>> class MakeVeryHappy(Transform):
...     successorof = (MakeHappy,)
...     @staticmethod
...     def transform(input):
...         if input == ':-)':
...             return ':-D'
...         return input
...
>>> transforms = sorted(Transform.getderived())
>>> transforms
[<class 'MakeContent'>, <class 'MakeHappy'>, <class 'MakeVeryHappy'>]
>>> input = ':-('
>>> functools.reduce(lambda k, s: s.transform(k), transforms, input)
':-D'

Installation

You use sortableclasses at your own risk. You have been warned.

sortableclasses works only in Python 3.

Clone the repository and run setup.py install.

Documentation

See <https://sortableclassespy.readthedocs.io/en/latest/> for reference.

You can also view the inline documentation, by:

pydoc sortableclasses

Contact

If there’s something wrong with sortableclasses, please open an issue at:

<https://github.com/odkr/sortableclasses.py/issues>

Licence

This programme is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This programme is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Further Information

GitHub:

<https://github.com/odkr/sortableclasses.py>

Read the docs:

<https://sortableclassespy.readthedocs.io/en/latest/>

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

sortableclass-0.9.4b0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

sortableclass-0.9.4b0-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file sortableclass-0.9.4b0.tar.gz.

File metadata

  • Download URL: sortableclass-0.9.4b0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for sortableclass-0.9.4b0.tar.gz
Algorithm Hash digest
SHA256 0f2a56f82672ee63a34ba777effe8fb603efad94b9fb2d19e28e92dac82f3412
MD5 c724057c608567b3b457a9924ca3f02c
BLAKE2b-256 970a9ceae10edd2ee60f47e76c0078d329ce0e889836e592cc41092f5061dd01

See more details on using hashes here.

File details

Details for the file sortableclass-0.9.4b0-py3-none-any.whl.

File metadata

  • Download URL: sortableclass-0.9.4b0-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for sortableclass-0.9.4b0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f619f274dfd67fd20ecc1656457839e4731ee3f6a60e09696e6202a1338c455
MD5 0e39e8b3195e0a8b791d7ecb44776123
BLAKE2b-256 c497b3a2f380df38f816724944622177de59281595938858c0be462dce3e6e6f

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