autopep8 on all files but operator.py
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
#debuging
|
||||
# debuging
|
||||
#from debug.tools import report
|
||||
|
||||
from .generic import Stack, flatten_list, expand_list, isNumber, isOperator, isNumerand
|
||||
@@ -13,13 +13,16 @@ from .random_expression import RdExpression
|
||||
|
||||
__all__ = ['Expression']
|
||||
|
||||
|
||||
class Fake_int(int, Explicable):
|
||||
isNumber = True
|
||||
|
||||
def __init__(self, val):
|
||||
super(Fake_int, self).__init__(val)
|
||||
self._val = val
|
||||
self.postfix_tokens = [self]
|
||||
self.steps = []
|
||||
|
||||
def simplify(self):
|
||||
return Fake_int(self._val)
|
||||
|
||||
@@ -28,7 +31,7 @@ class Expression(Explicable):
|
||||
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
|
||||
|
||||
@classmethod
|
||||
def random(self, form="", conditions=[], val_min = -10, val_max=10):
|
||||
def random(self, form="", conditions=[], val_min=-10, val_max=10):
|
||||
"""Create a random expression from form and with conditions
|
||||
|
||||
:param form: the form of the expression (/!\ variables need to be in brackets {})
|
||||
@@ -41,7 +44,7 @@ class Expression(Explicable):
|
||||
return Expression(random_generator(val_min, val_max))
|
||||
|
||||
@classmethod
|
||||
def tmp_render(cls, render = lambda _,x:Expression(x)):
|
||||
def tmp_render(cls, render=lambda _, x: Expression(x)):
|
||||
""" Same ad tmp_render for Renderable but default render is Expression
|
||||
|
||||
>>> exp = Expression("2*3/5")
|
||||
@@ -87,44 +90,56 @@ class Expression(Explicable):
|
||||
"""
|
||||
expression = object.__new__(cls)
|
||||
|
||||
if type(exp) == str:
|
||||
if isinstance(exp, str):
|
||||
expression.postfix_tokens = str2tokens(exp)
|
||||
|
||||
elif type(exp) == list:
|
||||
# Ici on ne peut convertir les "+" en opérateur que s'ils sont d'arité 2.
|
||||
exp_mod_op = [op.get_op(i) if op.can_be_operator(i) else i for i in exp]
|
||||
expression.postfix_tokens = flatten_list([tok.postfix_tokens if Expression.isExpression(tok) else tok for tok in exp_mod_op])
|
||||
elif isinstance(exp, list):
|
||||
# Ici on ne peut convertir les "+" en opérateur que s'ils sont
|
||||
# d'arité 2.
|
||||
exp_mod_op = [
|
||||
op.get_op(i) if op.can_be_operator(i) else i for i in exp]
|
||||
expression.postfix_tokens = flatten_list(
|
||||
[tok.postfix_tokens if Expression.isExpression(tok) else tok for tok in exp_mod_op])
|
||||
|
||||
elif type(exp) == Expression:
|
||||
elif isinstance(exp, Expression):
|
||||
return exp
|
||||
|
||||
elif isNumerand(exp):
|
||||
expression.postfix_tokens = [exp]
|
||||
|
||||
else:
|
||||
raise ValueError("Can't build Expression with {} object".format(type(exp)))
|
||||
raise ValueError(
|
||||
"Can't build Expression with {} object".format(
|
||||
type(exp)))
|
||||
|
||||
if len(expression.postfix_tokens) == 1:
|
||||
token = expression.postfix_tokens[0]
|
||||
|
||||
if type(token) == Fake_int or type(token) == int:
|
||||
if isinstance(token, Fake_int) or isinstance(token, int):
|
||||
return Fake_int(token)
|
||||
|
||||
elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
|
||||
ans = expression.postfix_tokens[0]
|
||||
return ans
|
||||
|
||||
elif type(token) == str:
|
||||
elif isinstance(token, str):
|
||||
# TODO: Pourquoi ne pas créer directement un polynom ici? |jeu. févr. 26 18:59:24 CET 2015
|
||||
# On crée un faux str en ajoutant la méthode simplify et simplified et la caractérisique isNumber
|
||||
simplify = lambda x:[x]
|
||||
# On crée un faux str en ajoutant la méthode simplify et
|
||||
# simplified et la caractérisique isNumber
|
||||
simplify = lambda x: [x]
|
||||
is_polynom = True
|
||||
methods_attr = {'simplify':simplify, '_isPolynom': is_polynom, 'postfix_tokens': [token]}
|
||||
fake_token = type('fake_str', (str,Explicable, ), methods_attr)(token)
|
||||
methods_attr = {
|
||||
'simplify': simplify,
|
||||
'_isPolynom': is_polynom,
|
||||
'postfix_tokens': [token]}
|
||||
fake_token = type(
|
||||
'fake_str', (str, Explicable, ), methods_attr)(token)
|
||||
return fake_token
|
||||
|
||||
else:
|
||||
raise ValueError("Unknow token type in Expression: {}".format(type(token)))
|
||||
raise ValueError(
|
||||
"Unknow token type in Expression: {}".format(
|
||||
type(token)))
|
||||
|
||||
else:
|
||||
expression._isExpression = 1
|
||||
@@ -139,7 +154,8 @@ class Expression(Explicable):
|
||||
return self.STR_RENDER(self.postfix_tokens)
|
||||
|
||||
def __repr__(self):
|
||||
return " ".join(["<", str(self.__class__) , str(self.postfix_tokens), ">"])
|
||||
return " ".join(["<", str(self.__class__),
|
||||
str(self.postfix_tokens), ">"])
|
||||
|
||||
def simplify(self):
|
||||
""" Compute entirely the expression and return the result with .steps attribute """
|
||||
@@ -157,9 +173,10 @@ class Expression(Explicable):
|
||||
tmpTokenList = []
|
||||
|
||||
while len(tokenList) > 2:
|
||||
# on va chercher les motifs du genre A B +, quand l'operateur est d'arité 2, pour les calculer
|
||||
# on va chercher les motifs du genre A B +, quand l'operateur est
|
||||
# d'arité 2, pour les calculer
|
||||
if isNumerand(tokenList[0]) and isNumerand(tokenList[1]) \
|
||||
and isOperator(tokenList[2]) and tokenList[2].arity == 2 :
|
||||
and isOperator(tokenList[2]) and tokenList[2].arity == 2:
|
||||
|
||||
# S'il y a une opération à faire
|
||||
op1 = tokenList[0]
|
||||
@@ -170,7 +187,8 @@ class Expression(Explicable):
|
||||
|
||||
tmpTokenList.append(res)
|
||||
|
||||
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
||||
# Comme on vient de faire le calcul, on peut détruire aussi les
|
||||
# deux prochains termes
|
||||
del tokenList[0:3]
|
||||
|
||||
# Et les motifs du gens A -, quand l'operateur est d'arité 1
|
||||
@@ -185,7 +203,8 @@ class Expression(Explicable):
|
||||
|
||||
tmpTokenList.append(res)
|
||||
|
||||
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
||||
# Comme on vient de faire le calcul, on peut détruire aussi les
|
||||
# deux prochains termes
|
||||
del tokenList[0:2]
|
||||
|
||||
else:
|
||||
@@ -194,7 +213,7 @@ class Expression(Explicable):
|
||||
del tokenList[0]
|
||||
|
||||
if len(tokenList) == 2 and isNumerand(tokenList[0]) \
|
||||
and isOperator(tokenList[1]) and tokenList[1].arity == 1:
|
||||
and isOperator(tokenList[1]) and tokenList[1].arity == 1:
|
||||
# S'il reste deux éléments dont un operation d'arité 1
|
||||
op1 = tokenList[0]
|
||||
operator = tokenList[1]
|
||||
@@ -203,7 +222,8 @@ class Expression(Explicable):
|
||||
|
||||
tmpTokenList.append(res)
|
||||
|
||||
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
||||
# Comme on vient de faire le calcul, on peut détruire aussi les
|
||||
# deux prochains termes
|
||||
del tokenList[0:2]
|
||||
|
||||
tmpTokenList += tokenList
|
||||
@@ -239,8 +259,8 @@ class Expression(Explicable):
|
||||
try:
|
||||
other._isExpression
|
||||
except AttributeError:
|
||||
return 0
|
||||
return 1
|
||||
return 0
|
||||
return 1
|
||||
|
||||
# -----------
|
||||
# Expression act as container from self.postfix_tokens
|
||||
@@ -255,17 +275,23 @@ class Expression(Explicable):
|
||||
# Some math manipulations
|
||||
|
||||
def operate(self, other, operator):
|
||||
if type(other) == Expression:
|
||||
return Expression(self.postfix_tokens + other.postfix_tokens + [operator])
|
||||
elif type(other) == list:
|
||||
if isinstance(other, Expression):
|
||||
return Expression(
|
||||
self.postfix_tokens +
|
||||
other.postfix_tokens +
|
||||
[operator])
|
||||
elif isinstance(other, list):
|
||||
return Expression(self.postfix_tokens + other + [operator])
|
||||
else:
|
||||
return Expression(self.postfix_tokens + [other] + [operator])
|
||||
|
||||
def roperate(self, other, operator):
|
||||
if type(other) == Expression:
|
||||
return Expression(other.postfix_tokens + self.postfix_tokens + [operator])
|
||||
elif type(other) == list:
|
||||
if isinstance(other, Expression):
|
||||
return Expression(
|
||||
other.postfix_tokens +
|
||||
self.postfix_tokens +
|
||||
[operator])
|
||||
elif isinstance(other, list):
|
||||
return Expression(other + self.postfix_tokens + [operator])
|
||||
else:
|
||||
return Expression([other] + self.postfix_tokens + [operator])
|
||||
@@ -358,7 +384,7 @@ def untest(exp):
|
||||
b = a.simplify()
|
||||
|
||||
for i in b.explain():
|
||||
#print(type(i))
|
||||
# print(type(i))
|
||||
print(i)
|
||||
|
||||
#print(type(a.simplified()), ":", a.simplified())
|
||||
@@ -371,27 +397,26 @@ if __name__ == '__main__':
|
||||
Ar = A.simplify()
|
||||
for i in Ar.explain():
|
||||
print(i)
|
||||
#print("------------")
|
||||
#for i in Ar.explain():
|
||||
# print("------------")
|
||||
# for i in Ar.explain():
|
||||
# print(i)
|
||||
|
||||
#print(type(Ar))
|
||||
# print(type(Ar))
|
||||
|
||||
|
||||
#print('\n-----------')
|
||||
# print('\n-----------')
|
||||
#A = Expression("-6 / 3 + 10 / -5")
|
||||
#Ar = A.simplify()
|
||||
#for i in Ar.explain():
|
||||
# for i in Ar.explain():
|
||||
# print(i)
|
||||
|
||||
#print('\n-----------')
|
||||
# print('\n-----------')
|
||||
#A = Expression("1/3 + 4/6")
|
||||
#Ar = A.simplify()
|
||||
#for i in Ar.explain():
|
||||
# for i in Ar.explain():
|
||||
# print(i)
|
||||
|
||||
#import doctest
|
||||
#doctest.testmod()
|
||||
# doctest.testmod()
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
|
||||
Reference in New Issue
Block a user