Metadata for Python
pip install metabeyond==0.0.7
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 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.
from metabeyond import hints
haiku = hints.Hint('This is designed to be poetic to read.')
@haiku
def basho():
"""old pond"""
frog.leap() in water
sound()
>>> 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 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.
from metabeyond import remarks
class Bean(remarks.Remark):
"""Marks the object as a bean."""
@Bean()
def bar():
return 69 # lol
class Route(remarks.Remark, constraint=inspect.isfunction):
def __init__(self, route):
self.route = route
@Route('/about-me')
def about_me():
...
>>> 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.