ats_utilities.factory_class module

Module

factory_class.py

Copyright

Copyright (C) 2017 - 2026 Vladimir Roncevic <elektron.ronca@gmail.com> ats_utilities is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ats_utilities is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Info

Factory universally injects instances, gets private instances and setup instance string representation. Encapsulates core utilities to minimize constructor overhead. Provides a simple factory mechanism for dependency injection.

ats_utilities.factory_class.format_instance_to_string(instance: Any) str[source]

Generates a standardized string representation for any class instance. Cleans private attributes and appends memory addresses in hex.

Parameters:

instance (<Any>) – The class instance to format.

Returns:

String representation of the instance.

Return type:

<str>

Exceptions:

None.

ats_utilities.factory_class.get_class_name(instance: Any) str[source]

Returns the class name of an instance.

Parameters:

instance (<Any>) – The class instance.

Returns:

The class name in string format.

Return type:

<str>

Exceptions:

None.

ats_utilities.factory_class.get_private_attr(instance: Any, attr_name: str) Any[source]

Dynamically retrieves a private attribute from an instance.

Parameters:
  • instance (<Any>) – The class instance (self) containing the attribute.

  • attr_name (<str>) – The target private attribute name (e.g., ‘_checker’).

Returns:

The resolved attribute value.

Return type:

<Any>

Exceptions:

AttributeError.

ats_utilities.factory_class.inject(instance: Any, *dependencies: tuple[str, Any, Any, str | list[str] | tuple[str, ...] | None]) None[source]

Universally injects system or domain components into a class instance. Adheres to SOLID principles by avoiding hardcoded component names or classes. Dynamically handles multi-dependency relationship chains between sequence steps.

Parameters:
  • instance (<Any>) – The object instance (self) to inject attributes into.

  • dependencies (<tuple[str, Any, Any, str | list[str] | tuple[str, ...] | None]>) – Variadic sequence of tuples containing injection rules. Format: (‘attr_name’, value, fallback, ‘depends_on_attr’) The ‘depends_on_attr’ can be a string, list, or tuple.

Exceptions:

None.

ats_utilities.factory_class.require_attributes(*attr_names: str) Callable[[Callable[[...], Any]], Callable[[...], Any]][source]

Checks if instance attribute is defined and has value or not. In case attribute value is not defined set default value to None. In case attribute value is not defined and not empty, raise ATSValueError exception.

Parameters:

attr_names (<tuple[str, ...]>) – Tuple of attribute names to check.

Returns:

Decorated function.

Return type:

<Callable[…, Any]>

Exceptions:

ATSValueError.