8000 Use pathlib · jerry-git/learn-python3@5f305d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f305d7

Browse files
committed
Use pathlib
1 parent 1409ba8 commit 5f305d7

File tree

5 files changed

+55
-72
lines changed

5 files changed

+55
-72
lines changed

notebooks/beginner/exercises/10_file_io_exercise.ipynb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"source": [
1111
"# EXECUTE THIS ONE FIRST!\n",
1212
"\n",
13-
"import os\n",
13+
"from pathlib import Path\n",
1414
"\n",
1515
"# Constants for the exercises:\n",
16-
"WORKING_DIR = os.getcwd()\n",
17-
"DATA_DIR = os.path.join(os.path.dirname(WORKING_DIR), \"data\")"
16+
"WORKING_DIR = Path.cwd()\n",
17+
"DATA_DIR = WORKING_DIR.parent / \"data\""
1818
]
1919
},
2020
{
@@ -32,12 +32,12 @@
3232
"outputs": [],
3333
"source": [
3434
"def sum_numbers_in_file(input_file):\n",
35-
" sum_ = 0 # A common way to use variable names that collide with built-in/keyword words is to add underscore\n",
36-
" with ____(input_file, ____) as ____:\n",
35+
" total = 0\n",
36+
" with ____(input_file) as ____:\n",
3737
" for line in ____:\n",
3838
" ____ = line.strip() # Remove potential white space\n",
39-
" ____ += float(____)\n",
40-
" return ____"
39+
" total += float(____)\n",
40+
" return _____"
4141
]
4242
},
4343
{
@@ -48,7 +48,7 @@
4848
},
4949
"outputs": [],
5050
"source": [
51-
"in_file = os.path.join(DATA_DIR, \"numbers.txt\")\n",
51+
"in_file = DATA_DIR / \"numbers.txt\"\n",
5252
"assert sum_numbers_in_file(in_file) == 189.5"
5353
]
5454
},
@@ -77,8 +77,8 @@
7777
},
7878
"outputs": [],
7979
"source": [
80-
"in_file1 = os.path.join(DATA_DIR, \"simple_file.txt\")\n",
81-
"in_file2 = os.path.join(DATA_DIR, \"simple_file_with_empty_lines.txt\")\n",
80+
"in_file1 = DATA_DIR / \"simple_file.txt\"\n",
81+
"in_file2 = DATA_DIR / \"simple_file_with_empty_lines.txt\"\n",
8282
"\n",
8383
"expected_file_1 = [\"First\", \"Second\", \"Third\", \"And\"]\n",
8484
"assert find_first_words(in_file1) == expected_file_1\n",
@@ -89,8 +89,9 @@
8989
}
9090
],
9191
"metadata": {
92+
"celltoolbar": "Edit Metadata",
9293
"kernelspec": {
93-
"display_name": "Python 3",
94+
"display_name": "Python 3 (ipykernel)",
9495
"language": "python",
9596
"name": "python3"
9697
},
@@ -104,7 +105,7 @@
104105
"name": "python",
105106
"nbconvert_exporter": "python",
106107
"pygments_lexer": "ipython3",
107-
"version": "3.5.4"
108+
"version": "3.11.0"
108109
}
109110
},
110111
"nbformat": 4,

notebooks/beginner/exercises/19_recap2_exercise.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@
128128
},
129129
"outputs": [],
130130
"source": [
131-
"import os\n",
131+
"from pathlib import Path\n",
132132
"\n",
133-
"WORKING_DIR = os.getcwd()\n",
134-
"DATA_DIR = os.path.join(os.path.dirname(WORKING_DIR), \"data\")\n",
135-
"DATA_FILE = os.path.join(DATA_DIR, \"random_data.txt\")\n",
133+
"WORKING_DIR = Path.cwd()\n",
134+
"DATA_DIR = WORKING_DIR.parent / \"data\"\n",
135+
"DATA_FILE = DATA_DIR / \"random_data.txt\"\n",
136136
"\n",
137137
"da = DataAnalyzer(DATA_FILE)\n",
138138
"\n",
@@ -171,7 +171,7 @@
171171
},
172172
"outputs": [],
173173
"source": [
174-
"EMPTY_FILE = os.path.join(DATA_DIR, \"empty_file.txt\")\n",
174+
"EMPTY_FILE = DATA_DIR / \"empty_file.txt\"\n",
175175
"try:\n",
176176
" da_empty = DataAnalyzer(EMPTY_FILE)\n",
177177
"except NoData:\n",
@@ -183,7 +183,7 @@
183183
],
184184
"metadata": {
185185
"kernelspec": {
186-
"display_name": "Python 3",
186+
"display_name": "Python 3 (ipykernel)",
187187
"language": "python",
188188
"name": "python3"
189189
},
@@ -197,7 +197,7 @@
197197
"name": "python",
198198
"nbconvert_exporter": "python",
199199
"pygments_lexer": "ipython3",
200-
"version": "3.5.4"
200+
"version": "3.11.0"
201201
}
202202
},
203203
"nbformat": 4,

