type filter for operations
This commit is contained in:
parent
6e2cc96781
commit
985032b1af
@ -15,6 +15,7 @@ from ..MO.mo import MO, MOnumber
|
|||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
from .exceptions import AddError
|
from .exceptions import AddError
|
||||||
from .arithmetic import lcm
|
from .arithmetic import lcm
|
||||||
|
from .type_filter import args_are
|
||||||
|
|
||||||
|
|
||||||
def add(left, right):
|
def add(left, right):
|
||||||
@ -40,6 +41,7 @@ def add(left, right):
|
|||||||
|
|
||||||
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
||||||
|
|
||||||
|
@args_are(MOnumber, MOnumber)
|
||||||
def monumber_monumber(left, right):
|
def monumber_monumber(left, right):
|
||||||
""" Adding 2 monumbers
|
""" Adding 2 monumbers
|
||||||
|
|
||||||
@ -55,6 +57,7 @@ def monumber_monumber(left, right):
|
|||||||
"""
|
"""
|
||||||
return MO.factory(left.value + right.value)
|
return MO.factory(left.value + right.value)
|
||||||
|
|
||||||
|
@args_are(MOnumber, MOFraction)
|
||||||
def monumber_mofraction(left, right):
|
def monumber_mofraction(left, right):
|
||||||
""" Adding a monumber and a mofraction
|
""" Adding a monumber and a mofraction
|
||||||
|
|
||||||
@ -72,14 +75,11 @@ def monumber_mofraction(left, right):
|
|||||||
> /
|
> /
|
||||||
| > 6
|
| > 6
|
||||||
| > 5
|
| > 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))
|
left_fraction = MOFraction(left, MOnumber(1))
|
||||||
return Tree("+", left_fraction, right)
|
return Tree("+", left_fraction, right)
|
||||||
|
|
||||||
|
@args_are(MOFraction, MOnumber)
|
||||||
def mofraction_monumber(left, right):
|
def mofraction_monumber(left, right):
|
||||||
""" Adding a monumber and a mofraction
|
""" Adding a monumber and a mofraction
|
||||||
|
|
||||||
@ -98,14 +98,10 @@ def mofraction_monumber(left, right):
|
|||||||
| > 4
|
| > 4
|
||||||
| > 1
|
| > 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))
|
right_fraction = MOFraction(right, MOnumber(1))
|
||||||
return Tree("+", left, right_fraction)
|
return Tree("+", left, right_fraction)
|
||||||
|
|
||||||
|
@args_are(MOFraction, MOFraction)
|
||||||
def mofraction_mofraction(left, right):
|
def mofraction_mofraction(left, right):
|
||||||
""" Adding two mofractions
|
""" Adding two mofractions
|
||||||
|
|
||||||
@ -174,11 +170,6 @@ def mofraction_mofraction(left, right):
|
|||||||
| | > 5
|
| | > 5
|
||||||
| | > 2
|
| | > 2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not isinstance(left, MOFraction) or not isinstance(right, MOFraction):
|
|
||||||
raise AddError(f"Wrong type for left (got {left.__class__.__name__})"
|
|
||||||
f"or right (got {right.__class__.__name__})")
|
|
||||||
|
|
||||||
if left.denominator == right.denominator:
|
if left.denominator == right.denominator:
|
||||||
num = Tree("+", left.numerator, right.numerator)
|
num = Tree("+", left.numerator, right.numerator)
|
||||||
return Tree("/", num, left.denominator)
|
return Tree("/", num, left.denominator)
|
||||||
|
@ -14,6 +14,7 @@ from ..tree import Tree
|
|||||||
from ..MO.mo import MO, MOnumber
|
from ..MO.mo import MO, MOnumber
|
||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
from .exceptions import DivideError
|
from .exceptions import DivideError
|
||||||
|
from .type_filter import args_are
|
||||||
|
|
||||||
|
|
||||||
def divide(left, right):
|
def divide(left, right):
|
||||||
@ -47,6 +48,7 @@ def divide(left, right):
|
|||||||
|
|
||||||
return MULFUNCTIONS[(type(left), type(right))](left, right)
|
return MULFUNCTIONS[(type(left), type(right))](left, right)
|
||||||
|
|
||||||
|
@args_are(MOnumber, MOnumber)
|
||||||
def monumber_monumber(left, right):
|
def monumber_monumber(left, right):
|
||||||
""" Divide 2 monumbers
|
""" Divide 2 monumbers
|
||||||
|
|
||||||
@ -61,6 +63,7 @@ def monumber_monumber(left, right):
|
|||||||
"""
|
"""
|
||||||
return MOFraction(left, right)
|
return MOFraction(left, right)
|
||||||
|
|
||||||
|
@args_are(MOnumber, MOFraction)
|
||||||
def monumber_mofraction(left, right):
|
def monumber_mofraction(left, right):
|
||||||
""" Divide a monumber and a mofraction
|
""" Divide a monumber and a mofraction
|
||||||
|
|
||||||
@ -92,6 +95,7 @@ def monumber_mofraction(left, right):
|
|||||||
|
|
||||||
return Tree("*", left, right.inverse())
|
return Tree("*", left, right.inverse())
|
||||||
|
|
||||||
|
@args_are(MOFraction, MOnumber)
|
||||||
def mofraction_monumber(left, right):
|
def mofraction_monumber(left, right):
|
||||||
""" Divide a monumber and a mofraction
|
""" Divide a monumber and a mofraction
|
||||||
|
|
||||||
@ -118,6 +122,7 @@ def mofraction_monumber(left, right):
|
|||||||
right_fraction = MOFraction(1, right)
|
right_fraction = MOFraction(1, right)
|
||||||
return Tree("*", left, right_fraction)
|
return Tree("*", left, right_fraction)
|
||||||
|
|
||||||
|
@args_are(MOFraction, MOFraction)
|
||||||
def mofraction_mofraction(left, right):
|
def mofraction_mofraction(left, right):
|
||||||
""" Divide two mofractions
|
""" Divide two mofractions
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ from ..tree import Tree
|
|||||||
from ..MO.mo import MO, MOnumber
|
from ..MO.mo import MO, MOnumber
|
||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
from .exceptions import MultiplyError
|
from .exceptions import MultiplyError
|
||||||
|
from .type_filter import args_are
|
||||||
|
|
||||||
|
|
||||||
def multiply(left, right):
|
def multiply(left, right):
|
||||||
@ -41,6 +42,7 @@ def multiply(left, right):
|
|||||||
|
|
||||||
return MULFUNCTIONS[(type(left), type(right))](left, right)
|
return MULFUNCTIONS[(type(left), type(right))](left, right)
|
||||||
|
|
||||||
|
@args_are(MOnumber, MOnumber)
|
||||||
def monumber_monumber(left, right):
|
def monumber_monumber(left, right):
|
||||||
""" Multiply 2 monumbers
|
""" Multiply 2 monumbers
|
||||||
|
|
||||||
@ -56,6 +58,7 @@ def monumber_monumber(left, right):
|
|||||||
"""
|
"""
|
||||||
return MO.factory(left.value * right.value)
|
return MO.factory(left.value * right.value)
|
||||||
|
|
||||||
|
@args_are(MOnumber, MOFraction)
|
||||||
def monumber_mofraction(left, right):
|
def monumber_mofraction(left, right):
|
||||||
""" Multiply a monumber and a mofraction
|
""" Multiply a monumber and a mofraction
|
||||||
|
|
||||||
@ -89,6 +92,7 @@ def monumber_mofraction(left, right):
|
|||||||
num = Tree("*", left, right.numerator)
|
num = Tree("*", left, right.numerator)
|
||||||
return Tree("/", num, right._denominator)
|
return Tree("/", num, right._denominator)
|
||||||
|
|
||||||
|
@args_are(MOFraction, MOnumber)
|
||||||
def mofraction_monumber(left, right):
|
def mofraction_monumber(left, right):
|
||||||
""" Multiply a monumber and a mofraction
|
""" Multiply a monumber and a mofraction
|
||||||
|
|
||||||
@ -113,6 +117,7 @@ def mofraction_monumber(left, right):
|
|||||||
num = Tree("*", left.numerator, right)
|
num = Tree("*", left.numerator, right)
|
||||||
return Tree("/", num, left._denominator)
|
return Tree("/", num, left._denominator)
|
||||||
|
|
||||||
|
@args_are(MOFraction, MOFraction)
|
||||||
def mofraction_mofraction(left, right):
|
def mofraction_mofraction(left, right):
|
||||||
""" Multiply two mofractions
|
""" Multiply two mofractions
|
||||||
|
|
||||||
|
44
mapytex/calculus/core/compute/type_filter.py
Normal file
44
mapytex/calculus/core/compute/type_filter.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vim:fenc=utf-8
|
||||||
|
#
|
||||||
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
||||||
|
#
|
||||||
|
# Distributed under terms of the MIT license.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Decorator to filter MO before operate
|
||||||
|
"""
|
||||||
|
|
||||||
|
from functools import wraps
|
||||||
|
from .exceptions import ComputeError
|
||||||
|
|
||||||
|
def args_are(left_type, right_type):
|
||||||
|
""" Decorator which filter arguments type
|
||||||
|
|
||||||
|
:param left_type: class or tuple of class to pass to isinstance for left arg
|
||||||
|
:param right_type: class or tuple of class to pass to isinstance for right arg
|
||||||
|
:returns: a decorator which will allow only some type
|
||||||
|
|
||||||
|
"""
|
||||||
|
def type_filter(func):
|
||||||
|
@wraps(func)
|
||||||
|
def filtered_func(left, right):
|
||||||
|
if not isinstance(left, left_type):
|
||||||
|
raise ComputeError("Wrong type for left argument"
|
||||||
|
f"Require {left_type}, got {left.__class__.__name__}"
|
||||||
|
)
|
||||||
|
if not isinstance(right, right_type):
|
||||||
|
raise ComputeError("Wrong type for right argument"
|
||||||
|
f"Require {right_type}, got {right.__class__.__name__}"
|
||||||
|
)
|
||||||
|
return func(left, right)
|
||||||
|
return filtered_func
|
||||||
|
return type_filter
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Reglages pour 'vim'
|
||||||
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||||
|
# cursor: 16 del
|
Loading…
Reference in New Issue
Block a user