{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Using Debuggingbook Code in your own Programs\n", "\n", "This notebook has instructions on how to use the `debuggingbook` code in your own programs." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "In short, there are three ways:\n", "\n", "1. Simply run the notebooks in your browser, using the \"mybinder\" environment. Choose \"Resources->Edit as Notebook\" in any of the `fuzzingbook.org` pages; this will lead you to a preconfigured Jupyter Notebook environment where you can toy around at your leisure.\n", "2. Import the code for your own Python programs. Using `pip install fuzzingbook`, you can install all code and start using it from your own code. See \"Can I import the code for my own Python projects?\", below.\n", "3. Download or check out the code and/or the notebooks from the project site. This allows you to edit and run all things locally. However, be sure to also install the required packages; see below for details." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Can I import the code for my own Python projects?\n", "\n", "Yes, you can! (If you like Python, that is.) We provide a `debuggingbook` Python package that you can install using the `pip` package manager:\n", "\n", "```shell\n", "$ pip install debuggingbook\n", "```\n", "\n", "As of `debuggingbook 1.1`, this is set up such that additional required Python packages are also installed. However, also see \"Install Additional Non-Python Packages\" below.\n", "\n", "Once `pip` is complete, you can import individual classes, constants, or functions from each notebook using\n", "\n", "```python\n", ">>> from debuggingbook. import \n", "```\n", "\n", "where `` is the name of the class, constant, or function to use, and `` is the name of the respective notebook. (If you read this at debuggingbook.org, then the notebook name is the identifier preceding `\".html\"` in the URL).\n", "\n", "Here is an example importing `Debugger` from [the chapter on debuggers](Debugger.ipynb), whose notebook name is `Debugger`:\n", "\n", "```python\n", ">>> from debuggingbook.Debugger import Debugger\n", ">>> with Debugger():\n", " function_to_be_observed()\n", "```\n", "\n", "The \"Synopsis\" section at the beginning of a chapter gives a short survey on useful code features you can use." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Which OS and Python versions are required?\n", "\n", "As of `debuggingbook 1.1`, Python 3.9 and later is required. Specifically, we use Python 3.9.7 for development and testing. This is also the version to be used if you check out the code from git, and the version you get if you use the debugging book within the \"mybinder\" environment.\n", "\n", "To use the `debuggingbook` code with earlier Python versions, use\n", "\n", "```shell\n", "$ pip install 'debuggingbook=1.0.1'\n", "```\n", "\n", "Our notebooks generally assume a Unix-like environment; the code is tested on Linux and macOS. System-independent code may also run on Windows." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Can I use the code from within a Jupyter notebook?\n", "\n", "Yes, you can! First, you install the `debuggingbook` package (as above); you can then access all code right from your notebook.\n", "\n", "Another way to use the code is to _import the notebooks directly_. Download the notebooks from the menu. Then, add your own notebooks into the same folder. After importing `bookutils`, you can then simply import the code from other notebooks, just as our own notebooks do.\n", "\n", "Here is again the above example, importing `Debugger` from [the chapter on debuggers](Debugger.ipynb) – but now from a notebook:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:03:05.590017Z", "iopub.status.busy": "2023-11-12T13:03:05.589892Z", "iopub.status.idle": "2023-11-12T13:03:05.623382Z", "shell.execute_reply": "2023-11-12T13:03:05.622958Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import bookutils.setup" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:03:05.625789Z", "iopub.status.busy": "2023-11-12T13:03:05.625549Z", "iopub.status.idle": "2023-11-12T13:03:05.738809Z", "shell.execute_reply": "2023-11-12T13:03:05.738490Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from Debugger import Debugger" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:03:05.740959Z", "iopub.status.busy": "2023-11-12T13:03:05.740794Z", "iopub.status.idle": "2023-11-12T13:03:05.742867Z", "shell.execute_reply": "2023-11-12T13:03:05.742387Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "with Debugger():\n", " x = 1 + 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "If you'd like to share your notebook, let us know; we can integrate it in the repository or even in the book." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Can I check out the code from git and get the latest and greatest?\n", "\n", "Yes, you can! We have a few continuous integration (CI) workflows running which do exactly that. After cloning the repository from [the project page](https://github.com/uds-se/debuggingbook/) and installing the additional packages (see below), you can `cd` into `notebooks` and start `jupyter` right away!\n", "\n", "There also is a `Makefile` provided with literally hundreds of targets; most important are the ones we also use in continuous integration:\n", "\n", "* `make check-imports` checks whether your code is free of syntax errors\n", "* `make check-style` checks whether your code is free of type errors\n", "* `make check-code` runs all derived code, testing it\n", "* `make check-notebooks` runs all notebooks, testing them\n", "\n", "If you want to contribute to the project, ensure that the above tests run through.\n", "\n", "The `Makefile` has many more, often experimental, targets. `make markdown` creates a `.md` variant in `markdown/`, and there's also `make word` and `make epub`, which are set to create Word and EPUB variants (with mixed results). Try `make help` for commonly used targets." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Can I just run the Python code? I mean, without Notebooks?\n", "\n", "Yes, you can! You can download the code as Python programs; simply select \"Resources → Download Code\" for one chapter or \"Resources → All Code\" for all chapters. These code files can be executed, yielding (hopefully) the same results as the notebooks.\n", "\n", "The code files can also be edited if you wish, but (a) they are very obviously generated from notebooks, (b) therefore not much fun to work with, and (c) if you fix any errors, you'll have to back-propagate them to the notebook before you can make a pull request. Use code files only under severely constrained circumstances.\n", "\n", "If you only want to **use** the Python code, install the code package (see below)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Which other Packages do I need to use the Python Modules?\n", "\n", "We have attempted to limit the dependencies to a minimum (sometimes using ugly hacks). Generally speaking, if you encounter that a module `X` is not found, just do `pip install X`. Most notebooks only need modules that are part of the standard Python library.\n", "\n", "For a full list of dependencies, there are two sources." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Step 1: Install Required Python Packages\n", "\n", "The [`requirements.txt` file within the project root folder](https://github.com/uds-se/debuggingbook/tree/master/) lists all _Python packages required_.\n", "\n", "You can do\n", "\n", "```sh\n", "$ pip install -r requirements.txt\n", "```\n", "\n", "to install all required packages (but using `pipenv` is preferred; see below)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "### Step 2: Install Additional Non-Python Packages\n", "\n", "The [`apt.txt` file in the `binder/` folder](https://github.com/uds-se/debuggingbook/tree/master/binder) lists all _Linux_ packages required.\n", "\n", "In most cases, however, it suffices to install the `dot` graph drawing program (part of the `graphviz` package). Here are some instructions:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### Installing Graphviz on Linux\n", "\n", "```sh\n", "$ sudo apt-get install graphviz\n", "```\n", "\n", "to install it. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### Installing Graphviz on macOS\n", "\n", "On macOS, if you use `conda`, run\n", "\n", "```sh\n", "$ conda install graphviz\n", "```\n", "\n", "If you use HomeBrew, run\n", "\n", "```sh\n", "$ brew install graphviz\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Installing the Debuggingbook in an Isolated Environment\n", "\n", "If you wish to install the debuggingbook in an environment that is isolated from your system interpreter,\n", "we recommend using [Pipenv](https://pipenv.pypa.io/), which can automatically create a so called *virtual environment* hosting all required packages.\n", "\n", "To accomplish this, please follow these steps:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Step 1: Install PyEnv\n", "\n", "Optionally install `pyenv` following the [official instructions](https://github.com/pyenv/pyenv#installation) if you are on a Unix operating system.\n", "If you are on Windows, consider using [pyenv-win](https://github.com/pyenv-win/pyenv-win) instead.\n", "This will allow you to seamlessly install any version of Python." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Step 2: Install PipEnv\n", "\n", "Install Pipenv following the official [installation instructions](https://pypi.org/project/pipenv/).\n", "If you have `pyenv` installed, Pipenv can automatically download and install the appropriate version of the Python distribution.\n", "Otherwise, Pipenv will use your system interpreter, which may or may not be the right version." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Step 3: Install Python Packages\n", "\n", "Run\n", "\n", "```sh\n", "$ pipenv install -r requirements.txt\n", "``` \n", "\n", "in the `debuggingbook` root directory." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Step 4: Install Additional Non-Python Packages\n", "\n", "See above for instructions on how to install additional non-python packages." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Step 5: Enter the Environment\n", "\n", "Enter the environment with \n", "\n", "```sh\n", "$ pipenv shell\n", "```\n", "\n", "where you can now execute\n", "\n", "```sh\n", "$ make -k check-code\n", "```\n", "\n", "to run the tests." ] } ], "metadata": { "ipub": { "bibliography": "fuzzingbook.bib", "toc": true }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true }, "toc-autonumbering": false }, "nbformat": 4, "nbformat_minor": 4 }