Skip to content

Standard Library

tinax.stdlib converts explicit argument sequences into caller-owned configuration and creates isolated stream loggers. It never reads sys.argv, calls logging.basicConfig, mutates root handlers, or registers named loggers globally.

parse_config passes a shallow read-only mapping to the factory; mutable values produced by custom argparse actions remain caller-owned. Its argv excludes the executable name, so an Abseil entry point passes argv[1:].

Tinax does not provide an Abseil domain. Grain executables still enter through absl.app.run because that upstream worker lifecycle requires parsed Abseil flags.

import logging
import sys

from tinax.stdlib import make_stream_logger

logger = make_stream_logger(
    "trainer",
    level=logging.INFO,
    stream=sys.stderr,
    format_string="%(levelname)s %(message)s",
)

parse_config

parse_config(
    parser: ArgumentParser,
    argv: Sequence[str],
    factory: Callable[[Mapping[str, object]], T],
) -> T

Parse explicit arguments and pass a shallow read-only value mapping to a configuration factory.

Parameters:

Name Type Description Default
parser ArgumentParser

Configured argparse.ArgumentParser. sys.argv is never read.

required
argv Sequence[str]

Argument strings excluding the executable name (an Abseil entry point passes argv[1:]).

required
factory Callable[[Mapping[str, object]], T]

Callable invoked with a shallow read-only mapping of parsed values; its return value is passed through unchanged.

required

Returns:

Type Description
T

The object produced by factory.

Raises:

Type Description
TypeError

If parser is not an ArgumentParser, argv is not a sequence of strings, or factory is not callable.

make_stream_logger

make_stream_logger(
    name: str,
    *,
    level: int,
    stream: TextIO,
    format_string: str,
) -> Logger

Construct an unregistered non-propagating logger with one explicit stream handler.

The logger is not added to the global registry, does not propagate to the root logger, and leaves logging.basicConfig and root handlers untouched.

Parameters:

Name Type Description Default
name str

Logger name. Must be non-empty.

required
level int

Logging level as an integer (booleans are rejected).

required
stream TextIO

Writable text stream providing callable write and flush.

required
format_string str

logging.Formatter format string, validated on creation.

required

Returns:

Type Description
Logger

A configured logging.Logger with a single stream handler.

Raises:

Type Description
TypeError

If name or format_string is not a string, level is not an integer (booleans are rejected), or stream lacks callable write and flush methods.

ValueError

If name is empty or format_string is not a valid format.