Python example: mrpt_core_example.py

Clock, thread pool and other core utilities from mrpt.core.

Modules: mrpt.core

  1#!/usr/bin/env python3
  2"""
  3Clock, thread pool and other core utilities from mrpt.core.
  4
  5Demonstrates:
  6  - Clock: current time, conversion to/from double
  7  - WorkerThreadsPool: thread pool for async tasks
  8  - abs_diff: absolute difference utilities
  9  - reverse_bytes: endianness helpers
 10  - deg2rad / rad2deg: angle unit conversion
 11  - get_env / get_env_int: environment variable access
 12  - from_string_int / from_string_double: safe string-to-number parsing
 13  - format1d / format1s: printf-style single-argument formatting
 14"""
 15
 16import math
 17import os
 18from mrpt.core import (
 19    Clock,
 20    WorkerThreadsPool,
 21    abs_diff_int, abs_diff_double,
 22    reverse_bytes_u32, reverse_bytes_u64,
 23    deg2rad, rad2deg,
 24    get_env, get_env_int,
 25    from_string_int, from_string_double,
 26    format1d, format1s,
 27)
 28
 29# ── Clock ─────────────────────────────────────────────────────────────────────
 30print("── Clock ───────────────────────────────────")
 31now_sec = Clock.nowDouble()
 32tp = Clock.fromDouble(123.456)
 33print(f"Current time: {now_sec:.3f} s")
 34print(f"Clock.fromDouble(123.456) → toDouble: {Clock.toDouble(tp):.3f}")
 35assert abs(Clock.toDouble(tp) - 123.456) < 1e-9
 36print("  Clock round-trip ✓")
 37
 38# ── WorkerThreadsPool ─────────────────────────────────────────────────────────
 39print("\n── WorkerThreadsPool ───────────────────────")
 40pool = WorkerThreadsPool(4)
 41print(f"WorkerThreadsPool(4): size={pool.size()}")
 42del pool  # destructor joins threads; must be explicit before Python exit to avoid segfault
 43
 44# ── abs_diff ──────────────────────────────────────────────────────────────────
 45print("\n── abs_diff ────────────────────────────────")
 46print(f"abs_diff_int(10, 3)      = {abs_diff_int(10, 3)}")      # 7
 47print(f"abs_diff_double(1.5, 4.0)= {abs_diff_double(1.5, 4.0)}")  # 2.5
 48assert abs_diff_int(10, 3) == 7
 49assert abs(abs_diff_double(1.5, 4.0) - 2.5) < 1e-9
 50print("  abs_diff ✓")
 51
 52# ── reverse_bytes ─────────────────────────────────────────────────────────────
 53print("\n── reverse_bytes ───────────────────────────")
 54x = 0x12345678
 55print(f"reverse_bytes_u32(0x12345678) = {hex(reverse_bytes_u32(x))}")  # 0x78563412
 56assert reverse_bytes_u32(x) == 0x78563412
 57print("  reverse_bytes ✓")
 58
 59# ── deg2rad / rad2deg ────────────────────────────────────────────────────────
 60print("\n── deg2rad / rad2deg ───────────────────────")
 61print(f"deg2rad(180) = {deg2rad(180.0):.6f}  (expect {math.pi:.6f})")
 62print(f"rad2deg(pi)  = {rad2deg(math.pi):.4f}  (expect 180.0000)")
 63assert abs(deg2rad(180.0) - math.pi) < 1e-9
 64assert abs(rad2deg(math.pi) - 180.0) < 1e-9
 65print("  angle conversions ✓")
 66
 67# ── get_env ───────────────────────────────────────────────────────────────────
 68print("\n── get_env ─────────────────────────────────")
 69# Use a well-known var that should exist in most environments
 70path_val = get_env("PATH", "")
 71print(f"get_env('PATH', '')   = {path_val[:40]!r}...")
 72assert path_val != "", "PATH env var should be set"
 73
 74# Non-existent variable → default value
 75missing = get_env("MRPT_TEST_NONEXISTENT_VAR_XYZ", "default_val")
 76print(f"get_env('nonexistent') = {missing!r}")
 77assert missing == "default_val"
 78
 79# Integer env var
 80os.environ["MRPT_TEST_INT"] = "42"
 81ival = get_env_int("MRPT_TEST_INT", 0)
 82print(f"get_env_int('MRPT_TEST_INT') = {ival}")
 83assert ival == 42
 84del os.environ["MRPT_TEST_INT"]
 85print("  get_env ✓")
 86
 87# ── from_string ───────────────────────────────────────────────────────────────
 88print("\n── from_string ─────────────────────────────")
 89print(f"from_string_int('42')          = {from_string_int('42')}")
 90print(f"from_string_int('bad', -1)     = {from_string_int('bad', -1)}")
 91print(f"from_string_double('3.14')     = {from_string_double('3.14'):.4f}")
 92print(f"from_string_double('x', 0.0)  = {from_string_double('x', 0.0)}")
 93assert from_string_int("42") == 42
 94assert from_string_int("bad", -1) == -1
 95assert abs(from_string_double("3.14") - 3.14) < 1e-9
 96print("  from_string ✓")
 97
 98# ── format1d / format1s ───────────────────────────────────────────────────────
 99print("\n── format1d / format1s ─────────────────────")
100s = format1d("Value = %.4f", 3.14159)
101print(f"format1d('Value = %.4f', pi) = {s!r}")
102assert "3.1416" in s
103
104s2 = format1s("Hello, %s!", "MRPT")
105print(f"format1s('Hello, %s!', 'MRPT') = {s2!r}")
106assert s2 == "Hello, MRPT!"
107print("  format ✓")
108
109print("\nAll checks ✓")