notebooks/beginner/notebooks/10_file_io.ipynb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
"metadata": {},
2222
"outputs": [],
2323
"source": [
24-
"import os\n",
24+
"from pathlib import Path\n",
2525
"\n",
26-
"current_file = os.path.realpath(\"file_io.ipynb\")\n",
26+
"current_file = Path(\"file_io.ipynb\").resolve()\n",
2727
"print(f\"current file: {current_file}\")\n",
28-
"# Note: in .py files you can get the path of current file by __file__\n",
28+
"# Note: in .py files you can get the path of current file by Path(__file__)\n",
2929
"\n",
30-
"current_dir = os.path.dirname(current_file)\n",
30+
"current_dir = current_file.parent\n",
3131
"print(f\"current directory: {current_dir}\")\n",
32-
"# Note: in .py files you can get the dir of current file by os.path.dirname(__file__)\n",
3332
"\n",
34-
"data_dir = os.path.join(os.path.dirname(current_dir), \"data\")\n",
33+
"data_dir = current_dir.parent / \"data\"\n",
3534
"print(f\"data directory: {data_dir}\")"
3635
]
3736
},
@@ -48,9 +47,9 @@
4847
"metadata": {},
4948
"outputs": [],
5049
"source": [
51-
"print(f\"exists: {os.path.exists(data_dir)}\")\n",
52-
"print(f\"is file: {os.path.isfile(data_dir)}\")\n",
53-
"print(f\"is directory: {os.path.isdir(data_dir)}\")"
50+
"print(f\"exists: {data_dir.exists()}\")\n",
51+
"print(f\"is file: {data_dir.is_file()}\")\n",
52+
"print(f\"is directory: {data_dir.is_dir()}\")"
5453
]
5554
},
5655
{
@@ -66,7 +65,7 @@
6665
"metadata": {},
6766
"outputs": [],
6867
"source": [
69-
"file_path = os.path.join(data_dir, \"simple_file.txt\")\n",
68+
"file_path = data_dir / \"simple_file.txt\"\n",
7069
"\n",
7170
"with open(file_path) as simple_file:\n",
7271
" for line in simple_file:\n",
@@ -90,7 +89,7 @@
9089
"metadata": {},
9190
"outputs": [],
9291
"source": [
93-
"file_path = os.path.join(data_dir, \"simple_file.txt\")\n",
92+
"file_path = data_dir / \"simple_file.txt\"\n",
9493
"\n",
9594
"# THIS IS NOT THE PREFERRED WAY\n",
9695
"simple_file = open(file_path)\n",
@@ -112,7 +111,7 @@
112111
"metadata": {},
113112
"outputs": [],
114113
"source": [
115-
"new_file_path = os.path.join(data_dir, \"new_file.txt\")\n",
114+
"new_file_path = data_dir / \"new_file.txt\"\n",
116115
"\n",
117116
"with open(new_file_path, \"w\") as my_file:\n",
118117
" my_file.write(\"This is my first file that I wrote with Python.\")"
@@ -131,8 +130,8 @@
131130
"metadata": {},
132131
"outputs": [],
133132
"source": [
134-
"if os.path.exists(new_file_path): # make sure it's there\n",
135-
" os.remove(new_file_path)"
133+
"if new_file_path.exists(): # make sure it's there\n",
134+
" new_file_path.unlink()"
136135
]
137136
}
138137
],

notebooks/beginner/notebooks/15_std_lib.ipynb

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,7 @@
3131
"\n",
3232
"# You can access any value separately:\n",
3333
"print(\n",
34-
" \"{} {} {} {} {} {}\".format(\n",
35-
" local_now.year,\n",
36-
" local_now.month,\n",
37-
" local_now.day,\n",
38-
" local_now.hour,\n",
39-
" local_now.minute,\n",
40-
" local_now.second,\n",
41-
" )\n",
34+
" f\"{local_now.year} {local_now.month} {local_now.day} {local_now.hour} {local_now.minute} {local_now.second}\"\n",
4235
")\n",
4336
"\n",
4437
"print(f\"date: {local_now.date()}\")\n",
@@ -111,19 +104,7 @@
111104
"cell_type": "markdown",
112105
"metadata": {},
113106
"source": [
114-
"### Working with timezones\n",
115-
"Let's first make sure [`pytz`](http://pytz.sourceforge.net/) is installed."
116-
]
117-
},
118-
{
119-
"cell_type": "code",
120-
"execution_count": null,
121-
"metadata": {},
122-
"outputs": [],
123-
"source": [
124-
"import sys\n",
125-
"\n",
126-
"!{sys.executable} -m pip install pytz"
107+
"### Working with timezones"
127108
]
128109
},
129110
{
@@ -133,23 +114,23 @@
133114
"outputs": [],
134115
"source": [
135116
"import datetime as dt\n",
136-
"import pytz\n",
117+
"from zoneinfo import ZoneInfo\n",
137118
"\n",
138119
"naive_utc_now = dt.datetime.utcnow()\n",
139120
"print(f\"naive utc now: {naive_utc_now}, tzinfo: {naive_utc_now.tzinfo}\")\n",
140121
"\n",
141122
"# Localizing naive datetimes\n",
142-
"UTC_TZ = pytz.timezone(\"UTC\")\n",
143-
"utc_now = UTC_TZ.localize(naive_utc_now)\n",
123+
"UTC_TZ = ZoneInfo(\"UTC\")\n",
124+
"utc_now = naive_utc_now.replace(tzinfo=UTC_TZ)\n",
144125
"print(f\"utc now: {utc_now}, tzinfo: {utc_now.tzinfo}\")\n",
145126
"\n",
146127
"# Converting localized datetimes to different timezone\n",
147-
"PARIS_TZ = pytz.timezone(\"Europe/Paris\")\n",
148-
"paris_now = PARIS_TZ.normalize(utc_now)\n",
128+
"PARIS_TZ = ZoneInfo(\"Europe/Paris\")\n",
129+
"paris_now = utc_now.astimezone(PARIS_TZ)\n",
149130
"print(f\"Paris: {paris_now}, tzinfo: {paris_now.tzinfo}\")\n",
150131
"\n",
151-
"NEW_YORK_TZ = pytz.timezone(\"America/New_York\")\n",
152-
"ny_now = NEW_YORK_TZ.normalize(utc_now)\n",
132+
"NEW_YORK_TZ = ZoneInfo(\"America/New_York\")\n",
133+
"ny_now = utc_now.astimezone(NEW_YORK_TZ)\n",
153134
"print(f\"New York: {ny_now}, tzinfo: {ny_now.tzinfo}\")"
154135
]
155136
},
@@ -248,8 +229,8 @@
248229
"metadata": {},
249230
"outputs": [],
250231
"source": [
251-
"import os\n",
252232
"import logging\n",
233+
"from pathlib import Path\n",
253234
"\n",
254235
"# This is only required for Jupyter notebook environment\n",
255236
"from importlib import reload\n",
@@ -259,7 +240,7 @@
259240
"logger = logging.getLogger(\"MyFileLogger\")\n",
260241
"\n",
261242
"# Let's define a file_handler for our logger\n",
262-
"log_path = os.path.join(os.getcwd(), \"my_log.txt\")\n",
243+
"log_path = Path.cwd() / \"my_log.txt\"\n",
263244
"file_handler = logging.FileHandler(log_path)\n",
264245
"\n",
265246
"# And a nice format\n",
@@ -401,7 +382,7 @@
401382
"name": "python",
402383
"nbconvert_exporter": "python",
403384
"pygments_lexer": "ipython3",
404-
"version": "3.10.3"
385+
"version": "3.11.0"
405386
}
406387
},
407388
"nbformat": 4,

