Skip to content

Utils

objectory.utils

Contain some utility functions or helpers.

objectory.utils.all_child_classes

all_child_classes(cls: type) -> set[type]

Get all the child classes (or subclasses) of a given class.

Based on: https://stackoverflow.com/a/3862957

Parameters:

Name Type Description Default
cls type

Specifies the class whose child classes you want to get.

required

Returns:

Type Description
set[type]

The set of all the child classes of the given class.

Example usage:

>>> from objectory.utils import all_child_classes
>>> class Foo:
...     pass
...
>>> all_child_classes(Foo)
set()
>>> class Bar(Foo):
...     pass
...
>>> all_child_classes(Foo)
{<class '....Bar'>}

objectory.utils.full_object_name

full_object_name(obj: Any) -> str

Compute the full name of an object.

This function works for class and function objects.

Parameters:

Name Type Description Default
obj Any

Specifies the class/function that you want to compute the full name.

required

Returns:

Type Description
str

The full name of the object.

Raises:

Type Description
TypeError

if the object is not a class or a function.

Example usage:

>>> from objectory.utils import full_object_name
>>> class MyClass:
...     pass
...
>>> full_object_name(MyClass)
'....MyClass'
>>> def my_function():
...     pass
...
>>> full_object_name(my_function)
'....my_function'

objectory.utils.import_object

import_object(object_path: str) -> Any

Import an object given its path.

This function can be used to dynamically import a class or a function. The object path should have the following structure: module_path.object_name. This function returns None if the object path does not respect this structure.

Parameters:

Name Type Description Default
object_path str

Specifies the path of the object to import.

required

Returns:

Type Description
Any

The object if the import was successful otherwise None.

Example usage:

>>> from objectory.utils import import_object
>>> obj = import_object("collections.Counter")
>>> obj()
Counter()
>>> fn = import_object("math.isclose")
>>> fn(1, 1)
True

objectory.utils.instantiate_object

instantiate_object(
    obj: Callable | type,
    *args: Any,
    _init_: str = "__init__",
    **kwargs: Any
) -> Any

Instantiate dynamically an object from its configuration.

Parameters:

Name Type Description Default
obj Callable | type

Specifies the class to instantiate or the function to call.

required
*args Any

Variable length argument list.

()
_init_ str

Specifies the function to use to create the object. This input is ignored if obj is a function. If "__init__", the object is created by calling the constructor.

'__init__'
**kwargs Any

Arbitrary keyword arguments.

{}

Returns:

Type Description
Any

The instantiated object if obj is a class name, otherwise the returned value of the function.

Raises:

Type Description
TypeError

if obj is not a class or a function.

Example usage:

>>> from collections import Counter
>>> from objectory.utils import instantiate_object
>>> instantiate_object(Counter, [1, 2, 1])
Counter({1: 2, 2: 1})
>>> instantiate_object(list, [1, 2, 1])
[1, 2, 1]

objectory.utils.is_lambda_function

is_lambda_function(obj: Any) -> bool

Indicate if the object is a lambda function or not.

Adapted from https://stackoverflow.com/a/23852434

Parameters:

Name Type Description Default
obj Any

Specifies the object to check.

required

Returns:

Type Description
bool

True if the input is a lambda function, otherwise False

Example usage:

>>> from objectory.utils import is_lambda_function
>>> is_lambda_function(lambda value: value + 1)
True
>>> def my_function(value: int) -> int:
...     return value + 1
...
>>> is_lambda_function(my_function)
False
>>> is_lambda_function(1)
False

objectory.utils.is_object_config

is_object_config(config: dict, cls: type) -> bool

Indicate if the input configuration is a configuration for a given class.

This function only checks if the value of the key _target_ is valid. It does not check the other values. If _target_ indicates a function, the returned type hint is used to check the class.

Parameters:

Name Type Description Default
config dict

Specifies the configuration to check.

required
cls type

Specifies the object class.

required

Returns:

Type Description
bool

True if the input configuration is a configuration for the given class.

Example usage:

>>> from objectory.utils import is_object_config
>>> from collections import Counter
>>> is_object_config({"_target_": "collections.Counter", "iterable": [1, 2, 1, 3]}, Counter)
True

objectory.utils.resolve_name

resolve_name(
    name: str,
    object_names: set[str],
    allow_import: bool = True,
) -> str | None

Find a match of the query name in the set of object names.

The resolution is successful only if there is only one object name that can match with the query name. Please read the documentation to learn more about the name resolution mechanism.

Parameters:

Name Type Description Default
name str

Specifies the query name to use to find a match in the set of object names.

required
object_names set[str]

Specifies the set of object names.

required
allow_import bool

If True, the parent package is installed if it was not imported previously.

True

Returns:

Type Description
str | None

The resolved name if the resolution was successful, otherwise None

Example usage:

>>> from objectory.utils import resolve_name
>>> resolve_name("OrderedDict", {"collections.OrderedDict", "collections.Counter"})
collections.OrderedDict
>>> resolve_name("objectory.utils.resolve_name", {"math.isclose"})
'objectory.utils.name_resolution.resolve_name'
>>> resolve_name("OrderedDict", {"collections.Counter", "math.isclose"})
None