Common utility functions for the kiarina namespace packages
Project description
kiarina-utils-common
English | 日本語
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 formatmodule_name:object_name- Example:
kiarina.utils.common:parse_config_string
- Example:
Returns
- The imported object (class, function, or any other object)
Raises
ValueError: If import_path format is invalidImportError: If the module cannot be importedAttributeError: 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 parseseparator(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 configurationget(specifier=None, **kwargs): Return a resolved configurationresolve(specifier=None, **kwargs): Return the resolved name and configuration asResolvedConfiglist_names()/list_aliases(): List available names and aliasesclear(): 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 factorycreate(name, *args, **kwargs): Create a new instance by nameresolve(input=None, *args, **kwargs): Resolve an instance or specifierget(name): Return a runtime-registered factorylist_names()/list_aliases(): List available names and aliasesclear(): 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 objectget(name=None): Return an object, creating and retaining it when necessarycreate(name, **kwargs): Create a new object from a configurationresolve(input=None, **kwargs): Resolve an instance or configuration specifier into a new objectregister_config(name, config)/unregister_config(name): Register or remove a runtime configurationlist_names()/list_aliases(): List available names and aliasesclear()/clear_configs(): Remove runtime objects or configurations
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd00b9a306d6cba21ece333d0af46998d0038085c4a1e0fecd26e3f3bc307155
|
|
| MD5 |
dc501f998d9405147102b744d0593a1a
|
|
| BLAKE2b-256 |
3577a76a88972a9a3541205e2d395712c84dc953b95e24060cf4fcd99f341f2d
|
File details
Details for the file kiarina_utils_common-2.2.0-py3-none-any.whl.
File metadata
- Download URL: kiarina_utils_common-2.2.0-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d18a014f0ec14f27d26c438dda3b4aeb11dbc01b89321fec876de4c9ca1d7e83
|
|
| MD5 |
dffb984306c30632f9dccc192c9ac1f1
|
|
| BLAKE2b-256 |
bf175bdd3a51713c4aa4d41091ee0ff3ba89662d4e94678cfe1498cd4fe4863c
|