Skip to content

Instantly share code, notes, and snippets.

@bobturneruk
Created April 4, 2023 14:33
Show Gist options
  • Save bobturneruk/dfccd3d6ce5af7545c4a1159ce4724e8 to your computer and use it in GitHub Desktop.
Save bobturneruk/dfccd3d6ce5af7545c4a1159ce4724e8 to your computer and use it in GitHub Desktop.
pulls code out of carpentries episodes - not the most reliable!
import re
with open("_episodes/08-connected-components.md") as f:
content = f.read()
content = re.sub("(\>[ |\n])", "", content) # 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("_episodes/08-connected-components.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