Skip to main content

Common utility functions for the kiarina namespace packages

Project description

kiarina-utils-common

English | 日本語

PyPI version Python License: MIT

The package providing the most general-purpose utilities in the kiarina namespace.

Installation

pip install kiarina-utils-common

API Reference

import_object

import_object(import_path)

Import and return an object from an import path.

Parameters

  • import_path (str): Import path in the format module_name:object_name
    • Example: kiarina.utils.common:parse_config_string

Returns

  • The imported object (class, function, or any other object)

Raises

  • ValueError: If import_path format is invalid
  • ImportError: If the module cannot be imported
  • AttributeError: If the object doesn't exist in the module

Examples

# Import a function
parse_fn = import_object('kiarina.utils.common:parse_config_string')
result = parse_fn('key=value')

# Import a class
MyClass = import_object('myapp.plugins:MyPlugin')
instance = MyClass()

# Use with type hints
from typing import Callable
parser: Callable = import_object('kiarina.utils.common:parse_config_string')

parse_config_string

parse_config_string(
    config_str,
    *,
    separator="&",
    key_value_separator="=",
    nested_separator=".",
    brackets="()",
)

Parse configuration string into nested dictionary.

Parameters

  • config_str (str): Configuration string to parse
  • separator (str, optional): Item separator. Default: "&"
  • key_value_separator (str, optional): Key-value separator. Default: "="
  • nested_separator (str, optional): Nested key separator. Default: "."
  • brackets (str, optional): Two-character open/close pair for quoting values that contain separator characters. Default: "()". Pass "" to disable.

Returns

  • dict[str, Any]: Parsed configuration dictionary

Examples

# Basic usage
parse_config_string("key1=value1&key2=value2")
# {"key1": "value1", "key2": "value2"}

# Nested keys
parse_config_string("cache.enabled=true&db.port=5432")
# {"cache": {"enabled": True}, "db": {"port": 5432}}

# Flags (no value)
parse_config_string("debug&verbose")
# {"debug": None, "verbose": None}

# Bracketed values (verbatim, type-conversion suppressed)
parse_config_string("k=(a&b=c)&n=5")
# {"k": "a&b=c", "n": 5}

# Custom separators
parse_config_string("a:1;b:2", separator=";", key_value_separator=":")
# {"a": 1, "b": 2}

ConfigRegistry

Manage configurations by name, alias, and default, with overrides from configuration strings or keyword arguments. Resolved configurations are deep-copied so the registered sources remain unchanged.

from kiarina.utils.config_registry import ConfigRegistry

registry = ConfigRegistry[dict[str, object]](
    get_default=lambda: "standard",
    get_aliases=lambda: {"default": "standard"},
    get_presets=lambda: {
        "standard": {"model": "example", "temperature": 0.5},
    },
)

config = registry.get("default?temperature=0.8")
# {"model": "example", "temperature": 0.8}

resolved = registry.resolve("standard")
print(resolved.name)
print(resolved.config)

Key methods

  • register(name, config) / unregister(name): Register or remove a runtime configuration
  • get(specifier=None, **kwargs): Return a resolved configuration
  • resolve(specifier=None, **kwargs): Return the resolved name and configuration as ResolvedConfig
  • list_names() / list_aliases(): List available names and aliases
  • clear(): Remove runtime configurations

ComponentRegistry

Manage classes and factories by name and create a new instance when needed. Preset and custom components may also be defined using import paths.

from kiarina.utils.component_registry import ComponentRegistry


class Client:
    def __init__(self, endpoint: str = "") -> None:
        self.endpoint = endpoint


registry = ComponentRegistry(expected_type=Client)
registry.register("client", Client)

client = registry.resolve("client?endpoint=https://example.com")

Key methods

  • register(name, factory) / unregister(name): Register or remove a component factory
  • create(name, *args, **kwargs): Create a new instance by name
  • resolve(input=None, *args, **kwargs): Resolve an instance or specifier
  • get(name): Return a runtime-registered factory
  • list_names() / list_aliases(): List available names and aliases
  • clear(): Remove runtime-registered factories

ObjectRegistry

Resolve configurations through ConfigRegistry and create objects from them. get() retains and reuses created objects, while create() and resolve() return a new object each time.

from kiarina.utils.object_registry import ObjectRegistry


class Client:
    def __init__(self, endpoint: str = "") -> None:
        self.endpoint = endpoint


registry = ObjectRegistry[Client, dict[str, object]](
    expected_type=Client,
    get_default=lambda: "default",
    get_presets=lambda: {
        "default": {"endpoint": "https://example.com"},
    },
)

shared_client = registry.get()
fresh_client = registry.resolve("default?endpoint=https://api.example.com")

Key methods

  • register(name, object) / unregister(name): Register or remove an object
  • get(name=None): Return an object, creating and retaining it when necessary
  • create(name, **kwargs): Create a new object from a configuration
  • resolve(input=None, **kwargs): Resolve an instance or configuration specifier into a new object
  • register_config(name, config) / unregister_config(name): Register or remove a runtime configuration
  • list_names() / list_aliases(): List available names and aliases
  • clear() / clear_configs(): Remove runtime objects or configurations

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

kiarina_utils_common-2.2.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

kiarina_utils_common-2.2.0-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_utils_common-2.2.0.tar.gz.

File metadata

  • Download URL: kiarina_utils_common-2.2.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kiarina_utils_common-2.2.0.tar.gz
Algorithm Hash digest
SHA256 fd00b9a306d6cba21ece333d0af46998d0038085c4a1e0fecd26e3f3bc307155
MD5 dc501f998d9405147102b744d0593a1a
BLAKE2b-256 3577a76a88972a9a3541205e2d395712c84dc953b95e24060cf4fcd99f341f2d

See more details on using hashes here.

File details

Details for the file kiarina_utils_common-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_utils_common-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d18a014f0ec14f27d26c438dda3b4aeb11dbc01b89321fec876de4c9ca1d7e83
MD5 dffb984306c30632f9dccc192c9ac1f1
BLAKE2b-256 bf175bdd3a51713c4aa4d41091ee0ff3ba89662d4e94678cfe1498cd4fe4863c

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