Skip to content

Instantly share code, notes, and snippets.

@ercas
Created May 1, 2020 03:15
Show Gist options
  • Save ercas/ba9b6903e9d1b061e8496ad716a25795 to your computer and use it in GitHub Desktop.
Save ercas/ba9b6903e9d1b061e8496ad716a25795 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
""" Command-line tool to automatically start an interactive Python session and
run some initial importing code. All code between the lines starting with the
comments `# BEGIN SETUP` and `# END SETUP` will be echoed back to the user with
the indentation level of the `# BEGIN SETUP` line removed.
"""
import os
PYTHON_BIN="ipython3"
if not "PYCALC" in os.environ:
os.environ["PYCALC"] = "1"
os.system("{} -i {}".format(PYTHON_BIN, __file__))
else:
# BEGIN SETUP
import numpy
import pandas
import pint
from matplotlib import pyplot
ureg = pint.UnitRegistry()
# END SETUP
print("\nloaded {}:".format(__file__))
print("> import os")
with open(__file__, "r") as f:
printing = False
indentation = 0
for line in f:
if line.lstrip().startswith("# END SETUP"):
break
elif line.lstrip().startswith("# BEGIN SETUP"):
printing = True
indentation = line.find("#")
else:
if printing:
print("> {}".format(line.rstrip()[indentation:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment