MCPcopy Create free account
hub / github.com/Netflix/vmaf / import_python_file

Function import_python_file

python/vmaf/tools/misc.py:167–211  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

165 return [i for (i, val) in enumerate(a) if func(val)]
166
167def 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
213def make_absolute_path(path: str, current_dir: str) -> str:
214 """

Callers 15

run_vmaf_cvFunction · 0.90
run_vmaf_kfold_cvFunction · 0.90
explain_model_on_datasetFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90

Calls 2

copyMethod · 0.45

Tested by 15

mainFunction · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
test_read_datasetMethod · 0.72