From e4efa1028e789adf22fb326930ee7c31dff52290 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 13 Mar 2018 14:43:48 +0300 Subject: [PATCH] Some PEP8 rectifications --- mapytex/calculus/core/MO/fraction.py | 8 ++--- mapytex/calculus/core/MO/mo.py | 19 +++++----- mapytex/calculus/core/compute/__init__.py | 1 - mapytex/calculus/core/compute/add.py | 13 ++++--- mapytex/calculus/core/compute/exceptions.py | 3 ++ mapytex/calculus/core/compute/minus.py | 11 +++--- mapytex/calculus/core/compute/multiply.py | 11 +++--- mapytex/calculus/core/coroutine.py | 4 +-- mapytex/calculus/core/operator.py | 40 ++++++++++----------- mapytex/calculus/core/renders/tree2tex.py | 17 +++++---- mapytex/calculus/core/renders/tree2txt.py | 12 +++---- mapytex/calculus/core/str2.py | 35 +++++++++--------- mapytex/calculus/core/tree.py | 29 ++++++++------- mapytex/calculus/core/tree_tools.py | 6 ++-- 14 files changed, 100 insertions(+), 109 deletions(-) diff --git a/mapytex/calculus/core/MO/fraction.py b/mapytex/calculus/core/MO/fraction.py index 59fdaba..4e1c01b 100644 --- a/mapytex/calculus/core/MO/fraction.py +++ b/mapytex/calculus/core/MO/fraction.py @@ -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): """ base_value = Tree("/", - MO.factory(numerator), - MO.factory(denominator), - ) + MO.factory(numerator), + MO.factory(denominator), + ) if negative: value = Tree("-", None, base_value) else: diff --git a/mapytex/calculus/core/MO/mo.py b/mapytex/calculus/core/MO/mo.py index c2b2837..7b70013 100644 --- a/mapytex/calculus/core/MO/mo.py +++ b/mapytex/calculus/core/MO/mo.py @@ -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"] @@ -43,23 +43,22 @@ class MO(object): """MO for math object - This base class is representing int and Decimal. It stocks its value in - self.value and it - + This base class is representing int and Decimal. It stocks its value in + self.value and it + """ def __init__(self, value): """ Initiate the MO It should be idempotent. - + >>> a = MO(3) >>> a >>> a = MO(a) >>> a - """ 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)) @@ -154,7 +153,7 @@ class MOnumber(MO): return str(self.value) return f"- {abs(self.value)}" - + class MOstr(MO): """ Unknown math object like x or n""" @@ -197,7 +196,7 @@ class MOstr(MO): MO.__init__(self, value) self.is_scalar = False - + # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: diff --git a/mapytex/calculus/core/compute/__init__.py b/mapytex/calculus/core/compute/__init__.py index 33696ce..3aaf8af 100644 --- a/mapytex/calculus/core/compute/__init__.py +++ b/mapytex/calculus/core/compute/__init__.py @@ -10,7 +10,6 @@ Computing with MO """ -from ..operator import OPERATORS from .exceptions import ComputeError from .add import add from .minus import minus diff --git a/mapytex/calculus/core/compute/add.py b/mapytex/calculus/core/compute/add.py index 84c6e0e..88f2aa0 100644 --- a/mapytex/calculus/core/compute/add.py +++ b/mapytex/calculus/core/compute/add.py @@ -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' diff --git a/mapytex/calculus/core/compute/exceptions.py b/mapytex/calculus/core/compute/exceptions.py index bfd24a5..e980ea5 100644 --- a/mapytex/calculus/core/compute/exceptions.py +++ b/mapytex/calculus/core/compute/exceptions.py @@ -22,6 +22,9 @@ class MinusError(ComputeError): class MultiplyError(ComputeError): pass +class DivideError(ComputeError): + pass + # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/core/compute/minus.py b/mapytex/calculus/core/compute/minus.py index b815e4a..dda5667 100644 --- a/mapytex/calculus/core/compute/minus.py +++ b/mapytex/calculus/core/compute/minus.py @@ -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' diff --git a/mapytex/calculus/core/compute/multiply.py b/mapytex/calculus/core/compute/multiply.py index 08c8797..621f4ad 100644 --- a/mapytex/calculus/core/compute/multiply.py +++ b/mapytex/calculus/core/compute/multiply.py @@ -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' diff --git a/mapytex/calculus/core/coroutine.py b/mapytex/calculus/core/coroutine.py index a34be60..b0e54fd 100644 --- a/mapytex/calculus/core/coroutine.py +++ b/mapytex/calculus/core/coroutine.py @@ -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 diff --git a/mapytex/calculus/core/operator.py b/mapytex/calculus/core/operator.py index 86c7983..c3d9fc9 100644 --- a/mapytex/calculus/core/operator.py +++ b/mapytex/calculus/core/operator.py @@ -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): diff --git a/mapytex/calculus/core/renders/tree2tex.py b/mapytex/calculus/core/renders/tree2tex.py index 7d12520..b34c304 100644 --- a/mapytex/calculus/core/renders/tree2tex.py +++ b/mapytex/calculus/core/renders/tree2tex.py @@ -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) # ----------------------------- diff --git a/mapytex/calculus/core/renders/tree2txt.py b/mapytex/calculus/core/renders/tree2txt.py index 3cb384e..e2fcbb1 100644 --- a/mapytex/calculus/core/renders/tree2txt.py +++ b/mapytex/calculus/core/renders/tree2txt.py @@ -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) # ----------------------------- diff --git a/mapytex/calculus/core/str2.py b/mapytex/calculus/core/str2.py index 704a822..c9f64d3 100644 --- a/mapytex/calculus/core/str2.py +++ b/mapytex/calculus/core/str2.py @@ -25,7 +25,7 @@ class ParsingError(Exception): def maybe_it_is(cara): """ Return a function which return - Maybe if cara startwith the argument and True if it is cara + Maybe if cara startwith the argument and True if it is cara :exemple: @@ -170,7 +170,7 @@ def lookfor(condition, replace = lambda x:''.join(x)): ans = "maybe" elif found: ans = replace(acc) - acc = [] + acc = [] else: ans = False acc = [] @@ -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) @@ -320,7 +318,7 @@ def concurent_broadcast(target, lookfors = []): @coroutine def missing_times(target): - """ Coroutine which send a "*" when it's missing + """ Coroutine which send a "*" when it's missing Cases where a "*" is missing: - 2x or yx or )x @@ -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,13 +682,13 @@ 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)))) - + for i in expression: str2_corout.send(i) a = str2_corout.throw(STOOOP) diff --git a/mapytex/calculus/core/tree.py b/mapytex/calculus/core/tree.py index 3443d07..8e6d116 100644 --- a/mapytex/calculus/core/tree.py +++ b/mapytex/calculus/core/tree.py @@ -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. diff --git a/mapytex/calculus/core/tree_tools.py b/mapytex/calculus/core/tree_tools.py index 5c1a880..dbdf1aa 100644 --- a/mapytex/calculus/core/tree_tools.py +++ b/mapytex/calculus/core/tree_tools.py @@ -29,7 +29,7 @@ def to_nested_parenthesis(op, left, right): def infix_str_concatenate(op, left, right): """ Concatenate arguments placing op on the middle. - + :example: >>> infix_str_concatenate('+', 1, 2) @@ -39,7 +39,7 @@ def infix_str_concatenate(op, left, right): def postfix_concatenate(op, left, right): """ Concatenate arguments placing op on the middle. - + :example: >>> postfix_concatenate('+', 1, 2) @@ -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: