Imports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (``xml.sax.saxutils.escape``) or with a colon as object delimiter (``xml.sax.saxutils:escape``). If the
(import_name: str, silent: bool = False)
| 138 | |
| 139 | |
| 140 | def import_string(import_name: str, silent: bool = False) -> t.Any: |
| 141 | """Imports an object based on a string. This is useful if you want to |
| 142 | use import paths as endpoints or something similar. An import path can |
| 143 | be specified either in dotted notation (``xml.sax.saxutils.escape``) |
| 144 | or with a colon as object delimiter (``xml.sax.saxutils:escape``). |
| 145 | |
| 146 | If the `silent` is True the return value will be `None` if the import |
| 147 | fails. |
| 148 | |
| 149 | :return: imported object |
| 150 | """ |
| 151 | try: |
| 152 | if ":" in import_name: |
| 153 | module, obj = import_name.split(":", 1) |
| 154 | elif "." in import_name: |
| 155 | module, _, obj = import_name.rpartition(".") |
| 156 | else: |
| 157 | return __import__(import_name) |
| 158 | return getattr(__import__(module, None, None, [obj]), obj) |
| 159 | except (ImportError, AttributeError): |
| 160 | if not silent: |
| 161 | raise |
| 162 | |
| 163 | |
| 164 | def open_if_exists(filename: str, mode: str = "rb") -> t.Optional[t.IO[t.Any]]: |
no outgoing calls
no test coverage detected