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.

Installation

pip

pip install metamk

github

pip install git+https://github.com/minoru-jp/metamk.git

Status

This project is in very early development (alpha stage).
APIs and behavior may change without notice.


License

MIT License © 2025 minoru_jp

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.3.tar.gz (8.3 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.3-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: metamk-0.0.3.tar.gz
  • Upload date:
  • Size: 8.3 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.3.tar.gz
Algorithm Hash digest
SHA256 4744a628714390d34323011923b4ee41b4875921bb5c7eacc0440247223a7d6f
MD5 e52e1e171e6422df95f6d5cfac61d8c6
BLAKE2b-256 8fc1f0862908753f2001e8b1b1ab9bda65ce2d940f6fc13961eefc5c09569d27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: metamk-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a8c35a2350653781b2addcd1d0914f0558399fe797cbc20c73b88391e11316cf
MD5 ebf7d1fba2378f7081aa987ad5dff35b
BLAKE2b-256 354318640de305f609968f7f9299a67a9d840180c33dd75dadb202ae1b42afd9

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