You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/good_practice/custom_modules.ipynb
+264-6Lines changed: 264 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,265 @@
4
4
"cell_type": "markdown",
5
5
"metadata": {},
6
6
"source": [
7
-
"# Custom modules"
7
+
"# Custom Modules"
8
+
]
9
+
},
10
+
{
11
+
"cell_type": "markdown",
12
+
"metadata": {},
13
+
"source": [
14
+
"### Pre-requisites\n",
15
+
"\n",
16
+
"* functions\n",
17
+
"* types\n",
18
+
"* collections\n",
19
+
"* packages"
20
+
]
21
+
},
22
+
{
23
+
"cell_type": "markdown",
24
+
"metadata": {},
25
+
"source": [
26
+
"# Modules\n",
27
+
"\n",
28
+
"Instead of creating one large piece of code that can often be unwieldy we can break it up into smaller chunks, or modules. Modules can be accessed through installed packages or they can written by the user. "
29
+
]
30
+
},
31
+
{
32
+
"cell_type": "markdown",
33
+
"metadata": {},
34
+
"source": [
35
+
"## Custom Modules"
36
+
]
37
+
},
38
+
{
39
+
"cell_type": "markdown",
40
+
"metadata": {},
41
+
"source": [
42
+
"Below is an example of a custom module (i.e. one we have written ourselves) called `print_things.py`:"
"An alternative to using `import` is to use the `from` statement. This allows us to select certain objects from our module rather than importing all of them in one go:"
"However, if we do want to `import` all objects in our `print_things` module with the `from` statement we can do so like this:\n",
149
+
"\n",
150
+
"```python\n",
151
+
"from print_things import *\n",
152
+
"```\n",
153
+
"\n",
154
+
"Using this style of import, if there are any modules that we don't want to be imported (i.e. that we only want to be used within the module itself), we can prefix them with an underscore (`_`).\n",
"Here, `_private_function()` can only be used within the `print_things.py` module. We can test this by importing the `print_things.py` module and trying to use it:"
170
+
]
171
+
},
172
+
{
173
+
"cell_type": "code",
174
+
"execution_count": null,
175
+
"metadata": {},
176
+
"outputs": [],
177
+
"source": [
178
+
"# Import all of the functions from the print_things.py module\n",
"Another useful way to use both the `import` / `from` statements is to `import` modules and use an alternative name:\n",
220
+
"\n",
221
+
"```python\n",
222
+
"from print_things import print_names as pn # Import print_names from the print_things module \n",
223
+
"import print_things as pt # Import the whole print_things module\n",
224
+
"``` "
225
+
]
226
+
},
227
+
{
228
+
"cell_type": "markdown",
229
+
"metadata": {},
230
+
"source": [
231
+
"## Organising Modules: Packages"
232
+
]
233
+
},
234
+
{
235
+
"cell_type": "markdown",
236
+
"metadata": {},
237
+
"source": [
238
+
"As a project grows, the number of individual modules may increase and can often be hard to manage. This is where Python packages come in. \n",
239
+
"\n",
240
+
"Packages are collections of modules contained within a directory. However, in order to make sure this directory is recognised as a package it needs to contain a file named `__init__.py`, this allows it to be imported (just like the `print_things.py` module we created earlier)."
241
+
]
242
+
},
243
+
{
244
+
"cell_type": "markdown",
245
+
"metadata": {},
246
+
"source": [
247
+
"For example if we had a package called `pkg` that contained two modules we will name `module2.py` and `module1.py` we could `import` them as:\n",
248
+
"\n",
249
+
"```python\n",
250
+
"import pkg.module1, pkg.module2\n",
251
+
"```"
252
+
]
253
+
},
254
+
{
255
+
"cell_type": "markdown",
256
+
"metadata": {},
257
+
"source": [
258
+
"### Location"
259
+
]
260
+
},
261
+
{
262
+
"cell_type": "markdown",
263
+
"metadata": {},
264
+
"source": [
265
+
"An important point that applies to packages (and by extension modules) is their location. Most of the time a package manager will sort out the location of installed packages so they are useable. Additionally, Python will also look in your current working directory for any [custom packages](https://pythoninchemistry.org/intro_python_chemists/advanced/custom_packages.html). \n"
0 commit comments