Skip to content

Instantly share code, notes, and snippets.

@uschille
Forked from bobturneruk/extract_code.py
Last active April 14, 2023 00:29
Show Gist options
  • Save uschille/9f9f2f5ce7d199a3cc64ceb20182cad9 to your computer and use it in GitHub Desktop.
Save uschille/9f9f2f5ce7d199a3cc64ceb20182cad9 to your computer and use it in GitHub Desktop.
pulls code out of carpentries episodes - not the most reliable!
import sys
import os
import re
path = os.path.dirname(sys.argv[1])
path = "." if path == "" else path
episode = os.path.splitext(os.path.basename(sys.argv[1]))[0]
with open(f"{path}/{episode}.md") as f:
content = f.read()
content = re.sub(r"^(>\s)*", "", content, flags=re.MULTILINE) # strip all "> "
content = re.sub("(\%matplotlib widget)", "", content) # strip notebook magics
matches = re.findall(
"(\~\~\~\n)(?!{)([\w\W]*?)(\~\~\~\n)(.*)",
content,
) # find all code blocks
code = ""
for m in matches:
if "python" in m[3]: # python code blocks only
code += m[1]
print(code)
with open(f"{path}/{episode}.py", "w") as f:
f.write(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment