Skip to content

Instantly share code, notes, and snippets.

@fomightez
Created January 11, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/bcbeaa34f258e6ab959c842438077cb8 to your computer and use it in GitHub Desktop.
Save fomightez/bcbeaa34f258e6ab959c842438077cb8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "1f6d85f0-79cc-48af-8f71-6a2214de4dfc",
"metadata": {},
"source": [
"## For SO answer at https://stackoverflow.com/q/75075576/8508004\n",
"\n",
"Demonstrate idea in action by making two scripts that will then make three other script files each with random names and content and run each of the eight total scripts only once.\n",
"\n",
"Sessions where this notebook was developed , and thus should be able to be run in similarly spawned sessions, can be obtained by going [here](https://github.com/binder-examples/conda) and pressing `launch binder`."
]
},
{
"cell_type": "markdown",
"id": "fb8d0ed4-c72f-4726-94fe-dbb9a4cc3966",
"metadata": {},
"source": [
"### Prepare environment with anything needed already not present\n",
"\n",
"Need shortuuid ackage to make making the random scripts and content easily."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7220bdf0-f46e-4dfb-9c3c-3d5ec90e07c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: shortuuid in /srv/conda/envs/notebook/lib/python3.7/site-packages (1.0.11)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install shortuuid"
]
},
{
"cell_type": "markdown",
"id": "d3169e07-00b2-45aa-a09a-7d7514cb5ebc",
"metadata": {},
"source": [
"### Generate the two scripts that will also spawn three scripts each themselves"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8843dc11-7e12-4661-b499-7788844b180f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing 's' (str) to file 'make_three_scripts.py'.\n"
]
}
],
"source": [
"s='''script_name = 'make_three_scripts.py'\n",
"import shortuuid \n",
"for x in range(1,4):\n",
" output_file_name_prefix = shortuuid.ShortUUID().random(length=10)\n",
" output_file_name = f\"{output_file_name_prefix}.py\"\n",
" so = f\"print('I was output by script number {x} of 3 made by {script_name};\"\n",
" so += f\" generated by {output_file_name}')\"\n",
" with open(output_file_name, 'w') as output_file:\n",
" output_file.write(so)'''\n",
"%store s >make_three_scripts.py\n",
"!sed 's/make_three_scripts.py/make_three_MORE_scripts.py/g' <make_three_scripts.py >make_three_MORE_scripts.py"
]
},
{
"cell_type": "markdown",
"id": "56cd6be9-b5b6-4ab3-866e-bf7d2015883b",
"metadata": {},
"source": [
"So we should have two scripts. Let's show those here."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "328bcf04-840e-4f1a-ac7b-7def24d6a459",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"script_name = 'make_three_scripts.py'\n",
"import shortuuid \n",
"for x in range(1,4):\n",
" output_file_name_prefix = shortuuid.ShortUUID().random(length=10)\n",
" output_file_name = f\"{output_file_name_prefix}.py\"\n",
" so = f\"print('I was output by script number {x} of 3 made by {script_name};\"\n",
" so += f\" generated by {output_file_name}')\"\n",
" with open(output_file_name, 'w') as output_file:\n",
" output_file.write(so)\n"
]
}
],
"source": [
"!cat make_three_scripts.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4e69172c-a21c-400a-9edc-2c16d0016d43",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"script_name = 'make_three_MORE_scripts.py'\n",
"import shortuuid \n",
"for x in range(1,4):\n",
" output_file_name_prefix = shortuuid.ShortUUID().random(length=10)\n",
" output_file_name = f\"{output_file_name_prefix}.py\"\n",
" so = f\"print('I was output by script number {x} of 3 made by {script_name};\"\n",
" so += f\" generated by {output_file_name}')\"\n",
" with open(output_file_name, 'w') as output_file:\n",
" output_file.write(so)\n"
]
}
],
"source": [
"!cat make_three_MORE_scripts.py"
]
},
{
"cell_type": "markdown",
"id": "0959934a-eecf-4ebe-b905-369eeb9d512d",
"metadata": {},
"source": [
"Finally, having prepared everything, now we need to run each script and any Python scripts the scripts spawn once. **This next part should cover what the OP sought.**\n",
"\n",
"# Running each script once\n",
"\n",
"Option to run each script once, allowing for additional Python scripts with the `.py` extension to also be generated in the course of running the scripts."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "27d879e5-b8af-46f3-97cf-c0c50cc3e62b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I was output by script number 2 of 3 made by make_three_MORE_scripts.py; generated by CwVVW2wWFx.py\n",
"I was output by script number 3 of 3 made by make_three_scripts.py; generated by Ar2FAJMZ5Z.py\n",
"I was output by script number 1 of 3 made by make_three_MORE_scripts.py; generated by HAenGmmo57.py\n",
"I was output by script number 3 of 3 made by make_three_MORE_scripts.py; generated by 8gcHDrxzTp.py\n",
"I was output by script number 2 of 3 made by make_three_scripts.py; generated by CfmcdhKqek.py\n",
"I was output by script number 1 of 3 made by make_three_scripts.py; generated by AyUV8wpVTo.py\n",
"\n",
"\n",
"\n",
"**************------------DEMO COMPLETE------------------***************\n",
"Number of scripts run in total: 8\n",
"Number of scripts spawn dynamically and run: 6\n"
]
}
],
"source": [
"import os\n",
"import fnmatch\n",
"import glob\n",
"\n",
"\n",
"executed_scripts = []\n",
"extension_to_match = \".py\"\n",
"\n",
"def execute_script(s):\n",
" %run {s}\n",
"\n",
"while set(executed_scripts) != set(glob.glob(f\"*{extension_to_match}\")):\n",
" for file in os.listdir('.'):\n",
" if fnmatch.fnmatch(file, '*'+extension_to_match):\n",
" if file not in executed_scripts:\n",
" execute_script(file)\n",
" executed_scripts.append(file)\n",
"\n",
"print(\"\\n\\n\\n**************------------DEMO COMPLETE------------------***************\")\n",
"print(f\"Number of scripts run in total: {len(executed_scripts)}\")\n",
"scripts_spawned_dynamically_and_run = set(executed_scripts) - set([\"make_three_scripts.py\",\"make_three_MORE_scripts.py\"])\n",
"print(f\"Number of scripts spawn dynamically and run: {len(scripts_spawned_dynamically_and_run)}\")"
]
},
{
"cell_type": "markdown",
"id": "c3b16ed8-fc4f-40ed-bde5-6fd6f8d28559",
"metadata": {},
"source": [
"Reset demo and try again? Uncomment and run the next cell to reset things by removing all the scripts. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6cdebb4f-0d84-404e-945b-0d104ec89895",
"metadata": {},
"outputs": [],
"source": [
"#!rm -rf *.py"
]
},
{
"cell_type": "markdown",
"id": "dc4775d0-ae74-4ba1-9e5d-511a7f56581b",
"metadata": {},
"source": [
"Re-comment that line in the cell above again so it will be inactive again, and then you'll be ready to use 'Kernel' > 'Restart Kernel and Run All Cells' to re-run from essentially scratch again. The pip install won't be repeated; howver, everything else will be re-run."
]
},
{
"cell_type": "markdown",
"id": "75740603-85ff-48b9-ad5c-df4f6f4e222a",
"metadata": {},
"source": [
"---------------------\n",
"\n",
"Enjoy!"
]
}
],
"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.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment