Skip to content

precise_imports

Utility functions for importing modules. During first import, all plugins are imported and cached.

cached_import

cached_import(type_name, base_module)

Imports a module entry point by name. All imports are cached, so overhead after first import is minimal. This is for internal use only: for importing modules use import_executor, import_sampler etc.

Parameters

type_name : str Class name of the type that should be imported. base_module : str For non-plugin modules, the module name. Eg. 'samplers'

Returns

class Class entry point. To construct an instance, use eg. cached_import(type_name, base_module)(**kwargs)

Raises

ImportError If the module or class cannot be found.

Source code in src/enchanted_surrogates/utils/precise_imports.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def cached_import(type_name: str, base_module: str):
    """
    Imports a module entry point by name. All imports are cached,
    so overhead after first import is minimal. This is for internal use only:
    for importing modules use import_executor, import_sampler etc.

    Parameters
    ----------
    type_name : str
        Class name of the type that should be imported.
    base_module : str
        For non-plugin modules, the module name. Eg. 'samplers'

    Returns
    -------
    class
        Class entry point. To construct an instance,
        use eg. cached_import(type_name, base_module)(**kwargs)

    Raises
    ------
    ImportError
        If the module or class cannot be found.
    """
    type_snake, type_pascal = get_snake_and_pascal(type_name)

    global __module_entry_points
    if not __module_entry_points:
        __module_entry_points = load_plugins()

    if type_snake in __module_entry_points:
        return __module_entry_points[type_snake]

    imported_type = getattr(
        importlib.import_module(f"enchanted_surrogates.{base_module}.{type_snake}"),
        type_pascal,
    )
    # Cache for later use
    __module_entry_points[type_snake] = imported_type
    return imported_type

cached_import_external

cached_import_external(type_name, module)

Imports a module entry point by name. All imports are cached, so overhead after first import is minimal. This is for internal use only: for importing modules use import_executor, import_sampler etc.

Parameters

type_name : str Class name of the type that should be imported. module : str Name of the python module from which to import

Returns

class Class entry point. To construct an instance, use eg. cached_import_external(type_name, module)(**kwargs)

Raises

ImportError If the module or class cannot be found.

Source code in src/enchanted_surrogates/utils/precise_imports.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def cached_import_external(type_name: str, module: str):
    """
    Imports a module entry point by name. All imports are cached,
    so overhead after first import is minimal. This is for internal use only:
    for importing modules use import_executor, import_sampler etc.

    Parameters
    ----------
    type_name : str
        Class name of the type that should be imported.
    module : str
        Name of the python module from which to import

    Returns
    -------
    class
        Class entry point. To construct an instance,
        use eg. cached_import_external(type_name, module)(**kwargs)

    Raises
    ------
    ImportError
        If the module or class cannot be found.
    """

    global __module_entry_points
    if not __module_entry_points:
        __module_entry_points = load_plugins()

    if module + "." + type_name in __module_entry_points:
        return __module_entry_points[module + "." + type_name]

    imported_type = getattr(importlib.import_module(f"{module}"), type_name)
    # Cache for later use
    __module_entry_points[module + "." + type_name] = imported_type
    return imported_type

camel_or_pascal_to_snake

camel_or_pascal_to_snake(s)

Converts a camelCase or PascalCase string to snake_case.

Parameters

s : str A string in camelCase or PascalCase format.

Returns

str The converted snake_case string.

Source code in src/enchanted_surrogates/utils/precise_imports.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def camel_or_pascal_to_snake(s):
    """
    Converts a camelCase or PascalCase string to snake_case.

    Parameters
    ----------
    s : str
        A string in camelCase or PascalCase format.

    Returns
    -------
    str
        The converted snake_case string.
    """
    return re.sub(r"(?<!^)(?=[A-Z])", "_", s).lower()

clear_import_cache

clear_import_cache()

Clears everything from import cache.

Source code in src/enchanted_surrogates/utils/precise_imports.py
110
111
112
113
114
115
def clear_import_cache():
    """
    Clears everything from import cache.
    """
    global __module_entry_points
    __module_entry_points = None

detect_case_style

detect_case_style(s)

Detects the naming convention of a given string.

Parameters

s : str The string to analyze.

Returns

str One of: 'snake_case', 'camelCase', 'PascalCase', 'kebab-case', or 'unknown'.

Source code in src/enchanted_surrogates/utils/precise_imports.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def detect_case_style(s):
    """
    Detects the naming convention of a given string.

    Parameters
    ----------
    s : str
        The string to analyze.

    Returns
    -------
    str
        One of: 'snake_case', 'camelCase', 'PascalCase', 'kebab-case', or 'unknown'.
    """

    if "_" in s and s.lower() == s:
        return "snake_case"
    elif re.match(r"^[a-z]+(?:[A-Z][a-z]*)+$", s):
        return "camelCase"
    elif re.match(r"^[A-Z][a-z]+(?:[A-Z][a-z]*)*$", s):
        return "PascalCase"
    elif "-" in s and s.lower() == s:
        return "kebab-case"
    else:
        return "unknown"

get_snake_and_pascal

get_snake_and_pascal(string)

Given a string in either snake_case or PascalCase, returns both formats.

Parameters

string : str The input string to normalize.

Returns

