Some PEP8 rectifications

This commit is contained in:
Bertrand Benjamin 2018-03-13 14:43:48 +03:00
parent e9046c49ff
commit e4efa1028e
14 changed files with 100 additions and 109 deletions

View File

@ -7,7 +7,7 @@
# Distributed under terms of the MIT license.
from mapytex.calculus.core.tree import Tree
from .mo import MO, MOError
from .mo import MO
__all__ = ["MOFraction"]
@ -33,9 +33,9 @@ class MOFraction(MO):
<MOFraction - 2 / 3>
"""
base_value = Tree("/",
MO.factory(numerator),
MO.factory(denominator),
)
MO.factory(numerator),
MO.factory(denominator),
)
if negative:
value = Tree("-", None, base_value)
else:

View File

@ -6,9 +6,9 @@
#
# Distributed under terms of the MIT license.
from decimal import Decimal
from ..coroutine import coroutine, STOOOP
from ..renders import tree2txt, tree2tex
from decimal import Decimal
__all__ = ["moify", "MO", "MOstr"]
@ -59,7 +59,6 @@ class MO(object):
>>> a = MO(a)
>>> a
<MO 3>
"""
try:
self.value = value.value
@ -107,7 +106,7 @@ class MOnumber(MO):
""" Base number math object (int or Decimal) """
def __init__(self, value, negative=False):
def __init__(self, value):
""" Initiate a number MO
>>> MOnumber(23)
@ -134,7 +133,7 @@ class MOnumber(MO):
except AttributeError:
val = value
if isinstance(val, int) or isinstance(val, Decimal):
if isinstance(val, (int, Decimal)):
MO.__init__(self, value)
elif isinstance(val, float):
MO.__init__(self, Decimal(val))

View File

@ -10,7 +10,6 @@
Computing with MO
"""
from ..operator import OPERATORS
from .exceptions import ComputeError
from .add import add
from .minus import minus

View File

@ -11,8 +11,7 @@ Adding MO
"""
from ..tree import Tree
from ..operator import OPERATORS
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.mo import MO, MOnumber
from ..MO.fraction import MOFraction
from .exceptions import AddError
from .arithmetic import lcm
@ -206,11 +205,11 @@ def mofraction_mofraction(left, right):
# 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,
(MOFraction, MOFraction): mofraction_mofraction,
}
(MOnumber, MOnumber): monumber_monumber,
(MOnumber, MOFraction): monumber_mofraction,
(MOFraction, MOnumber): mofraction_monumber,
(MOFraction, MOFraction): mofraction_mofraction,
}
# -----------------------------
# Reglages pour 'vim'

View File

@ -22,6 +22,9 @@ class MinusError(ComputeError):
class MultiplyError(ComputeError):
pass
class DivideError(ComputeError):
pass
# -----------------------------
# Reglages pour 'vim'

View File

@ -10,12 +10,9 @@
Minus MO: take the opposit
"""
from ..tree import Tree
from ..operator import OPERATORS
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.mo import MO, MOnumber
from ..MO.fraction import MOFraction
from .exceptions import MinusError
from .arithmetic import lcm
def minus(left, right):
@ -92,9 +89,9 @@ def mofraction(right):
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
MINUSFUNCTIONS = {
MOnumber: monumber,
MOFraction: mofraction,
}
MOnumber: monumber,
MOFraction: mofraction,
}
# -----------------------------
# Reglages pour 'vim'

View File

