pyiron_snippets.retrieve module

Helper functions for managing the relationship between strings and imports.

exception pyiron_snippets.retrieve.StringNotImportableError[source]

Bases: ImportError

pyiron_snippets.retrieve.get_importable_string_from_string_reduction(string_reduction: str, reduced_object: object) str[source]

Per the pickle docs:

> If a string is returned, the string should be interpreted as the name of a global

variable. It should be the object’s local name relative to its module; the pickle module searches the module namespace to determine the object’s module. This behaviour is typically useful for singletons.

To then import such an object from a non-local caller, we try scoping the string with the module of the object which returned it.

pyiron_snippets.retrieve.import_from_string(library_path: str) object[source]

Import an object using a string of its python library location.

Parameters:

library_path (str) – The full module path to the desired object.

Returns:

The imported object.

Return type:

(object)

Example

>>> from pyiron_snippets import retrieve
>>> ThreadPoolExecutor = retrieve.import_from_string(
...     "concurrent.futures.ThreadPoolExecutor"
... )
>>> with ThreadPoolExecutor(max_workers=2) as executor:
...     future = executor.submit(pow, 2, 3)
...     print(future.result())
8