#!/usr/bin/env python # coding: utf-8 # # Tutorial 06: Deriving Evolver and Driver classes # # > Interactive online tutorial: # > [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ubermag/micromagneticmodel/master?filepath=docs%2Fipynb%2Findex.ipynb) # # 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. # # 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. # In[1]: import micromagneticmodel as mm class MyDriver(mm.Driver): _allowed_kwargs = ['arg1', 'arg2'] # `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. # In[2]: driver = MyDriver(arg1=1, arg2='value') # The attributes are # In[3]: driver.arg1 # In[4]: driver.arg2 # If we try to pass a keyword argument at initialisation, which is not in the `_allowed_kwargs` list, `AttributeError` is rased: # In[5]: try: driver = MyDriver(arg3=1) except AttributeError: print('Exception raised.') # The main driver method which must be implemented by a derived class is `drive`. # In[6]: try: driver.drive() except NotImplementedError: print('Exception raised.') # ## Other # # Full description of all existing descriptors can be found in the [API Reference](https://micromagneticmodel.readthedocs.io/en/latest/?badge=latest).