(file, replace_dict=dict([]))
| 18 | |
| 19 | |
| 20 | def execute_script(file, replace_dict=dict([])): |
| 21 | from IPython import get_ipython |
| 22 | |
| 23 | regex = ( |
| 24 | re.compile("|".join(replace_dict.keys())) |
| 25 | if len(replace_dict.keys()) > 0 |
| 26 | else None |
| 27 | ) |
| 28 | |
| 29 | # Open the file. Read all lines into a string |
| 30 | contents = "" |
| 31 | with open(file, "r") as fp: |
| 32 | for line in fp: |
| 33 | # Replace the key value pairs |
| 34 | contents += ( |
| 35 | line |
| 36 | if regex == None |
| 37 | else regex.sub(lambda m: replace_dict[m.group()], line) |
| 38 | ) |
| 39 | |
| 40 | # Execute this script as a cell |
| 41 | result = get_ipython().run_cell(contents) |
| 42 | return result.success |
| 43 | |
| 44 | |
| 45 | def execute_code(code): |
no test coverage detected