Skip to main content

A minimal framework for explicit phase structuring around the main action (inspired by AAA pattern and Design by Contract)

Project description

metamk - A minimal framework for defining phase structure around a main action.

Inspired by Arrange–Act–Assert testing pattern and Design by Contract,
metamk structures execution into clear, explicit phases without adding control logic.

It separates the main processing phase from setup, checks, and cleanup,
and provides both synchronous and asynchronous APIs.

from metamk import Mark

mark = Mark()

async with mark.a.as_block():
    mark.a.setup(async_setup())
    mark.a.before(async_check())

    result = mark.MAIN(await async_action())
    
    mark.a.after(async_validate())
    mark.a.final(async_cleanup())

The main action (MAIN) always belongs to Mark, while async phase methods are available under Mark.a.

Synchronous use is also supported:

with mark.as_block():
    mark.setup(lambda: print("setup"))
    mark.before(lambda: True)

    result = mark.MAIN("main action")
    
    mark.after(lambda: True)
    mark.final(lambda: print("done"))

When you want to group multiple operations within the same phase,
you can use the corresponding as_*_block() context instead of a single method call.

async with mark.a.as_block():
    async with mark.a.as_setup_block():
        await obj.setup()

    async with mark.a.as_before_block():
        await obj.pre_check()

    async with mark.a.as_MAIN_block():
        result = await obj.main_action()
        print(f"MAIN result: {result}")

    async with mark.a.as_after_block():
        await obj.validate()

    async with mark.a.as_final_block():
        await obj.cleanup()

Phase Management

This module provides a safe and flexible mechanism for managing the execution state (phase) of a process.
Phases generally progress in the following order:

INIT → SETUP → BEFORE → MAIN → CLEANUP → AFTER → FINAL → TERMINATED

However, strict sequential progression is not enforced.
While phases are expected to move “forward” in order,
it is allowed to call the same phase method or block multiple times in succession,
or to skip intermediate phases — for example, jumping directly to FINAL if necessary.

Additionally, the CLEANUP and AFTER phases require that the MAIN phase has already been executed.
If this condition is not met, or if the phase order is reversed, a PhaseError will be raised.

The INVARIANT phase is a flexible phase that can be invoked after any phase,
as long as the process has not reached the TERMINATED state.

Furthermore, by using Mark.invoke, you can perform a simple, standalone call
that is completely independent of the phase system.

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

metamk-0.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metamk-0.0.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file metamk-0.0.1.tar.gz.

File metadata

  • Download URL: metamk-0.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for metamk-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f5a549ad67bff1093308c6af7a5f9d685a8d792b1b0ec6f6c4a7ca7243ba5c2d
MD5 a8b442f115cf8f659700c7a41fb3df1c
BLAKE2b-256 0484ef2b9b1e53449554db7e0fabc27cf7f9c24232cec3ad6d9df2b22c3f53ab

See more details on using hashes here.

File details

Details for the file metamk-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: metamk-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for metamk-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 531770b4b21ea68e5e088375b5c635a7f2f760c33dc12a86d2aa49c196913b65
MD5 a8ca8037eaf44b51cfb29f11b280a84c
BLAKE2b-256 3305e4c116474f9a89f3b6b7bb1d5e2b76e74b25fc567453f7485bd3ec58a5cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page