Get a Python object from its string representation. For example, for ``sys.stdout.write`` would import the module ``sys`` and return the ``write`` function.
(obj_as_str: str)
| 359 | |
| 360 | |
| 361 | def get_callable(obj_as_str: str) -> object: |
| 362 | """ |
| 363 | Get a Python object from its string representation. |
| 364 | |
| 365 | For example, for ``sys.stdout.write`` would import the module ``sys`` |
| 366 | and return the ``write`` function. |
| 367 | """ |
| 368 | components = obj_as_str.split(".") |
| 369 | attrs = [] |
| 370 | while components: |
| 371 | try: |
| 372 | obj = importlib.import_module(".".join(components)) |
| 373 | except ImportError: |
| 374 | attrs.insert(0, components.pop()) |
| 375 | else: |
| 376 | break |
| 377 | |
| 378 | if not obj: |
| 379 | raise ImportError(f'Could not import "{obj_as_str}"') |
| 380 | |
| 381 | for attr in attrs: |
| 382 | obj = getattr(obj, attr) |
| 383 | |
| 384 | return obj |
| 385 | |
| 386 | |
| 387 | def get_context(config_fname: pathlib.Path, **kwargs): |
no test coverage detected