tuple of str (snake_case version, PascalCase version)

Notes

If the input is already in snake_case or PascalCase, it is preserved. camelCase and other formats are not supported.

Source code in src/enchanted_surrogates/utils/precise_imports.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def get_snake_and_pascal(string):
    """
    Given a string in either snake_case or PascalCase, returns both formats.

    Parameters
    ----------
    string : str
        The input string to normalize.

    Returns
    -------
    tuple of str
        (snake_case version, PascalCase version)

    Notes
    -----
    If the input is already in snake_case or PascalCase, it is preserved.
    camelCase and other formats are not supported.
    """
    string_case = detect_case_style(string)
    if string_case == "snake_case":
        string_snake = string
        string_pascal = snake_to_pascal(string)
    elif string_case == "PascalCase":
        string_snake = camel_or_pascal_to_snake(string)
        string_pascal = string
    else:
        raise ValueError(
            f"Input string '{string}' must be in either snake_case or PascalCase format."
        )
    return string_snake, string_pascal

import_executor

import_executor(executor_type, executor_config)

Dynamically imports and instantiates a executor class based on naming convention.

Parameters

executor_type : str The name of the executor (in snake_case or PascalCase). executor_config : dict Keyword arguments to pass to the executor constructor.

Returns

object An instance of the executor class.

Raises

ImportError If the module or class cannot be found.

Source code in src/enchanted_surrogates/utils/precise_imports.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def import_executor(executor_type, executor_config):
    """
    Dynamically imports and instantiates a executor class based on naming convention.

    Parameters
    ----------
    executor_type : str
        The name of the executor (in snake_case or PascalCase).
    executor_config : dict
        Keyword arguments to pass to the executor constructor.

    Returns
    -------
    object
        An instance of the executor class.

    Raises
    ------
    ImportError
        If the module or class cannot be found.
    """
    executor = cached_import(executor_type, "executors")(**executor_config)
    return executor

import_parser

import_parser(parser_type, parser_config)

Dynamically imports and instantiates a parser class based on naming convention.

Parameters

parser_type : str The name of the parser (in snake_case or PascalCase). parser_config : dict Keyword arguments to pass to the sampler constructor.

Returns

object An instance of the parser class.

Raises

ImportError If the module or class cannot be found.

Source code in src/enchanted_surrogates/utils/precise_imports.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
def import_parser(parser_type, parser_config):
    """
    Dynamically imports and instantiates a parser class based on naming convention.

    Parameters
    ----------
    parser_type : str
        The name of the parser (in snake_case or PascalCase).
    parser_config : dict
        Keyword arguments to pass to the sampler constructor.

    Returns
    -------
    object
        An instance of the parser class.

    Raises
    ------
    ImportError
        If the module or class cannot be found.
    """
    parser = cached_import(parser_type, "parsers")(**parser_config)
    return parser

import_runner

import_runner(runner_type, runner_config)

Dynamically imports and instantiates a runner class based on naming convention.

Parameters

runner_type : str The name of the sampler (in snake_case or PascalCase). sampler_config : dict Keyword arguments to pass to the sampler constructor.

Returns

object An instance of the sampler class.

Raises

ImportError If the module or class cannot be found.

Source code in src/enchanted_surrogates/utils/precise_imports.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def import_runner(runner_type, runner_config):
    """
    Dynamically imports and instantiates a runner class based on naming convention.

    Parameters
    ----------
    runner_type : str
        The name of the sampler (in snake_case or PascalCase).
    sampler_config : dict
        Keyword arguments to pass to the sampler constructor.

    Returns
    -------
    object
        An instance of the sampler class.

    Raises
    ------
    ImportError
        If the module or class cannot be found.
    """
    runner = cached_import(runner_type, "runners")(**runner_config)
    return runner

import_sampler

import_sampler(sampler_type, sampler_config)

Dynamically imports and instantiates a sampler class based on naming convention.

Parameters

sampler_type : str The name of the sampler (in snake_case or PascalCase). sampler_config : dict Keyword arguments to pass to the sampler constructor.

Returns

object An instance of the sampler class.

Raises

ImportError If the module or class cannot be found.

Source code in src/enchanted_surrogates/utils/precise_imports.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def import_sampler(sampler_type, sampler_config):
    """
    Dynamically imports and instantiates a sampler class based on naming convention.

    Parameters
    ----------
    sampler_type : str
        The name of the sampler (in snake_case or PascalCase).
    sampler_config : dict
        Keyword arguments to pass to the sampler constructor.

    Returns
    -------
    object
        An instance of the sampler class.

    Raises
    ------
    ImportError
        If the module or class cannot be found.
    """
    config: dict = sampler_config.copy()
    config.pop("type", None)

    sampler = cached_import(sampler_type, "samplers")(**config)
    return sampler

snake_to_pascal

snake_to_pascal(s)

Converts a snake_case string to PascalCase.

Parameters

s : str A string in snake_case format.

Returns

str The converted PascalCase string.

Source code in src/enchanted_surrogates/utils/precise_imports.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def snake_to_pascal(s):
    """
    Converts a snake_case string to PascalCase.

    Parameters
    ----------
    s : str
        A string in snake_case format.

    Returns
    -------
    str
        The converted PascalCase string.
    """

    return "".join(word.title() for word in s.split("_"))