# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Deriving evolver and driver classes
#
# 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_attributes` list must be defined. It is a list of strings, which lists the kwargs which are allowed to be passed.

# +
import micromagneticmodel as mm

class MyDriver(mm.Driver):
    _allowed_attributes = ['arg1', 'arg2']
    
    def drive(system):
        return system


# -

# `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.

driver = MyDriver(arg1=1, arg2='value')

# The attributes are

driver.arg1

driver.arg2

# If we try to pass a keyword argument at initialisation, which is not in the `_allowed_kwargs` list, `AttributeError` is rased:

try:
    driver = MyDriver(arg3=1)
except AttributeError:
    print('Exception raised.')

# The main driver method which must be implemented by a derived class is `drive`.

try:
    driver.drive()
except NotImplementedError:
    print('Exception raised.')

StarcoderdataJupyterScriptsDedupFiltered