@ -11,8 +11,7 @@ Multiply MO
"""
from ..tree import Tree
from ..operator import OPERATORS
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.mo import MO, MOnumber
from ..MO.fraction import MOFraction
from .exceptions import MultiplyError
@ -116,10 +115,10 @@ def mofraction_monumber(left, right):
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
MULFUNCTIONS = {
(MOnumber, MOnumber): monumber_monumber,
(MOnumber, MOFraction): monumber_mofraction,
(MOFraction, MOnumber): mofraction_monumber,
}
(MOnumber, MOnumber): monumber_monumber,
(MOnumber, MOFraction): monumber_mofraction,
(MOFraction, MOnumber): mofraction_monumber,
}
# -----------------------------
# Reglages pour 'vim'

View File

@ -16,8 +16,8 @@ __all__ = ["coroutine", "STOOOP", "RESTAAART"]
def coroutine(func):
@wraps(func)
def start(*args,**kwargs):
cr = func(*args,**kwargs)
def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr
return start

View File

@ -12,26 +12,26 @@ class OperatorError(Exception):
pass
OPERATORS = {
"+": {'repr': "+",
'arity': 2,
'precedence': 0,
},
"-": {'repr': "-",
'arity': 1,
'precedence': 1,
},
"*": {'repr': "*",
'arity': 2,
'precedence': 2,
},
"/": {'repr': "/",
'arity': 2,
'precedence': 3,
},
"^": {'repr': "^",
'arity': 2,
'precedence': 4,
},
"+": {'repr': "+",
'arity': 2,
'precedence': 0,
},
"-": {'repr': "-",
'arity': 1,
'precedence': 1,
},
"*": {'repr': "*",
'arity': 2,
'precedence': 2,
},
"/": {'repr': "/",
'arity': 2,
'precedence': 3,
},
"^": {'repr': "^",
'arity': 2,
'precedence': 4,
},
}
def is_operator(string):

View File

@ -126,8 +126,7 @@ def mul2tex(left, right):
if display_time:
return f"{left_} \\times {right_}"
else:
return f"{left_}{right_}"
return f"{left_}{right_}"
def div2tex(left, right):
r""" / rendering
@ -199,12 +198,12 @@ def pow2tex(left, right):
return f"{left_} ^ {right_}"
OPERATOR2tex = {
"+": plus2tex,
"-": minus2tex,
"*": mul2tex,
"/": div2tex,
"^": pow2tex,
OPERATOR2TEX = {
"+": plus2tex,
"-": minus2tex,
"*": mul2tex,
"/": div2tex,
"^": pow2tex,
}
def tree2tex(tree):
@ -217,7 +216,7 @@ def tree2tex(tree):
>>> tree2tex(t)
'2 + 3 \\times 4'
"""
return OPERATOR2tex[tree.node](tree.left_value, tree.right_value)
return OPERATOR2TEX[tree.node](tree.left_value, tree.right_value)
# -----------------------------

View File

@ -214,11 +214,11 @@ def pow2txt(left, right):
return f"{left_} ^ {right_}"
OPERATOR2TXT = {
"+": plus2txt,
"-": minus2txt,
"*": mul2txt,
"/": div2txt,
"^": pow2txt,
"+": plus2txt,
"-": minus2txt,
"*": mul2txt,
"/": div2txt,
"^": pow2txt,
}
def tree2txt(tree):
@ -231,7 +231,7 @@ def tree2txt(tree):
>>> tree2txt(t)
'2 + 3 * 4'
"""
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
# -----------------------------

View File

@ -293,8 +293,6 @@ def concurent_broadcast(target, lookfors = []):
lookfors_ = [remember_lookfor(lkf) for lkf in lookfors]
stock = []
ans = []
try:
while True:
found = False
@ -310,7 +308,7 @@ def concurent_broadcast(target, lookfors = []):
for lf in lookfors_:
lf.throw(RESTAAART)
for i in found:
ans = target_.send(i)
target_.send(i)
except STOOOP as err:
for lf in lookfors_:
last = lf.throw(RESTAAART)
@ -380,14 +378,14 @@ def missing_times(target):
if not previous is None:
previous = None
if type(tok) == str:
if isinstance(tok, str):
if tok == '(':
target_.send("*")
elif not is_operator(tok) and tok != ')':
target_.send("*")
if type(tok) == int or \
(type(tok) == str and \
if isinstance(tok, int) or \
(isinstance(tok, str) and \
not is_operator(tok) and \
not tok == '('):
previous = tok
@ -462,7 +460,6 @@ def lookforNumbers(target):
target_ = target
current = ""
ans = []
try:
while True:
tok = yield
@ -473,12 +470,12 @@ def lookforNumbers(target):
if tok == '.':
if "." in 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")
else:
current += tok
elif tok == '-':
if len(current) > 0:
if current:
target_.send(typifiy_numbers(current))
target_.send('+')
current = tok
@ -494,9 +491,9 @@ def lookforNumbers(target):
current += tok
except STOOOP as err:
if current:
target_.send(typifiy_numbers(current))
yield target_.throw(err)
if current:
target_.send(typifiy_numbers(current))
yield target_.throw(err)
def typifiy_numbers(number):
""" Transform a str number into a integer or a decimal """
@ -564,7 +561,7 @@ def list_sink():
except STOOOP:
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
:example:
@ -685,9 +682,9 @@ def str2(sink, convert_to_MO=True):
"""
lfop = lookfor(is_operator)
operator_corout = partial(concurent_broadcast, lookfors = [lfop])
operator_corout = partial(concurent_broadcast, lookfors=[lfop])
def pipeline(expression):
if convert_to_MO:
if convert_to_mo:
str2_corout = lookforNumbers(operator_corout(missing_times(moify(pparser(sink)))))
else:
str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink))))

View File

@ -10,11 +10,10 @@ Tree class
"""
from .tree_tools import (to_nested_parenthesis,
infix_str_concatenate,
postfix_concatenate,
show_tree,
)
from .coroutine import *
postfix_concatenate,
show_tree,
)
from .coroutine import coroutine, STOOOP
from .str2 import str2
from .operator import OPERATORS
@ -55,7 +54,7 @@ class Tree(object):
self.right_value = right_value
@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
:example:
@ -83,7 +82,7 @@ class Tree(object):
| > n
"""
t = MutableTree.from_str(expression, convert_to_MO)
t = MutableTree.from_str(expression, convert_to_mo)
return cls.from_any_tree(t)
@classmethod
@ -268,7 +267,7 @@ class Tree(object):
: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)
+
> *
@ -385,7 +384,7 @@ class Tree(object):
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.
Callback act on every leaf.
@ -408,7 +407,7 @@ class Tree(object):
except AttributeError:
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.
Callback act on every nodes.
@ -580,7 +579,7 @@ class MutableTree(Tree):
self.right_value = right_value
@classmethod
def from_str(cls, expression, convert_to_MO=True):
def from_str(cls, expression, convert_to_mo=True):
""" Initiate the MutableTree
:example:
@ -606,8 +605,8 @@ class MutableTree(Tree):
| > -2
| > 3
"""
str2mutTree = str2(cls.sink, convert_to_MO)
return str2mutTree(expression)
str_2_mut_tree = str2(cls.sink, convert_to_mo)
return str_2_mut_tree(expression)
@classmethod
@coroutine
@ -867,7 +866,7 @@ class AssocialTree(Tree):
: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)
+
> *
@ -938,7 +937,7 @@ class AssocialTree(Tree):
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.
Callback act on every leaf.

View File

@ -61,7 +61,7 @@ def postfix_concatenate(op, left, right):
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
:example: