Source code for mmf.utils.examples.mmf_test_example
"""Simple example module to demonstrate the testing framework.
See also the unit tests in the `tests` directory.
"""
import math
from mmf.objects import StateVars, process_vars
[docs]class Complex(StateVars):
"""A simple class representing complex numbers.
This is not a good example of a real class, but allows us to
demonstrate some features of the testing framework.
Examples
--------
Here are some doctests demonstrating the usage:
>>> c = Complex(real=0, imag=1)
>>> c
Complex(imag=1)
>>> c*c
Complex(real=-1)
"""
_state_vars = [('real', 0, "Real part"),
('imag', 0, "Imaginary part")]
process_vars()
def __init__(self, *v, **kw):
if 2 == len(v):
self.real, self.imag = v
def __mul__(self, c):
return Complex(self.real*c.real - self.imag*c.imag,
self.real*c.imag + self.imag*c.real)
def __sub__(self, c):
return Complex(self.real - c.real, self.imag - c.imag)
def __abs__(self):
return math.sqrt(self.real**2 + self.imag**2)