{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 06: Deriving Evolver and Driver classes\n", "\n", "> Interactive online tutorial:\n", "> [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ubermag/micromagneticmodel/master?filepath=docs%2Fipynb%2Findex.ipynb)\n", "\n", "In `micromagneticmodel` package, base classes `micromagneticmodel.Driver` and `micromagneticmodel.Evolver` are defined. Their purpose is to build individual evolvers and drivers in a particular micromagnetic calculator. In this tutorial, we will demonstrate some of their basic properties, on an example of a `micromagneticmodel.Driver` class. The behaviour of the `micromagneticmodel.Evolver` class is the same.\n", "\n", "Let us derive `MyDriver` class. In order to define it, `_allowed_kwargs` list must be defined. It is a list of strings, which lists the kwargs which are allowed to be passed." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import micromagneticmodel as mm\n", "\n", "class MyDriver(mm.Driver):\n", " _allowed_kwargs = ['arg1', 'arg2']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Driver` class does not require any parameters to be passed at initialisation. If a keyword argument is from `_allowed_kwargs` list, it will be assigned as a class attribute. Otherwise, `AttributeError` will be raised." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "driver = MyDriver(arg1=1, arg2='value')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The attributes are" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "driver.arg1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'value'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "driver.arg2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we try to pass a keyword argument at initialisation, which is not in the `_allowed_kwargs` list, `AttributeError` is rased:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " driver = MyDriver(arg3=1)\n", "except AttributeError:\n", " print('Exception raised.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main driver method which must be implemented by a derived class is `drive`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " driver.drive()\n", "except NotImplementedError:\n", " print('Exception raised.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other\n", "\n", "Full description of all existing descriptors can be found in the [API Reference](https://micromagneticmodel.readthedocs.io/en/latest/?badge=latest)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }