flake8 corr (not finish yet)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
from .operator import Operator, save_mainOp
|
||||
|
||||
|
||||
class Add(Operator):
|
||||
r""" The operator +
|
||||
|
||||
@@ -26,13 +27,13 @@ class Add(Operator):
|
||||
"""
|
||||
|
||||
_CARACT = {
|
||||
"operator" : "+", \
|
||||
"name" : "add",\
|
||||
"priority" : 1, \
|
||||
"arity" : 2, \
|
||||
"actions" : ("__add__","__radd__"), \
|
||||
"txt" : "{op1} + {op2}",\
|
||||
"tex" : "{op1} + {op2}",\
|
||||
"operator": "+",
|
||||
"name": "add",
|
||||
"priority": 1,
|
||||
"arity": 2,
|
||||
"actions": ("__add__", "__radd__"),
|
||||
"txt": "{op1} + {op2}",
|
||||
"tex": "{op1} + {op2}",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
@@ -50,20 +51,19 @@ class Add(Operator):
|
||||
if args[1][0] == "-":
|
||||
op1 = self.l_parenthesis(args[0], True)
|
||||
op2 = self.r_parenthesis(args[1][1:], True)
|
||||
ans = link.replace('+','-').format(op1 = op1, op2 = op2)
|
||||
ans = link.replace('+', '-').format(op1=op1, op2=op2)
|
||||
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
else:
|
||||
op1 = self.l_parenthesis(args[0], True)
|
||||
op2 = self.r_parenthesis(args[1], True)
|
||||
ans = link.format(op1 = op1, op2 = op2)
|
||||
ans = link.format(op1=op1, op2=op2)
|
||||
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
@@ -3,6 +3,7 @@
|
||||
|
||||
from .operator import Operator, save_mainOp
|
||||
|
||||
|
||||
class Div(Operator):
|
||||
r""" The operator /
|
||||
|
||||
@@ -26,12 +27,12 @@ class Div(Operator):
|
||||
"""
|
||||
|
||||
_CARACT = {
|
||||
"operator" : "/", \
|
||||
"name" : "div",\
|
||||
"priority" : 5, \
|
||||
"arity" : 2, \
|
||||
"txt" : "{op1} / {op2}",\
|
||||
"tex" : "\\frac{{ {op1} }}{{ {op2} }}",\
|
||||
"operator": "/",
|
||||
"name": "div",
|
||||
"priority": 5,
|
||||
"arity": 2,
|
||||
"txt": "{op1} / {op2}",
|
||||
"tex": "\\frac{{ {op1} }}{{ {op2} }}",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
@@ -43,11 +44,11 @@ class Div(Operator):
|
||||
return op1
|
||||
else:
|
||||
from ..fraction import Fraction
|
||||
return Fraction(op1,op2)
|
||||
return Fraction(op1, op2)
|
||||
|
||||
def __tex__(self, *args):
|
||||
# Pas besoin de parenthèses en plus pour \frac
|
||||
replacement = {"op"+str(i+1): op for (i,op) in enumerate(args)}
|
||||
replacement = {"op"+str(i+1): op for (i, op) in enumerate(args)}
|
||||
|
||||
ans = self.tex.format(**replacement)
|
||||
ans = save_mainOp(ans, self)
|
||||
|
@@ -4,6 +4,7 @@
|
||||
|
||||
from .operator import Operator, save_mainOp
|
||||
|
||||
|
||||
class Mul(Operator):
|
||||
r""" The operator *
|
||||
|
||||
@@ -30,13 +31,13 @@ class Mul(Operator):
|
||||
"""
|
||||
|
||||
_CARACT = {
|
||||
"operator" : "*", \
|
||||
"name" : "mul",\
|
||||
"priority" : 4, \
|
||||
"arity" : 2, \
|
||||
"actions" : ("__mul__","__rmul__"), \
|
||||
"txt" : "{op1} * {op2}",\
|
||||
"tex" : "{op1} \\times {op2}",\
|
||||
"operator": "*",
|
||||
"name": "mul",
|
||||
"priority": 4,
|
||||
"arity": 2,
|
||||
"actions": ("__mul__", "__rmul__"),
|
||||
"txt": "{op1} * {op2}",
|
||||
"tex": "{op1} \\times {op2}",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
@@ -106,17 +107,14 @@ class Mul(Operator):
|
||||
op2 = self.r_parenthesis(args[1], True)
|
||||
|
||||
if not self.is_visible(op1, op2):
|
||||
ans = "{op1} {op2}".format(op1 = op1, op2 = op2)
|
||||
ans = "{op1} {op2}".format(op1=op1, op2=op2)
|
||||
else:
|
||||
ans = link.format(op1 = op1, op2 = op2)
|
||||
ans = link.format(op1=op1, op2=op2)
|
||||
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
@@ -1,17 +1,16 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
#from debug.tools import report
|
||||
# from debug.tools import report
|
||||
|
||||
from ..generic import flatten_list
|
||||
|
||||
from ..generic import flatten_list, isNumber
|
||||
from functools import wraps
|
||||
import types
|
||||
|
||||
class Operator():
|
||||
|
||||
"""The operator class, is a string (representation of the operator) with its arity"""
|
||||
|
||||
def __init__(self, operator = "", name = "", priority = 0, actions = ("",""), txt = "", tex = "", arity = 2, **kwrds):
|
||||
def __init__(self, operator="", name="", priority=0, actions=("", ""), txt="", tex="", arity=2, **kwrds):
|
||||
""" Create an Operator """
|
||||
self.operator = operator
|
||||
self.name = name
|
||||
@@ -29,8 +28,7 @@ class Operator():
|
||||
return getattr(args[0], self.actions)()
|
||||
|
||||
elif self.arity == 2:
|
||||
#if type(args[1]) == int:
|
||||
if issubclass(type(args[1]),int):
|
||||
if issubclass(type(args[1]), int):
|
||||
return getattr(args[0], self.actions[0])(args[1])
|
||||
else:
|
||||
return getattr(args[1], self.actions[1])(args[0])
|
||||
@@ -45,12 +43,12 @@ class Operator():
|
||||
"""
|
||||
if self.arity == 1:
|
||||
op1 = self.l_parenthesis(args[0], True)
|
||||
ans = link.format(op1 = op1)
|
||||
ans = link.format(op1=op1)
|
||||
|
||||
elif self.arity == 2:
|
||||
op1 = self.l_parenthesis(args[0], True)
|
||||
op2 = self.r_parenthesis(args[1], True)
|
||||
ans = link.format(op1 = op1, op2 = op2)
|
||||
ans = link.format(op1=op1, op2=op2)
|
||||
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
@@ -174,7 +172,7 @@ class Operator():
|
||||
ans = opl
|
||||
elif opl.mainOp.priority < self.priority:
|
||||
ans = flatten_list(["(", opl, ")"])
|
||||
except AttributeError as e:
|
||||
except AttributeError:
|
||||
# op has not the attribute priority
|
||||
pass
|
||||
|
||||
@@ -214,6 +212,7 @@ class Operator():
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
|
||||
def save_mainOp(obj, mainOp):
|
||||
"""Create a temporary class build over built-in type to stock the main operation of a calculus
|
||||
|
||||
|
@@ -2,7 +2,6 @@
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import types
|
||||
from .add import Add
|
||||
from .div import Div
|
||||
from .mul import Mul
|
||||
@@ -20,7 +19,7 @@ class Operator_set(object):
|
||||
""" Initiate the operator_set """
|
||||
self._operators = {}
|
||||
|
||||
def get_op(self, op, arity = 2):
|
||||
def get_op(self, op, arity=2):
|
||||
r"""Return the corresponding operator
|
||||
|
||||
:op: symbole of the op
|
||||
@@ -31,7 +30,7 @@ class Operator_set(object):
|
||||
>>> op.get_op('+')
|
||||
+
|
||||
>>> mul = op.get_op('*')
|
||||
Traceback (most recent call last):
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
KeyError: '* (arity: 2) is not available'
|
||||
>>> op.store_operator(Mul())
|
||||
@@ -42,7 +41,7 @@ class Operator_set(object):
|
||||
'{op1} * {op2}'
|
||||
>>> op.store_operator(Sub1())
|
||||
>>> sub1 = op.get_op('-')
|
||||
Traceback (most recent call last):
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
KeyError: '- (arity: 2) is not available'
|
||||
>>> sub1 = op.get_op('-', 1)
|
||||
@@ -52,7 +51,7 @@ class Operator_set(object):
|
||||
try:
|
||||
return getattr(self, self._operators[(op, arity)])
|
||||
except KeyError:
|
||||
raise KeyError("{theOp} (arity: {arity}) is not available".format(theOp = op, arity = arity))
|
||||
raise KeyError("{theOp} (arity: {arity}) is not available".format(theOp=op, arity=arity))
|
||||
|
||||
def available_op(self):
|
||||
""" Get the set of available operators strings
|
||||
@@ -72,8 +71,7 @@ class Operator_set(object):
|
||||
return set([i[0] for i in self._operators])
|
||||
|
||||
def can_be_operator(cls, symbole):
|
||||
r"""
|
||||
Tell if the symbole can be an operator
|
||||
r"""Tell if the symbole can be an operator
|
||||
|
||||
>>> op = Operator_set()
|
||||
>>> op.can_be_operator("+")
|
||||
@@ -81,7 +79,7 @@ class Operator_set(object):
|
||||
>>> op.store_operator(Add())
|
||||
>>> op.can_be_operator("+")
|
||||
True
|
||||
|
||||
|
||||
"""
|
||||
if type(symbole) == str:
|
||||
return symbole in [i[0] for i in cls._operators]
|
||||
@@ -89,7 +87,7 @@ class Operator_set(object):
|
||||
return False
|
||||
|
||||
def store_operator(self, operator):
|
||||
""" Save the operator as a method and
|
||||
""" Save the operator as a method and
|
||||
|
||||
:param operator: the operator (the class) (it will be accessible through .name method name.
|
||||
|
||||
@@ -105,7 +103,7 @@ class Operator_set(object):
|
||||
>>> a.tex
|
||||
'{op1} + {op2}'
|
||||
>>> op.store_operator("+")
|
||||
Traceback (most recent call last):
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: + is not en Operator it's a <class 'str'>
|
||||
"""
|
||||
|
Reference in New Issue
Block a user