Skip to main content

Notation

Declare metadata beside a function with a decorator and retrieve it later from a registry.

Installation

python -m pip install notation

Requires Python 3.8 or newer.

Usage

Annotate functions

Positional values are stored under integer keys starting at 0; keyword values are stored under their names. The decorator returns the original function unchanged.

from notation import Notation

notes = Notation()

@notes("public", method="GET")
def index():
    return "Hello, World!"

notes[index]   # {0: 'public', 'method': 'GET'}

Enumerate registered functions

The supplied dictionary becomes the instance namespace, so vars() returns it directly:

routes = Notation()

@routes(path="/hello")
def hello():
    return "Hello, World!"

handlers = {meta["path"]: fn for fn, meta in vars(routes).items()}

API

Notation(annotation=None) installs annotation, which must be a dict or None, as the instance's __dict__. A supplied dictionary is used directly, not copied, and vars(notation) returns it. None creates a new empty dictionary.

  • notation[key], notation[key] = value, key in notation operate on the backing dictionary.
  • notation.name reads, writes, or deletes the string key "name". A missing attribute raises AttributeError; a missing item raises KeyError.
  • bool(notation) is False when the backing dictionary is empty.
  • notation(*args, **kwargs) returns a decorator that stores {0: args[0], ..., **kwargs} under the decorated function and returns the function. Decorating the same function again replaces its metadata.

Because the dictionary is the instance namespace, keys that spell Python protocol hooks, such as __deepcopy__ or __getstate__, are visible to copy and pickle; avoid them as metadata keys. Overrides on dict subclasses, such as defaultdict.__missing__, apply to item access but not to attribute access. Iteration, len, get, item deletion, and equality are not provided; use vars(notation) instead.

Metadata is keyed by the exact function object. If another decorator replaces the function, the registry still refers to whichever object it was given. Decorators apply bottom-up, so apply Notation outermost to key the metadata by the object your module name refers to:

import functools

@notes("public", method="GET")
@functools.lru_cache
def index():
    return "Hello, World!"

notes[index]   # {0: 'public', 'method': 'GET'}

With the order reversed, notes stores metadata under the inner function and lru_cache binds index to a new wrapper, so notes[index] raises KeyError.

Tests

python -m pip install -e . mypy
python -m unittest discover -s tests
python -m mypy --strict src/notation tests/typecheck_notation.py

License

MIT. See LICENSE.

Metadata

Release files for notation 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for notation 1.0.1
File Size Uploaded
notation-1.0.1.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for notation 1.0.1
File Interpreter ABI Platform
notation-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / notation-1.0.1.tar.gz

Download URL notation-1.0.1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b93b041937bc513c4717def20f2595764d53a66087f7dbc1a6c265114a5a0884
BLAKE2b-256 checksum
How to use checksums
58dd5a38aaed22d7bd969fe461e008b06652e1a6678caffe633499ebd8187373
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / notation-1.0.1-py3-none-any.whl

Download URL notation-1.0.1-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
104a3f02890bd18e849ae2d542e16f836c4aa53df63c36725af455dc43564fce
BLAKE2b-256 checksum
How to use checksums
f2361a55673f405c04eac4eb81610f2976796899f1de1a9931c20652a2244292
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

1 release file

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page