|
16 | 16 | "id": "eb6b0843-d4d9-4f63-b155-11739ba90398", |
17 | 17 | "metadata": {}, |
18 | 18 | "source": [ |
19 | | - "Every Python installation has a number of modules that come by default.\n", |
| 19 | + "Every Python installation includes a number of modules by default.\n", |
20 | 20 | "They are called collectively the \"Python Standard Library\" or \"built-in\" modules.\n", |
21 | 21 | "\n", |
22 | 22 | "Even though these modules are available in our computer, we cannot directly use them, we need to import them into our current namespace to actually use them.\n", |
|
51 | 51 | "id": "0ca12de9-9840-417c-b41f-fd3032093c12", |
52 | 52 | "metadata": {}, |
53 | 53 | "source": [ |
54 | | - "That means that the word pathlib is unknown in this context.\n", |
| 54 | + "That means that the word pathlib is unknown in this context (namespace). \n", |
55 | 55 | "Let's bring it in:" |
56 | 56 | ] |
57 | 57 | }, |
|
99 | 99 | "id": "a12b0f16-b0d7-45fe-b3fe-1d443199a117", |
100 | 100 | "metadata": {}, |
101 | 101 | "source": [ |
102 | | - "We can also import only specific parts of the module:\n", |
103 | | - "\n", |
104 | | - "```python\n", |
105 | | - "from pathlib import Path\n", |
106 | | - "```" |
| 102 | + "We can also import only specific parts of the module (submodules):" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": null, |
| 108 | + "id": "15e3f4c3-4509-46b8-8a86-380a2f4131df", |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "from pathlib import Path" |
107 | 113 | ] |
108 | 114 | }, |
109 | 115 | { |
|
150 | 156 | "id": "3b452c0b-682f-4868-9fd6-4dc6f7a5ff69", |
151 | 157 | "metadata": {}, |
152 | 158 | "source": [ |
153 | | - "## Script vs. exportable code\n", |
154 | | - "A Python module (any `.py` file) might contain code that we want to run (for example as a one-off script) along code that we only want to use somewhere else. \n", |
155 | | - "For example, we might have a bunch of code like this in a file:\n", |
| 159 | + "## Script vs. Importable Code\n", |
| 160 | + "We can also import code that we wrote ourselves with the same syntax.\n", |
| 161 | + "For example, if we have a python file called `mymodule.py` we can import it from another file with:\n", |
| 162 | + "\n", |
| 163 | + "```python\n", |
| 164 | + "import mymodule\n", |
| 165 | + "```\n", |
156 | 166 | "\n", |
| 167 | + "A Python module (any `.py` file) might contain code that we want to run (for example as a one-off script) along code that we only want to use somewhere else. \n", |
157 | 168 | "We can isolate the part of the code that we want to run as a script with this trick.\n", |
158 | 169 | "\n", |
| 170 | + "That's easier to show than to explain, so let's do some exercises." |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "id": "ee8413b5-b03f-456f-8298-a82feda2b076", |
| 176 | + "metadata": {}, |
| 177 | + "source": [ |
| 178 | + "## Exercises\n", |
| 179 | + "Create 2 files like these in our working project \n", |
| 180 | + "`/pycourse/src/pycourse/data.py`, `/pycourse/src/pycourse/preprocessing.py`:\n", |
| 181 | + "\n", |
| 182 | + "\n", |
159 | 183 | "```python\n", |
160 | 184 | "# data.py\n", |
| 185 | + "RAWDATA = ... # 3 dots are called \"Ellipsis\" and act as placeholder\n", |
161 | 186 | "\n", |
162 | | - "def download(url):\n", |
163 | | - " ...\n", |
164 | | - "def process(data):\n", |
| 187 | + "def fetch_data(raw_data):\n", |
165 | 188 | " ... \n", |
166 | | - "def plot(data):\n", |
167 | | - " ...\n", |
168 | | - " \n", |
| 189 | + "\n", |
169 | 190 | "if __name__ == \"__main__\":\n", |
170 | | - " print(\"I am running as a script\") \n", |
171 | | - " def script_func():\n", |
172 | | - " ...\n", |
| 191 | + " print(\"Fetching data\") \n", |
| 192 | + " data = fetch_data(RAWDATA)\n", |
| 193 | + " print(data)\n", |
| 194 | + "```\n", |
| 195 | + "\n", |
| 196 | + "```python\n", |
| 197 | + "# preprocessing.py\n", |
| 198 | + "\n", |
| 199 | + "def cleanup(data):\n", |
| 200 | + " ... \n", |
173 | 201 | " \n", |
174 | | - "```" |
| 202 | + "def preprocess_pipeline(raw_data):\n", |
| 203 | + " data = fetch_data(raw_data)\n", |
| 204 | + " return cleanup(data)\n", |
| 205 | + " \n", |
| 206 | + "if __name__ == \"__main__\":\n", |
| 207 | + " print(\"Running preprocessing\") \n", |
| 208 | + " raw_data = ...\n", |
| 209 | + " preprocess_pipeline(raw_data)\n", |
| 210 | + "```\n", |
| 211 | + "\n", |
| 212 | + "\n", |
| 213 | + "1) Copy the `RAWDATA` from the previous lab exercises into a file `data.py`\n", |
| 214 | + "2) Create another file called `preprocessing.py` in the same directory.\n", |
| 215 | + "3) Import the `RAWDATA` string inside `preprocessing.py`\n", |
| 216 | + "4) Run `data.py` as a script with `uv run src/pycourse/data.py`. Pay attention to the printed output\n", |
| 217 | + "5) Run preprocessing as a script with `uv run src/pycourse/preprocessing.py`. Pay attention to the printed output. What difference do you see compared to the previous point?" |
175 | 218 | ] |
176 | 219 | }, |
177 | 220 | { |
178 | 221 | "cell_type": "markdown", |
179 | | - "id": "ee8413b5-b03f-456f-8298-a82feda2b076", |
| 222 | + "id": "1add3a63-5817-4a9c-bfcb-d041ab25eb79", |
180 | 223 | "metadata": {}, |
181 | 224 | "source": [ |
182 | | - "## Exercises\n", |
183 | | - "1) Copy the content of the code above into a file `data.py` and run it\n", |
184 | | - "2) Create another file called `analysis.py` right next to `data.py` (in the same directory)\n", |
185 | | - "3) Inside `analysis.py` import `download` from `data.py` and run `analysis.py`\n", |
186 | | - "4) Inside `analysis.py` import `script_func` from `data.py` and run `analysis.py`. Do you understand what happens?" |
| 225 | + "Congratulations! You just ran your first python scripts 🚀" |
187 | 226 | ] |
188 | 227 | } |
189 | 228 | ], |
|
203 | 242 | "name": "python", |
204 | 243 | "nbconvert_exporter": "python", |
205 | 244 | "pygments_lexer": "ipython3", |
206 | | - "version": "3.10.13" |
| 245 | + "version": "3.13.3" |
207 | 246 | } |
208 | 247 | }, |
209 | 248 | "nbformat": 4, |
|
0 commit comments