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 per class and at runtime.

Copyright 2016, 2017, 2018, 2019 Odin Kroeger

Use case

sortableclasses makes classes sortable by precedence and priority. This is useful if you want to take some input, apply a set of transformations, and output the result (i.e., if you’re writing what on UNIX-ish systems is called a ‘filter’). Ordinarily, you would chain those transformations as function calls (i.e., return transform_1(...(transform_n(input)))), but the larger the number of transformation grows, the more difficult this is to maintain.

sortableclasses enables you to define each of those transformations as a class, assign each of them a list of predecessor and successor classes or a numerical priority, and then simply sort them using sort or sorted. Simply put, it enables you to write classes that are similar to plugins in how they function.

For example:

>>> 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 requires Python 3.

If you have Python’s setuptools, simply say:

pip3 install sortableclasses

Otherwise, download the most recent stable release (v0.9.4rc31), unzip it and copy the directory sortableclasses into a directory in your Python’s sys.path.

You can do all of the above by:

# Download and unpack *sortableclasses* to the current directory.
curl -fsS https://codeload.github.com/odkr/sortableclasses.py/tar.gz/v0.9.4rc31 |
    tar -xz
# The command below guesses a directory to install *sortableclasses* to.
PYPATH=$(python3 -c 'import sys; print("\n".join(sys.path))' |
    grep -v '.zip' | grep -E "(${HOME?}|/local/)" | head -n1)
# If the command below errors, no suitable directory was found.
# Otherwise, it will show you where *sortableclasses* will be installed to.
echo "${PYPATH:?'Did not find a suitable directory.'}" >&2
# Copy the directory "sortableclasses" into that directory.
[ -d "${PYPATH:?}" ] && {
    PACKAGE=sortableclasses.py-0.9.4rc31/sortableclasses
    cp -r "$PACKAGE" "$PYPATH" || sudo cp -r "$PACKAGE" "$PYPATH"
}
# Remove the downloaded files, if you want to.
rm -rf sortableclasses.py-0.9.4rc31

Documentation

You can view the reference documentation at Read the Docs or, once you installed sortableclasses, by:

pydoc sortableclasses

Contact

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

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

sortableclasses-0.9.4rc31.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

sortableclasses-0.9.4rc31-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file sortableclasses-0.9.4rc31.tar.gz.

File metadata

  • Download URL: sortableclasses-0.9.4rc31.tar.gz
  • Upload date:
  • Size: 19.1 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 sortableclasses-0.9.4rc31.tar.gz
Algorithm Hash digest
SHA256 5b7d28d511be770591fff8374f93a8fcd2f17bba4fcfc8650a4787762f67aaf5
MD5 044540e586c178b1fe77d4d912375eb1
BLAKE2b-256 943db8f099c57c83f6ab7b59d15274595aced8b4da21dd2376e2bc25d58ab60d

See more details on using hashes here.

File details

Details for the file sortableclasses-0.9.4rc31-py3-none-any.whl.

File metadata

  • Download URL: sortableclasses-0.9.4rc31-py3-none-any.whl
  • Upload date:
  • Size: 19.1 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 sortableclasses-0.9.4rc31-py3-none-any.whl
Algorithm Hash digest
SHA256 4bcf6a7baaad23cf84441acfe478c0f23d91d8cbbb219e6fbb75ba44760636bd
MD5 7ca0dadcb2dd37332fe2e9bf7ee63c84
BLAKE2b-256 be5841273ff134a6017344d2f69869f35c3cf9d283069a4f9104e31933f0344a

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