Some PEP8 rectifications
This commit is contained in:
parent
e9046c49ff
commit
e4efa1028e
@ -7,7 +7,7 @@
|
|||||||
# Distributed under terms of the MIT license.
|
# Distributed under terms of the MIT license.
|
||||||
|
|
||||||
from mapytex.calculus.core.tree import Tree
|
from mapytex.calculus.core.tree import Tree
|
||||||
from .mo import MO, MOError
|
from .mo import MO
|
||||||
|
|
||||||
__all__ = ["MOFraction"]
|
__all__ = ["MOFraction"]
|
||||||
|
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
#
|
#
|
||||||
# Distributed under terms of the MIT license.
|
# Distributed under terms of the MIT license.
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
from ..coroutine import coroutine, STOOOP
|
from ..coroutine import coroutine, STOOOP
|
||||||
from ..renders import tree2txt, tree2tex
|
from ..renders import tree2txt, tree2tex
|
||||||
from decimal import Decimal
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["moify", "MO", "MOstr"]
|
__all__ = ["moify", "MO", "MOstr"]
|
||||||
@ -59,7 +59,6 @@ class MO(object):
|
|||||||
>>> a = MO(a)
|
>>> a = MO(a)
|
||||||
>>> a
|
>>> a
|
||||||
<MO 3>
|
<MO 3>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.value = value.value
|
self.value = value.value
|
||||||
@ -107,7 +106,7 @@ class MOnumber(MO):
|
|||||||
|
|
||||||
""" Base number math object (int or Decimal) """
|
""" Base number math object (int or Decimal) """
|
||||||
|
|
||||||
def __init__(self, value, negative=False):
|
def __init__(self, value):
|
||||||
""" Initiate a number MO
|
""" Initiate a number MO
|
||||||
|
|
||||||
>>> MOnumber(23)
|
>>> MOnumber(23)
|
||||||
@ -134,7 +133,7 @@ class MOnumber(MO):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
val = value
|
val = value
|
||||||
|
|
||||||
if isinstance(val, int) or isinstance(val, Decimal):
|
if isinstance(val, (int, Decimal)):
|
||||||
MO.__init__(self, value)
|
MO.__init__(self, value)
|
||||||
elif isinstance(val, float):
|
elif isinstance(val, float):
|
||||||
MO.__init__(self, Decimal(val))
|
MO.__init__(self, Decimal(val))
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
Computing with MO
|
Computing with MO
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ..operator import OPERATORS
|
|
||||||
from .exceptions import ComputeError
|
from .exceptions import ComputeError
|
||||||
from .add import add
|
from .add import add
|
||||||
from .minus import minus
|
from .minus import minus
|
||||||
|
@ -11,8 +11,7 @@ Adding MO
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from ..tree import Tree
|
from ..tree import Tree
|
||||||
from ..operator import OPERATORS
|
from ..MO.mo import MO, MOnumber
|
||||||
from ..MO.mo import MO, MOnumber, MOstr
|
|
||||||
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
|
||||||
|
@ -22,6 +22,9 @@ class MinusError(ComputeError):
|
|||||||
class MultiplyError(ComputeError):
|
class MultiplyError(ComputeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class DivideError(ComputeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
@ -10,12 +10,9 @@
|
|||||||
Minus MO: take the opposit
|
Minus MO: take the opposit
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ..tree import Tree
|
from ..MO.mo import MO, MOnumber
|
||||||
from ..operator import OPERATORS
|
|
||||||
from ..MO.mo import MO, MOnumber, MOstr
|
|
||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
from .exceptions import MinusError
|
from .exceptions import MinusError
|
||||||
from .arithmetic import lcm
|
|
||||||
|
|
||||||
|
|
||||||
def minus(left, right):
|
def minus(left, right):
|
||||||
|
@ -11,8 +11,7 @@ Multiply MO
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from ..tree import Tree
|
from ..tree import Tree
|
||||||
from ..operator import OPERATORS
|
from ..MO.mo import MO, MOnumber
|
||||||
from ..MO.mo import MO, MOnumber, MOstr
|
|
||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
from .exceptions import MultiplyError
|
from .exceptions import MultiplyError
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ __all__ = ["coroutine", "STOOOP", "RESTAAART"]
|
|||||||
|
|
||||||
def coroutine(func):
|
def coroutine(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def start(*args,**kwargs):
|
def start(*args, **kwargs):
|
||||||
cr = func(*args,**kwargs)
|
cr = func(*args, **kwargs)
|
||||||
next(cr)
|
next(cr)
|
||||||
return cr
|
return cr
|
||||||
return start
|
return start
|
||||||
|
@ -126,7 +126,6 @@ def mul2tex(left, right):
|
|||||||
|
|
||||||
if display_time:
|
if display_time:
|
||||||
return f"{left_} \\times {right_}"
|
return f"{left_} \\times {right_}"
|
||||||
else:
|
|
||||||
return f"{left_}{right_}"
|
return f"{left_}{right_}"
|
||||||
|
|
||||||
def div2tex(left, right):
|
def div2tex(left, right):
|
||||||
@ -199,7 +198,7 @@ def pow2tex(left, right):
|
|||||||
|
|
||||||
return f"{left_} ^ {right_}"
|
return f"{left_} ^ {right_}"
|
||||||
|
|
||||||
OPERATOR2tex = {
|
OPERATOR2TEX = {
|
||||||
"+": plus2tex,
|
"+": plus2tex,
|
||||||
"-": minus2tex,
|
"-": minus2tex,
|
||||||
"*": mul2tex,
|
"*": mul2tex,
|
||||||
@ -217,7 +216,7 @@ def tree2tex(tree):
|
|||||||
>>> tree2tex(t)
|
>>> tree2tex(t)
|
||||||
'2 + 3 \\times 4'
|
'2 + 3 \\times 4'
|
||||||
"""
|
"""
|
||||||
return OPERATOR2tex[tree.node](tree.left_value, tree.right_value)
|
return OPERATOR2TEX[tree.node](tree.left_value, tree.right_value)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
@ -293,8 +293,6 @@ def concurent_broadcast(target, lookfors = []):
|
|||||||
|
|
||||||
lookfors_ = [remember_lookfor(lkf) for lkf in lookfors]
|
lookfors_ = [remember_lookfor(lkf) for lkf in lookfors]
|
||||||
|
|
||||||
stock = []
|
|
||||||
ans = []
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
found = False
|
found = False
|
||||||
@ -310,7 +308,7 @@ def concurent_broadcast(target, lookfors = []):
|
|||||||
for lf in lookfors_:
|
for lf in lookfors_:
|
||||||
lf.throw(RESTAAART)
|
lf.throw(RESTAAART)
|
||||||
for i in found:
|
for i in found:
|
||||||
ans = target_.send(i)
|
target_.send(i)
|
||||||
except STOOOP as err:
|
except STOOOP as err:
|
||||||
for lf in lookfors_:
|
for lf in lookfors_:
|
||||||
last = lf.throw(RESTAAART)
|
last = lf.throw(RESTAAART)
|
||||||
@ -380,14 +378,14 @@ def missing_times(target):
|
|||||||
if not previous is None:
|
if not previous is None:
|
||||||
previous = None
|
previous = None
|
||||||
|
|
||||||
if type(tok) == str:
|
if isinstance(tok, str):
|
||||||
if tok == '(':
|
if tok == '(':
|
||||||
target_.send("*")
|
target_.send("*")
|
||||||
elif not is_operator(tok) and tok != ')':
|
elif not is_operator(tok) and tok != ')':
|
||||||
target_.send("*")
|
target_.send("*")
|
||||||
|
|
||||||
if type(tok) == int or \
|
if isinstance(tok, int) or \
|
||||||
(type(tok) == str and \
|
(isinstance(tok, str) and \
|
||||||
not is_operator(tok) and \
|
not is_operator(tok) and \
|
||||||
not tok == '('):
|
not tok == '('):
|
||||||
previous = tok
|
previous = tok
|
||||||
@ -462,7 +460,6 @@ def lookforNumbers(target):
|
|||||||
target_ = target
|
target_ = target
|
||||||
|
|
||||||
current = ""
|
current = ""
|
||||||
ans = []
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
tok = yield
|
tok = yield
|
||||||
@ -473,12 +470,12 @@ def lookforNumbers(target):
|
|||||||
if tok == '.':
|
if tok == '.':
|
||||||
if "." in current:
|
if "." in current:
|
||||||
raise ParsingError(f"Can't build a number with 2 dots (current is {current})")
|
raise ParsingError(f"Can't build a number with 2 dots (current is {current})")
|
||||||
elif len(current) == 0:
|
elif not current:
|
||||||
raise ParsingError(f"Can't build a number starting with a dot")
|
raise ParsingError(f"Can't build a number starting with a dot")
|
||||||
else:
|
else:
|
||||||
current += tok
|
current += tok
|
||||||
elif tok == '-':
|
elif tok == '-':
|
||||||
if len(current) > 0:
|
if current:
|
||||||
target_.send(typifiy_numbers(current))
|
target_.send(typifiy_numbers(current))
|
||||||
target_.send('+')
|
target_.send('+')
|
||||||
current = tok
|
current = tok
|
||||||
@ -564,7 +561,7 @@ def list_sink():
|
|||||||
except STOOOP:
|
except STOOOP:
|
||||||
yield ans
|
yield ans
|
||||||
|
|
||||||
def str2(sink, convert_to_MO=True):
|
def str2(sink, convert_to_mo=True):
|
||||||
""" Return a pipeline which parse an expression with the sink as an endpont
|
""" Return a pipeline which parse an expression with the sink as an endpont
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
@ -685,9 +682,9 @@ def str2(sink, convert_to_MO=True):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
lfop = lookfor(is_operator)
|
lfop = lookfor(is_operator)
|
||||||
operator_corout = partial(concurent_broadcast, lookfors = [lfop])
|
operator_corout = partial(concurent_broadcast, lookfors=[lfop])
|
||||||
def pipeline(expression):
|
def pipeline(expression):
|
||||||
if convert_to_MO:
|
if convert_to_mo:
|
||||||
str2_corout = lookforNumbers(operator_corout(missing_times(moify(pparser(sink)))))
|
str2_corout = lookforNumbers(operator_corout(missing_times(moify(pparser(sink)))))
|
||||||
else:
|
else:
|
||||||
str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink))))
|
str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink))))
|
||||||
|
@ -10,11 +10,10 @@ Tree class
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from .tree_tools import (to_nested_parenthesis,
|
from .tree_tools import (to_nested_parenthesis,
|
||||||
infix_str_concatenate,
|
|
||||||
postfix_concatenate,
|
postfix_concatenate,
|
||||||
show_tree,
|
show_tree,
|
||||||
)
|
)
|
||||||
from .coroutine import *
|
from .coroutine import coroutine, STOOOP
|
||||||
from .str2 import str2
|
from .str2 import str2
|
||||||
from .operator import OPERATORS
|
from .operator import OPERATORS
|
||||||
|
|
||||||
@ -55,7 +54,7 @@ class Tree(object):
|
|||||||
self.right_value = right_value
|
self.right_value = right_value
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_str(cls, expression, convert_to_MO=True):
|
def from_str(cls, expression, convert_to_mo=True):
|
||||||
""" Initiate a tree from an string expression
|
""" Initiate a tree from an string expression
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
@ -83,7 +82,7 @@ class Tree(object):
|
|||||||
| > n
|
| > n
|
||||||
|
|
||||||
"""
|
"""
|
||||||
t = MutableTree.from_str(expression, convert_to_MO)
|
t = MutableTree.from_str(expression, convert_to_mo)
|
||||||
return cls.from_any_tree(t)
|
return cls.from_any_tree(t)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -268,7 +267,7 @@ class Tree(object):
|
|||||||
|
|
||||||
:example:
|
:example:
|
||||||
|
|
||||||
>>> t = Tree.from_str("3*4+2", convert_to_MO=False)
|
>>> t = Tree.from_str("3*4+2", convert_to_mo=False)
|
||||||
>>> print(t)
|
>>> print(t)
|
||||||
+
|
+
|
||||||
> *
|
> *
|
||||||
@ -385,7 +384,7 @@ class Tree(object):
|
|||||||
|
|
||||||
return function(self.node, left_value, right_value)
|
return function(self.node, left_value, right_value)
|
||||||
|
|
||||||
def get_leafs(self, callback = lambda x:x):
|
def get_leafs(self, callback=lambda x:x):
|
||||||
""" Generator which yield all the leaf value of the tree.
|
""" Generator which yield all the leaf value of the tree.
|
||||||
Callback act on every leaf.
|
Callback act on every leaf.
|
||||||
|
|
||||||
@ -408,7 +407,7 @@ class Tree(object):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
yield callback(self.right_value)
|
yield callback(self.right_value)
|
||||||
|
|
||||||
def get_nodes(self, callback = lambda x:x):
|
def get_nodes(self, callback=lambda x:x):
|
||||||
""" Generator which yield all nodes of the tree.
|
""" Generator which yield all nodes of the tree.
|
||||||
Callback act on every nodes.
|
Callback act on every nodes.
|
||||||
|
|
||||||
@ -580,7 +579,7 @@ class MutableTree(Tree):
|
|||||||
self.right_value = right_value
|
self.right_value = right_value
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_str(cls, expression, convert_to_MO=True):
|
def from_str(cls, expression, convert_to_mo=True):
|
||||||
""" Initiate the MutableTree
|
""" Initiate the MutableTree
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
@ -606,8 +605,8 @@ class MutableTree(Tree):
|
|||||||
| > -2
|
| > -2
|
||||||
| > 3
|
| > 3
|
||||||
"""
|
"""
|
||||||
str2mutTree = str2(cls.sink, convert_to_MO)
|
str_2_mut_tree = str2(cls.sink, convert_to_mo)
|
||||||
return str2mutTree(expression)
|
return str_2_mut_tree(expression)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@coroutine
|
@coroutine
|
||||||
@ -867,7 +866,7 @@ class AssocialTree(Tree):
|
|||||||
|
|
||||||
:example:
|
:example:
|
||||||
|
|
||||||
>>> t = Tree.from_str("3*4+2", convert_to_MO=False)
|
>>> t = Tree.from_str("3*4+2", convert_to_mo=False)
|
||||||
>>> print(t)
|
>>> print(t)
|
||||||
+
|
+
|
||||||
> *
|
> *
|
||||||
@ -938,7 +937,7 @@ class AssocialTree(Tree):
|
|||||||
|
|
||||||
return function(self.node, left_value, right_value)
|
return function(self.node, left_value, right_value)
|
||||||
|
|
||||||
def get_leafs(self, callback = lambda x:x):
|
def get_leafs(self, callback=lambda x: x):
|
||||||
""" Generator which yield all the leaf value of the tree.
|
""" Generator which yield all the leaf value of the tree.
|
||||||
Callback act on every leaf.
|
Callback act on every leaf.
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ def postfix_concatenate(op, left, right):
|
|||||||
|
|
||||||
return left_tokens + right_tokens + [op]
|
return left_tokens + right_tokens + [op]
|
||||||
|
|
||||||
def show_tree(op, left, right, sep = "|", node_caracter = ">"):
|
def show_tree(op, left, right, sep="|", node_caracter=">"):
|
||||||
""" Shape argument to make nice Tree display
|
""" Shape argument to make nice Tree display
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
|
Loading…
Reference in New Issue
Block a user