Created
April 4, 2023 14:33
-
-
Save bobturneruk/dfccd3d6ce5af7545c4a1159ce4724e8 to your computer and use it in GitHub Desktop.
pulls code out of carpentries episodes - not the most reliable!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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