Import a python file as a module, allowing overriding some of the variables. Assumption: in the original python file, variables to be overridden get assigned once only, in a single line.
(filepath : str, override : dict = None)
| 165 | return [i for (i, val) in enumerate(a) if func(val)] |
| 166 | |
| 167 | def import_python_file(filepath : str, override : dict = None): |
| 168 | """ |
| 169 | Import a python file as a module, allowing overriding some of the variables. |
| 170 | Assumption: in the original python file, variables to be overridden get assigned once only, in a single line. |
| 171 | """ |
| 172 | if override is None: |
| 173 | filename = get_file_name_without_extension(filepath) |
| 174 | try: |
| 175 | from importlib.machinery import SourceFileLoader |
| 176 | ret = SourceFileLoader(filename, filepath).load_module() |
| 177 | except ImportError: |
| 178 | import imp |
| 179 | ret = imp.load_source(filename, filepath) |
| 180 | return ret |
| 181 | else: |
| 182 | override_ = override.copy() |
| 183 | tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix='.py') |
| 184 | with open(filepath, 'r') as fin: |
| 185 | with open(tmpfile.name, 'w') as fout: |
| 186 | while True: |
| 187 | line = fin.readline() |
| 188 | if len(override_) > 0: |
| 189 | suffixes = [] |
| 190 | for key in list(override_.keys()): |
| 191 | if key in line and '=' in line: |
| 192 | s = f"{key} = '{override_[key]}'" if isinstance(override_[key], str) else f"{key} = {override_[key]}" |
| 193 | suffixes.append(s) |
| 194 | del override_[key] |
| 195 | if len(suffixes) > 0: |
| 196 | line = '\n'.join([line.strip()] + suffixes) + '\n' |
| 197 | fout.write(line) |
| 198 | if not line: |
| 199 | break |
| 200 | if len(override_) > 0: |
| 201 | for key in override_: |
| 202 | s = f"{key} = '{override_[key]}'" if isinstance(override_[key], str) else f"{key} = {override_[key]}" |
| 203 | s += '\n' |
| 204 | fout.write(s) |
| 205 | #============= debug ================= |
| 206 | # with open(tmpfile.name, 'r') as fin: |
| 207 | # print(fin.read()) |
| 208 | #===================================== |
| 209 | ret = import_python_file(tmpfile.name) |
| 210 | os.remove(tmpfile.name) |
| 211 | return ret |
| 212 | |
| 213 | def make_absolute_path(path: str, current_dir: str) -> str: |
| 214 | """ |