Throw bases for computing (and start tiding exceptions)
This commit is contained in:
49
mapytex/calculus/core/compute/__init__.py
Normal file
49
mapytex/calculus/core/compute/__init__.py
Normal file
@@ -0,0 +1,49 @@
|
||||
#! /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
|
||||
"""
|
||||
|
||||
from ..operator import OPERATORS
|
||||
from .exceptions import ComputeError
|
||||
|
||||
def compute(node, left_v, right_v):
|
||||
"""
|
||||
Computing a node
|
||||
|
||||
:example:
|
||||
|
||||
>>> compute("+", 1, 2)
|
||||
3
|
||||
>>> compute("-", None, 2)
|
||||
-2
|
||||
>>> compute("-", 1, 2)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
mapytex.calculus.core.compute.exceptions.ComputeError: left_v need to be None for operator with arity 1
|
||||
"""
|
||||
op = OPERATORS[node]
|
||||
lv = left_v
|
||||
rv = right_v
|
||||
if op['arity'] == 1:
|
||||
if lv is None:
|
||||
return op["operate"](rv)()
|
||||
else:
|
||||
raise ComputeError("left_v need to be None for operator with arity 1")
|
||||
else:
|
||||
try:
|
||||
return op["operate"](lv)(rv)
|
||||
except NotImplemented:
|
||||
return op["roperate"](rv)(lv)
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
119
mapytex/calculus/core/compute/add.py
Normal file
119
mapytex/calculus/core/compute/add.py
Normal file
@@ -0,0 +1,119 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 lafrite <lafrite@Poivre>
|
||||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
|
||||
"""
|
||||
Adding MO
|
||||
"""
|
||||
|
||||
from ..tree import Tree
|
||||
from ..operator import OPERATORS
|
||||
from ..MO.mo import MO, MOnumber, MOstr
|
||||
from ..MO.fraction import MOFraction
|
||||
from .exceptions import AddError
|
||||
|
||||
|
||||
def add(left, right):
|
||||
""" Perform the addition of left and right
|
||||
|
||||
:param left: left MO
|
||||
:param right: right MO
|
||||
:returns: Tree or MO
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> b = MOnumber(6)
|
||||
>>> add(a, b)
|
||||
<MOnumber 10>
|
||||
>>> b = MOnumber(0)
|
||||
>>> add(a, b)
|
||||
<MOnumber 4>
|
||||
|
||||
"""
|
||||
if left.value == 0:
|
||||
return right
|
||||
elif right.value == 0:
|
||||
return left
|
||||
|
||||
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
||||
|
||||
def monumber_monumber(left, right):
|
||||
""" Adding 2 monumbers
|
||||
|
||||
:param left: left MOnumber
|
||||
:param right: right MOnumber
|
||||
:returns: MONumber
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> b = MOnumber(6)
|
||||
>>> monumber_monumber(a, b)
|
||||
<MOnumber 10>
|
||||
|
||||
"""
|
||||
return MO.factory(left.value + right.value)
|
||||
|
||||
def monumber_mofraction(left, right):
|
||||
""" Adding a monumber and a mofraction
|
||||
|
||||
:param left: a monumber
|
||||
:param right: a mofraction
|
||||
:returns: Tree with the number converted into a mofraction
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> b = MOFraction(6, 5)
|
||||
>>> print(monumber_mofraction(a, b))
|
||||
+
|
||||
> /
|
||||
| > 4
|
||||
| > 1
|
||||
> /
|
||||
| > 6
|
||||
| > 5
|
||||
|
||||
"""
|
||||
if not isinstance(left, MOnumber) or not isinstance(right, MOFraction):
|
||||
raise AddError(f"Wrong type for left (got {left.__class__.__name__}) \
|
||||
or right (got {right.__class__.__name__})")
|
||||
left_fraction = MOFraction(left, MOnumber(1))
|
||||
return Tree("+", left_fraction, right)
|
||||
|
||||
def mofraction_monumber(left, right):
|
||||
""" Adding a monumber and a mofraction
|
||||
|
||||
:param left: a mofraction
|
||||
:param right: a monumber
|
||||
:returns: Tree with the number converted into a mofraction
|
||||
|
||||
>>> a = MOFraction(6, 5)
|
||||
>>> b = MOnumber(4)
|
||||
>>> print(mofraction_monumber(a, b))
|
||||
+
|
||||
> /
|
||||
| > 6
|
||||
| > 5
|
||||
> /
|
||||
| > 4
|
||||
| > 1
|
||||
"""
|
||||
|
||||
if not isinstance(left, MOFraction) or not isinstance(right, MOnumber):
|
||||
raise AddError(f"Wrong type for left (got {left.__class__.__name__})"
|
||||
f"or right (got {right.__class__.__name__})")
|
||||
|
||||
right_fraction = MOFraction(right, MOnumber(1))
|
||||
return Tree("+", left, right_fraction)
|
||||
|
||||
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
|
||||
ADDFUNCTIONS = {
|
||||
(MOnumber, MOnumber): monumber_monumber,
|
||||
(MOnumber, MOFraction): monumber_mofraction,
|
||||
(MOFraction, MOnumber): mofraction_monumber,
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
23
mapytex/calculus/core/compute/exceptions.py
Normal file
23
mapytex/calculus/core/compute/exceptions.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 lafrite <lafrite@Poivre>
|
||||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
|
||||
"""
|
||||
Exceptions for computing
|
||||
"""
|
||||
|
||||
class ComputeError(Exception):
|
||||
pass
|
||||
|
||||
class AddError(ComputeError):
|
||||
pass
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
Reference in New Issue
Block a user