Simple and powerful tagging for Python objects.
The Python package gentag provides simple and powerful tagging for arbitrary Python objects. After defining your tags and associated objects you can query for the difference, intersection and union of tags to select specific objects. The package is currently tested on cPython 2.6, 2.7, 3.4, 3.5, 3.6 and PyPy (2.7).
While the ideas behind gentag have been floating around in my head since 2012 I didn’t publish this as a standalone Python package until 2018 which explains why I’m publishing the initial version as a beta. Looking ahead towards the future:
- It may be that the current version serves my needs fine and at some point I decide to replace the ‘beta’ label with a ‘stable’ label without making any substantive changes.
- Releasing gentag is one step in the direction of releasing another Python package that I’ve been thinking about for a very long time now and if I turn out to have trouble integrating gentag into that package I won’t hesitate to make (potentially major) changes to gentag.
The gentag package is available on PyPI which means installation should be as simple as:
$ pip install gentag
There’s actually a multitude of ways to install Python packages (e.g. the per user site-packages directory, virtual environments or just installing system wide) and I have no intention of getting into that discussion here, so if this intimidates you then read up on your options before returning to these instructions ;-).
The following sections give an overview of how to get started. For more details about the Python API please refer to the API documentation available on Read the Docs.
To get started you have to create a Scope object:
>>> from gentag import Scope >>> tags = Scope()
The purpose of Scope objects is to group together related tags into an evaluation context for tag expressions.
Once you’ve defined some tags and associated objects you can query them, for example here we query for the union of two tags:
>>> tags.evaluate('archiving | encryption') ['deb', 'gpg', 'luks', 'tar', 'zip']
These tag expressions can get arbitrarily complex:
>>> tags.evaluate('(archiving | encryption) & compression') ['deb', 'zip']
The following operators can be used to compose tags:
These operators create new Tag objects that can be composed further. Although tags composed at runtime in Python syntax don’t have a name, it is possible define named composite tags using the Scope.define() method (see below).
There’s one special tag that is always available under the name ‘all’. As you might have guessed it provides access to a set with all tagged objects:
>>> tags.evaluate('all') ['bzip2', 'deb', 'gpg', 'gzip', 'luks', 'lzma', 'tar', 'zip']
This can be useful to select all but a specific tag of objects:
>>> tags.evaluate('all - encryption') ['bzip2', 'deb', 'gzip', 'lzma', 'tar']
The example in the usage section isn’t actually very useful, this is partly because I didn’t want a complicated subject matter to distract readers from usage instructions :-).
The actual use case that triggered the ideas behind gentag presented itself to me in 2012 when I wanted to query a database of more than 200 Linux server names categorized by aspects such as:
- The distributor id (a string like ‘debian’ or ‘ubuntu’).
- The distribution codename (a string like ‘trusty’ or ‘xenial’).
- The server’s role (database, mailserver, webserver, etc).
- The server’s environment (production, development).
The easy selection of subsets of servers for my Python programs to operate on quickly evolved into my main interface for selecting groups of servers. Since then I’ve wanted to use similar functionality in other places, but found it too much work to develop one-off solutions. This is how gentag was born.
The latest version of gentag is available on PyPI and GitHub. The documentation is hosted on Read the Docs. For bug reports please create an issue on GitHub. If you have questions, suggestions, etc. feel free to send me an e-mail at firstname.lastname@example.org.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size gentag-2.0-py2.py3-none-any.whl (14.7 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size gentag-2.0.tar.gz (13.1 kB)||File type Source||Python version None||Upload date||Hashes View|