notebooks/intermediate/exercises/05_idiomatic_python_exercise.ipynb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,15 @@
260260
},
261261
"outputs": [],
262262
"source": [
263-
"import os\n",
263+
"from pathlib import Path\n",
264264
"\n",
265-
"CURRENT_DIR = os.getcwd()\n",
266-
"DATA_DIR = os.path.join(os.path.dirname(CURRENT_DIR), \"data\")\n",
265+
"CURRENT_DIR = Path.cwd()\n",
266+
"DATA_DIR = CURRENT_DIR.parent / \"data\"\n",
267+
"\n",
268+
"DATA_FILE1 = DATA_DIR / \"misc_data1.txt\"\n",
269+
"DATA_FILE2 = DATA_DIR / \"misc_data2.txt\"\n",
270+
"DATA_FILE3 = DATA_DIR / \"empty.txt\"\n",
267271
"\n",
268-
"DATA_FILE1 = os.path.join(DATA_DIR, \"misc_data1.txt\")\n",
269-
"DATA_FILE2 = os.path.join(DATA_DIR, \"misc_data2.txt\")\n",
270-
"DATA_FILE3 = os.path.join(DATA_DIR, \"empty.txt\")\n",
271272
"\n",
272273
"expected1 = \"\"\"missing values: 2\n",
273274
"highest number: 99.0\n",
@@ -317,8 +318,9 @@
317318
}
318319
],
319320
"metadata": {
321+
"celltoolbar": "Edit Metadata",
320322
"kernelspec": {
321-
"display_name": "Python 3",
323+
"display_name": "Python 3 (ipykernel)",
322324
"language": "python",
323325
"name": "python3"
324326
},
@@ -332,7 +334,7 @@
332334
"name": "python",
333335
"nbconvert_exporter": "python",
334336
"pygments_lexer": "ipython3",
335-
"version": "3.5.4"
337+
"version": "3.11.0"
336338
}
337339
},
338340
"nbformat": 4,

0 commit comments

Comments
 (0)
0