Skip to content

Commit 0ee9be4

Browse files
committed
Split up more cells. Put planned output into gitignore
1 parent e1a35e8 commit 0ee9be4

File tree

3 files changed

+265
-71
lines changed

3 files changed

+265
-71
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ test_folder/output.txt
77

88
__pycache__/
99
*.pyc
10-
plant_log.csv
10+
plant_log.csv
11+
calculator.py
12+
example.csv

Intermediate.ipynb

Lines changed: 114 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5291,7 +5291,7 @@
52915291
{
52925292
"cell_type": "code",
52935293
"execution_count": null,
5294-
"id": "acfd6a4d",
5294+
"id": "cf38baf4-5486-4c17-bc93-42510f8a6db7",
52955295
"metadata": {
52965296
"editable": true,
52975297
"slideshow": {
@@ -5303,55 +5303,120 @@
53035303
"source": [
53045304
"from pathlib import Path\n",
53055305
"# Create a Path object for the current directory\n",
5306-
"current_directory = Path('.')\n",
5307-
"\n",
5306+
"current_directory = Path('.')"
5307+
]
5308+
},
5309+
{
5310+
"cell_type": "code",
5311+
"execution_count": null,
5312+
"id": "47affbe3-0eb6-4ed6-8717-3ef05b072a80",
5313+
"metadata": {
5314+
"editable": true,
5315+
"slideshow": {
5316+
"slide_type": "fragment"
5317+
},
5318+
"tags": []
5319+
},
5320+
"outputs": [],
5321+
"source": [
53085322
"# list all ipynb files in the current directory\n",
53095323
"for file in current_directory.glob('*.ipynb'):\n",
5310-
" print(file.name)\n",
5311-
"\n",
5312-
"## We can also list all files in the current directory (commented as the might be long)\n",
5313-
"# for file in current_directory.iterdir():\n",
5314-
"# print(file.name)\n",
5315-
"\n",
5316-
"# Check whether a file exists\n",
5324+
" print(file.name)"
5325+
]
5326+
},
5327+
{
5328+
"cell_type": "code",
5329+
"execution_count": null,
5330+
"id": "98714766-5033-43d8-a9a3-0589b7bf32ec",
5331+
"metadata": {
5332+
"editable": true,
5333+
"slideshow": {
5334+
"slide_type": "fragment"
5335+
},
5336+
"tags": []
5337+
},
5338+
"outputs": [],
5339+
"source": [
5340+
"# We can also list all files in the current directory\n",
5341+
"for file in current_directory.iterdir():\n",
5342+
" print(file.name)"
5343+
]
5344+
},
5345+
{
5346+
"cell_type": "code",
5347+
"execution_count": null,
5348+
"id": "3fe290ad-e12f-4021-b8ef-a6d9bd6a7467",
5349+
"metadata": {
5350+
"editable": true,
5351+
"slideshow": {
5352+
"slide_type": "fragment"
5353+
},
5354+
"tags": []
5355+
},
5356+
"outputs": [],
5357+
"source": [
5358+
"# go to subfolders and files in these folders using /\n",
53175359
"file_path = current_directory / 'data' / 'data_file.txt'\n",
5360+
"\n",
5361+
"# check whether a file exists\n",
53185362
"if file_path.exists():\n",
53195363
" print(f\"The file {file_path} exists.\")"
53205364
]
53215365
},
53225366
{
53235367
"cell_type": "code",
53245368
"execution_count": null,
5325-
"id": "04329a55",
5369+
"id": "cdcbc0b1-197d-4fc5-95e9-9501868b7525",
53265370
"metadata": {
53275371
"editable": true,
53285372
"slideshow": {
5329-
"slide_type": ""
5373+
"slide_type": "fragment"
53305374
},
53315375
"tags": []
53325376
},
53335377
"outputs": [],
53345378
"source": [
53355379
"# Create a new test folder\n",
53365380
"new_folder = current_directory / 'test_folder'\n",
5337-
"new_folder.mkdir(exist_ok=True) # exist_ok=True prevents error if folder already exists\n",
5338-
"\n",
5339-
"# Create a new file in the test folder\n",
5381+
"new_folder.mkdir(exist_ok=True) # exist_ok=True prevents error if folder already exists"
5382+
]
5383+
},
5384+
{
5385+
"cell_type": "code",
5386+
"execution_count": null,
5387+
"id": "b33c4e26-ec82-405d-97a5-bd88488c06d8",
5388+
"metadata": {
5389+
"editable": true,
5390+
"slideshow": {
5391+
"slide_type": "fragment"
5392+
},
5393+
"tags": []
5394+
},
5395+
"outputs": [],
5396+
"source": [
5397+
"# Write text to a file in test_folder\n",
53405398
"output = \"This is some output\\nSecond line!\"\n",
53415399
"output_file = new_folder / 'output.txt'\n",
5342-
"output_file.write_text(output)\n",
5343-
"\n",
5400+
"output_file.write_text(output)"
5401+
]
5402+
},
5403+
{
5404+
"cell_type": "code",
5405+
"execution_count": null,
5406+
"id": "89847e34-7e99-4b89-904f-b7ec2c8dd774",
5407+
"metadata": {
5408+
"editable": true,
5409+
"slideshow": {
5410+
"slide_type": "fragment"
5411+
},
5412+
"tags": []
5413+
},
5414+
"outputs": [],
5415+
"source": [
53445416
"# Read back the content from the file we just created\n",
53455417
"content = output_file.read_text() # Read from the same file we created\n",
53465418
"print(\"Content of the file we just created:\")\n",
5347-
"print(content)\n",
5348-
"\n",
5349-
"# Also demonstrate reading an existing data file\n",
5350-
"data_file = current_directory / 'data' / 'data_file.txt'\n",
5351-
"if data_file.exists():\n",
5352-
" data_content = data_file.read_text()\n",
5353-
" print(f\"\\nContent of {data_file.name}:\")\n",
5354-
" print(data_content)"
5419+
"print(content)"
53555420
]
53565421
},
53575422
{
@@ -5398,10 +5463,15 @@
53985463
"source": [
53995464
"import csv, math\n",
54005465
"\n",
5466+
"# Read the file object\n",
54015467
"with open(\"example.csv\", 'w') as out_f:\n",
5468+
" # use csv.writer to interact\n",
54025469
" writer = csv.writer(out_f, delimiter=',')\n",
5470+
" # write the headings\n",
54035471
" writer.writerow([\"x_axis\", \"y_axis\"])\n",
5472+
" # generate the data for x\n",
54045473
" x_axis = [x * 0.1 for x in range(0, 100)]\n",
5474+
" # write x and cos(x) for every entry\n",
54055475
" for x in x_axis:\n",
54065476
" writer.writerow([x, math.cos(x)])\n",
54075477
"\n",
@@ -5427,7 +5497,7 @@
54275497
{
54285498
"cell_type": "code",
54295499
"execution_count": null,
5430-
"id": "b4f9954b",
5500+
"id": "7d2b75d2-f0a2-490e-be7d-28dad71ef05c",
54315501
"metadata": {
54325502
"editable": true,
54335503
"slideshow": {
@@ -5440,12 +5510,23 @@
54405510
"# Show the first few lines to demonstrate what we created\n",
54415511
"print(\"First few lines of the file:\")\n",
54425512
"with open(\"example.csv\", 'r') as f:\n",
5443-
" for i, line in enumerate(f):\n",
5444-
" if i < 5: # Show first 5 lines\n",
5445-
" print(f\" {line.strip()}\")\n",
5446-
" else:\n",
5447-
" break\n",
5448-
"\n",
5513+
" for _ in range(5):\n",
5514+
" print(f.readline().strip())"
5515+
]
5516+
},
5517+
{
5518+
"cell_type": "code",
5519+
"execution_count": null,
5520+
"id": "9af8c87f-62db-4a04-86d8-d51428395210",
5521+
"metadata": {
5522+
"editable": true,
5523+
"slideshow": {
5524+
"slide_type": ""
5525+
},
5526+
"tags": []
5527+
},
5528+
"outputs": [],
5529+
"source": [
54495530
"# Show the value of Y for X==1.0 saved in the file\n",
54505531
"print(\"The value of Y for X==1.0 saved in the file:\")\n",
54515532
"with open (\"example.csv\", 'r') as in_f:\n",
@@ -5782,7 +5863,7 @@
57825863
],
57835864
"metadata": {
57845865
"kernelspec": {
5785-
"display_name": "teaching_data_analysis",
5866+
"display_name": "Python 3 (ipykernel)",
57865867
"language": "python",
57875868
"name": "python3"
57885869
},

0 commit comments

Comments
 (0)