{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modules and Packages\n", "\n", "## Overview, Objectives, and Key Terms\n", " \n", "Python modules offer a unique way to organize your code, especially as it evolves into several functions. Moreover, any of the external packages you've already used, like NumPy and Matplotlib, are organized as modules within larger *packages*. By the end of this lesson, you'll understand how to organize your create and \n", "use your own modules and packages.\n", "\n", "\n", "### Objectives\n", "\n", "By the end of this lesson, you should be able to\n", "\n", "- Produce a module of functions\n", "- Set appropriate paths in order to import modules you create\n", "\n", "### Key Terms\n", "\n", "- `module`\n", "- `import`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Modules\n", "\n", "From nearly the beginning, we've imported modules like NumPy and used the functions they provide. In fact, a `module`\n", "is an actual Python type:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "module" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy\n", "type(numpy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modules are easy to create: they can be as simple as a single Python script. Suppose we write a few functions for analyzing a vector $\\mathbf{e}$ that represents errors (perhaps due to measurement uncertainty or\n", "numerical approximation). Some useful error metrics include \n", "\n", " 1. *mean, absolute error*, $\\sum^n_i |e_i|/n$\n", " 2. *root-mean-square error*, $\\sqrt{\\sum^n_i e_i^2/n}$\n", " 3. *maximum, absolute error*, $\\max(|e_i|) \\, \\forall i \\in [1, n]$\n", "\n", "If $\\mathbf{e}$ is a *sequential* type, e.g., a `list`, `tuple`, or `np.ndarray`, then the following functions represent possible implementations of each metric:\n", "\n", "```python\n", "def mean_abs_error(e):\n", " \"\"\"Mean, absolute error.\"\"\"\n", " v = 0\n", " for i in range(len(e)) :\n", " v += abs(e[i])\n", " return v/len(e)\n", " \n", "def rms_error(e) :\n", " \"\"\"Root-mean-square error.\"\"\"\n", " v = 0\n", " for i in range(len(e)) :\n", " v += e[i]**2\n", " return v**0.5\n", " \n", "def max_abs_error(e) :\n", " \"\"\"Maximum, absolute error.\"\"\"\n", " v = 0\n", " for i in range(len(e)) :\n", " if abs(e[i]) > v:\n", " v = abs(e[i])\n", " return v\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although we can define these (and any other function) basically anywhere we want in a Python script, it can be helpful to collect these functions in a separate file, say `error_metrics.py`. Now, if you navigate in the command line to the directory that contains this file, you can list the directory contents and see something similar to the following:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```PowerShell\n", " C:\\Users\\robertsj\\Documents\\PythonForEngineers>dir\n", " Volume in drive C has no label.\n", " Volume Serial Number is CP-1\n", "\n", " Directory of C:\\Users\\robertsj\\Documents\\PythonForEngineers\n", "\n", " 12/02/1942 03:25 PM