Skip to content

Instantly share code, notes, and snippets.

@globby
Last active August 29, 2015 14:06
Show Gist options
  • Save globby/3e49bdd459d181e8a2ee to your computer and use it in GitHub Desktop.
Save globby/3e49bdd459d181e8a2ee to your computer and use it in GitHub Desktop.
A little goto implementation for python
import sys, re
globals_ = globals()
def setglobals(g):
global globals_
globals_ = g
def goto(l):
global globals_
with open(sys.argv[0], "rb") as f:
data = f.read()
data_ = data.split('\n')
if isinstance(l, int):
l-=1 if l > 0 else 0
elif isinstance(l, str):
r=re.search(r"^\s*(#{0}|(def|class)\s+{0})".format(l), data, re.MULTILINE)
l=len(data_)-(data[r.start():].count("\n")) if r else len(data_)
if 0 < l < len(data_) or 0 < (l*-1) <= len(data_):
exec("".join(data_[l:]),globals_)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment