Skip to content

Commit

Permalink
First pass of Getting to know py; mv Functions above For loops
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanistheone committed Apr 3, 2024
1 parent 642eda9 commit 0c78b16
Show file tree
Hide file tree
Showing 4 changed files with 1,244 additions and 1,044 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# TMP storage for solutions
tutorials/solutions/


# jupyter-book dir
_build

Expand Down
135 changes: 135 additions & 0 deletions scripts/split_off_solutions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "16eab062-5094-47b9-97cb-4ee9ac3acd44",
"metadata": {},
"source": [
"# Create a version of a tutorial without solutions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c37c81f5-0a4a-4e54-b5e1-a70847eafe17",
"metadata": {},
"outputs": [],
"source": [
"from copy import deepcopy\n",
"import nbformat\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "df2d36ea-de2e-427e-b040-985378f42f4a",
"metadata": {},
"outputs": [],
"source": [
"def has_solution(cell):\n",
" \"\"\"Return True if cell is marked as containing an exercise solution.\"\"\"\n",
" cell_text = cell[\"source\"].replace(\" \", \"\").lower()\n",
" first_line = cell_text.split(\"\\n\")[0]\n",
" return (\n",
" cell_text.startswith(\"#@titlesolution\")\n",
" or \"to_remove\" in first_line\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "36437d86-8afe-4a09-9616-aaba4ba3591e",
"metadata": {},
"outputs": [],
"source": [
"# TODO: import \n",
"# def extract_solutions(nb, nb_dir, nb_name):\n",
"# from process_notebooks.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "364235e7-9426-44e0-bc35-9e9e21578202",
"metadata": {},
"outputs": [],
"source": [
"SOLUTIONS_DIR = \"/Users/ivan/Projects/Minireference/STATSbook/noBSstatsnotebooks/tutorials/solutions\"\n",
"\n",
"def split_off_solutions(srcfilename: str, destfilename: str):\n",
" \"\"\"Load the notebook, adjust headings in markdown cells, and write back.\"\"\"\n",
" with open(srcfilename, 'r', encoding='utf-8') as inf:\n",
" nb = nbformat.read(inf, as_version=4)\n",
"\n",
" for cell in nb.cells:\n",
" if cell.cell_type == 'code' and has_solution(cell):\n",
" cell_source = cell[\"source\"]\n",
"\n",
" cell_source = cell_source.replace(\"@titlesolution\", \"\")\n",
" title = cell_source.splitlines()[0].strip()\n",
" filename = title.replace(\"# \",\"\").replace(\" \",\"_\") + \".py\"\n",
" with open(os.path.join(SOLUTIONS_DIR,filename), \"w\") as solf:\n",
" solf.write(cell_source)\n",
"\n",
" # clear cell\n",
" cell['source'] = \"\"\n",
" if \"outputID\" in cell[\"metadata\"]:\n",
" del cell[\"metadata\"][\"outputId\"]\n",
" if \"outputs\" in cell:\n",
" cell[\"outputs\"] = []\n",
" if \"execution_count\" in cell:\n",
" del cell[\"execution_count\"]\n",
"\n",
" with open(destfilename, 'w', encoding='utf-8') as outf:\n",
" nbformat.write(nb, outf)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "18d69778-a915-4e55-8205-6af438db6a79",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Example usage\n",
"\n",
"split_off_solutions(\n",
" '../tutorials/python_tutorial.ipynb',\n",
" '../tutorials/python_tutorial2.ipynb'\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3400317d-5016-4dc1-a8aa-70b09f374b5d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 0c78b16

Please sign in to comment.