Mapytex/mapytex/calculus/core/compute/__init__.py

55 lines
1.2 KiB
Python
Raw Normal View History

2018-01-21 15:16:08 +00:00
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Computing with MO
2018-01-21 15:16:08 +00:00
"""
from ..operator import OPERATORS
from .exceptions import ComputeError
from .add import add
from .minus import minus
from .multiply import multiply
OPERATIONS = {
"+": add,
"-": minus,
"*": multiply,
}
2018-03-07 13:26:06 +00:00
2018-01-21 15:16:08 +00:00
def compute(node, left_v, right_v):
"""
Computing a node
:example:
>>> from ..MO.mo import MOnumber
>>> compute("+", MOnumber(1), MOnumber(2))
<MOnumber 3>
>>> compute("-", None, MOnumber(2))
<MOnumber - 2>
>>> compute("*", MOnumber(1), MOnumber(2))
<MOnumber 2>
>>> compute("~", MOnumber(1), MOnumber(2))
2018-03-07 13:26:06 +00:00
Traceback (most recent call last):
...
mapytex.calculus.core.compute.exceptions.ComputeError: Unknown operation (~)
2018-01-21 15:16:08 +00:00
"""
try:
operation = OPERATIONS[node]
except KeyError:
raise ComputeError(f"Unknown operation ({node})")
return operation(left_v, right_v)
2018-01-21 15:16:08 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del