How to Implement a Manual Approval Gate Mock in Python

Simulates a manual approval workflow with threshold-based rules, random decisions for medium amounts, and logs each result with timing.

Easy Python 3.9+ Aug 9, 2026 Production deployment patterns 18 views 0 copies

Python code

24 lines
Python 3.9+
import random
import time


def approve_request(amount: float) -> bool:
    if amount <= 1000:
        return True
    if amount <= 5000:
        return random.random() < 0.7
    return False


def main():
    requests = [500, 1200, 7500, 3000, 50]
    for amount in requests:
        start = time.perf_counter()
        approved = approve_request(amount)
        elapsed = time.perf_counter() - start
        status = "APPROVED" if approved else "REJECTED"
        print(f"${amount:>7,.2f} -> {status} ({elapsed:.4f}s)")


if __name__ == "__main__":
    main()

Output

stdout
$ 500.00 -> APPROVED (0.0000s)
$ 1,200.00 -> APPROVED (0.0000s)
$ 7,500.00 -> REJECTED (0.0000s)
$ 3,000.00 -> REJECTED (0.0000s)
$    50.00 -> APPROVED (0.0000s)

How it works

This mock uses simple threshold checks to mimic an approval gate: amounts under $1,000 auto-approve, amounts over $5,000 auto-reject, and mid-range amounts pass with 70% probability. The random.random() call simulates a human or external decision, and time.perf_counter() measures the latency introduced by the gate. This pattern is useful for testing pipelines that must handle asynchronous or manual approval steps.

Common mistakes

  • Using `random.random() < 0.7` without seeding can make tests non-deterministic.
  • Forgetting that `time.perf_counter()` measures wall-clock time, not just CPU time.
  • Hardcoding thresholds instead of reading them from config or environment variables.

Variations

  1. Use `secrets.randbelow()` or `random.SystemRandom()` for more security-sensitive decisions.
  2. Replace the boolean return with an enum like `ApprovalStatus.APPROVED` for richer state handling.

Real-world use cases

  • Testing a payment orchestration service that must pause and wait for an approval webhook.
  • Simulating a human-in-the-loop review step in a CI/CD deployment pipeline.
  • Creating a fallback decision engine when the real approval service is unavailable.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Production deployment patterns

Related tutorials and quizzes for this topic.