Skip to main content

Common utility functions for the kiarina namespace packages

Project description

kiarina-utils-common

PyPI version Python License: MIT

Common utility functions for the kiarina namespace packages.

Installation

pip install kiarina-utils-common

Features

Dynamic Object Import

Import objects (classes, functions, constants) dynamically from import paths. Useful for plugin systems and dynamic loading scenarios.

from kiarina.utils.common import import_object

# 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()

# Import with type hints for better IDE support
from typing import Callable
parser: Callable = import_object("kiarina.utils.common:parse_config_string")

Configuration String Parser

Parse configuration strings into nested dictionaries with automatic type conversion.

from kiarina.utils.common import parse_config_string

# Basic usage
config = parse_config_string("cache.enabled=true&db.port=5432")
# Result: {"cache": {"enabled": True}, "db": {"port": 5432}}

# Flag support (no value)
config = parse_config_string("debug&verbose&cache.enabled=true")
# Result: {"debug": None, "verbose": None, "cache": {"enabled": True}}

# Array indices support
config = parse_config_string("items.0=first&items.1=second")
# Result: {"items": ["first", "second"]}

# Bracketed values (atomic, no type conversion)
config = parse_config_string("vad=(mock?sample_rate=16000&p.0=1.0)&top_k=3")
# Result: {"vad": "mock?sample_rate=16000&p.0=1.0", "top_k": 3}

# Custom separators
config = parse_config_string(
    "key1:val1;key2.sub:42",
    separator=";",
    key_value_separator=":",
)
# Result: {"key1": "val1", "key2": {"sub": 42}}

Type Conversion

Values are automatically converted to appropriate types:

  • "true", "True"bool(True)
  • "false", "False"bool(False)
  • Numeric strings ("1", "0", "-5", "3.14") → int or float
  • Other strings → str

Nested Keys

Use dot notation for nested structures:

config = parse_config_string("database.host=localhost&database.port=5432")
# Result: {"database": {"host": "localhost", "port": 5432}}

Array Indices

Use numeric keys for array structures:

config = parse_config_string("users.0.name=Alice&users.0.age=30&users.1.name=Bob")
# Result: {"users": [{"name": "Alice", "age": 30}, {"name": "Bob"}]}

API Reference

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(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}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

This is a personal project by kiarina. While issues and pull requests are welcome, please note that this is primarily developed for personal use.

Related Packages

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.0.0.tar.gz (11.5 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.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kiarina_utils_common-2.0.0.tar.gz
  • Upload date:
  • Size: 11.5 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.0.0.tar.gz
Algorithm Hash digest
SHA256 f27d0118fb372b8f2bf7e7467f4f3f194e3f1a70bf49fd18ae1d081d7a9b705d
MD5 bf8db3e8bf9a30b82fb39d46821468b6
BLAKE2b-256 0dc0ce4f1620709a7686a47dc5271c1b23e8781a2bc368fb2600bf3e3852158d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kiarina_utils_common-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 367316c7180dea50eb184a7b9bf74ef8fdf8acb98279c1c90e8e1ada8e06022b
MD5 3ae3045083c4c7eb1c6c3e9870d09f24
BLAKE2b-256 64a1b0737ddb96381434d15c684cd22bd3af448a9ec800718e9e3519d0292f4e

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