metabeyond - metadata utilities for modern Python code
A small set of utilities for writing self-documenting code, and for allowing introspection of metadata applied using decorators at runtime in a similar fashion to Java 6 annotation interfaces. This entire module is heavily influenced by Java 6 annotations such as the following nonsensical example:
@Configuration
@FunctionalInterface
@SafeForSpeedsNotExceeding( value = 1.15572735, units = ATTOPARSECS_PER_MICROFORTNIGHT)
public class QuantumDecoherenceStabilizer {
@Haiku
public void checkNodes() {
if (tree.hasAnyRealLeafNodes()) {
freshenLeavesOn(tree);
}
}
}
thus providing the following Python solution:
@Configuration()
@FunctionalInterface()
@SafeForSpeedsNotExceeding(value=1.15572735, units=ATTOPARSECS_PER_MICROFORTNIGHT)
class QuantumDecoherenceStabilizer:
@haiku
def check_nodes(self) -> str:
if tree.has_any_real_leaf_nodes():
freshen_leaves_on(tree)
(See the Google Annotation Gallery for more esoteric annotation examples!)
Hints
Hints are designed to add some description to a docstring for utilities such as Sphinx. The idea behind using this as a decorator is that decorators are often much more visible to the reader than text in docstrings, so it is a simple way to exploit readability. Apart from manipulating the docstring, no reference to this decorator is ever actually kept. It is essentially transparent.
- Definition of a hint:
from metabeyond import hints
haiku = hints.Hint('This is designed to be poetic to read.')
- Applying a hint. Hints never have parenthesis after their name in a decoration.
@haiku
def basho():
"""old pond"""
frog.leap() in water
sound()
- This provides the following effect:
>>> import inspect
>>> get_docstring = lambda o: inspect.cleandoc(inspect.getdoc(o))
>>> print(get_docstring(basho))
old pond
This is designed to be poetic to read.
Remarks
Remarks are the next step up from hints and are designed to register themselves unto a class or function to be detected later. This is akin to how Java 6 annotations work.
- Defining a remark
from metabeyond import remarks
class Bean(remarks.Remark):
"""Marks the object as a bean."""
- Applying a remark. Remarks always have parenthesis. Failure to add parenthesis will result in the decorated item being replaced by the decorator that would otherwise be called, which will cause any dependant code to break, so don't try it.
@Bean()
def bar():
return 69 # lol
- We may provide more complex definitions with validation constraints on the decorated element, or with attributes specific to each decoration. See the documentation for a full explanation, but the following gives you the general idea. This example defines a route decorator similar to what is provided by flask. Applying it to a class will result in a failure.
class Route(remarks.Remark, constraint=inspect.isfunction):
def __init__(self, route):
self.route = route
@Route('/about-me')
def about_me():
...
- Inspecting any applied remarks is also pretty easy.
>>> from metabeyond import remarks
>>> remarks.get_remark(Route, about_me)
Route(route='/about_me')
Other methods for searching and querying in various ways can be found in the documentation.
Release files for metabeyond 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| metabeyond-0.2.2.tar.gz | 13.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| metabeyond-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.8 kB
Release files / metabeyond-0.2.2.tar.gz
| Download URL | metabeyond-0.2.2.tar.gz |
|---|---|
| Size | 13.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e45d3f09e935cbe9289b2a1ea404c87dea431fc0c01f5ab90d617563a73fda2d
|
|
BLAKE2b-256 checksum How to use checksums |
e87e89c5be4c2790c3fb74fc7d74b63acdc0fbdf3797ab72047656a50e0e2109
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
|
Release files / metabeyond-0.2.2-py3-none-any.whl
| Download URL | metabeyond-0.2.2-py3-none-any.whl |
|---|---|
| Size | 15.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2a3a14fa7a782f21a3b1dacd1259487841a7020badb307a189c363b399fc3a3d
|
|
BLAKE2b-256 checksum How to use checksums |
ab297005495c4a9d0f2ec9b8503424e896809f22ac13e1fbf1c74cea13d5d3b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
|