From a0200dacfae1b64a4b4a2d6cb674e17c1e0447e7 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Mon, 5 Dec 2016 19:45:13 -0600 Subject: [PATCH] Run Flake8 during CI --- pymbolic/algorithm.py | 42 +++++++++++++++------------------ pymbolic/cse.py | 10 -------- pymbolic/functions.py | 29 ++++++++++++++--------- pymbolic/interop/ast.py | 34 +++++++++++++------------- pymbolic/interop/sympy.py | 22 ++++++++--------- pymbolic/mapper/flattener.py | 4 ---- pymbolic/mapper/flop_counter.py | 2 -- pymbolic/maxima.py | 2 +- pymbolic/parser.py | 1 + pymbolic/sympy_interface.py | 2 +- setup.cfg | 1 + test/simple.py | 8 +++---- 12 files changed, 73 insertions(+), 84 deletions(-) diff --git a/pymbolic/algorithm.py b/pymbolic/algorithm.py index 4ea9dcb..603a9a1 100644 --- a/pymbolic/algorithm.py +++ b/pymbolic/algorithm.py @@ -1,10 +1,4 @@ -from __future__ import division -from __future__ import absolute_import -from __future__ import print_function -import six -from six.moves import range -from six.moves import zip -from functools import reduce +from __future__ import division, absolute_import, print_function __copyright__ = "Copyright (C) 2009-2013 Andreas Kloeckner" @@ -28,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import six +from six.moves import range, zip, reduce import cmath from pytools import memoize @@ -81,12 +77,12 @@ def extended_euclidean(q, r): p, a, b = extended_euclidean(r, q) return p, b, a - Q = 1, 0 - R = 0, 1 + Q = 1, 0 # noqa + R = 0, 1 # noqa while r: quot, t = divmod(q, r) - T = Q[0] - quot*R[0], Q[1] - quot*R[1] + T = Q[0] - quot*R[0], Q[1] - quot*R[1] # noqa q, r = r, t Q, R = R, T @@ -115,20 +111,20 @@ def lcm(q, r): # {{{ fft @memoize -def find_factors(N): +def find_factors(n): from math import sqrt - N1 = 2 - max_N1 = int(sqrt(N))+1 - while N % N1 != 0 and N1 <= max_N1: - N1 += 1 + n1 = 2 + max_n1 = int(sqrt(n))+1 + while n % n1 != 0 and n1 <= max_n1: + n1 += 1 - if N1 > max_N1: - N1 = N + if n1 > max_n1: + n1 = n - N2 = N // N1 + n2 = n // n1 - return N1, N2 + return n1, n2 def fft(x, sign=1, wrap_intermediate=lambda x: x): @@ -149,12 +145,12 @@ def fft(x, sign=1, wrap_intermediate=lambda x: x): from math import pi import numpy - N = len(x) + n = len(x) - if N == 1: + if n == 1: return x - N1, N2 = find_factors(N) + N1, N2 = find_factors(n) sub_ffts = [ wrap_intermediate( @@ -222,7 +218,7 @@ def sym_fft(x, sign=1): # }}} -def csr_matrix_multiply(S, x): +def csr_matrix_multiply(S, x): # noqa """Multiplies a :class:`scipy.sparse.csr_matrix` S by an object-array vector x. """ h, w = S.shape diff --git a/pymbolic/cse.py b/pymbolic/cse.py index e57dd39..d145946 100644 --- a/pymbolic/cse.py +++ b/pymbolic/cse.py @@ -30,8 +30,6 @@ from pymbolic.mapper import IdentityMapper, WalkMapper COMMUTATIVE_CLASSES = (prim.Sum, prim.Product) - - class NormalizedKeyGetter(object): def __call__(self, expr): if isinstance(expr, COMMUTATIVE_CLASSES): @@ -44,8 +42,6 @@ class NormalizedKeyGetter(object): return expr - - class UseCountMapper(WalkMapper): def __init__(self, get_key): self.subexpr_counts = {} @@ -80,9 +76,6 @@ class UseCountMapper(WalkMapper): self.subexpr_counts[key] = 1 - - - class CSEMapper(IdentityMapper): def __init__(self, to_eliminate, get_key): self.to_eliminate = to_eliminate @@ -136,8 +129,6 @@ class CSEMapper(IdentityMapper): tuple(self.rec(v) for v in expr.values)) - - def tag_common_subexpressions(exprs): get_key = NormalizedKeyGetter() ucm = UseCountMapper(get_key) @@ -155,4 +146,3 @@ def tag_common_subexpressions(exprs): cse_mapper = CSEMapper(to_eliminate, get_key) result = [cse_mapper(expr) for expr in exprs] return result - diff --git a/pymbolic/functions.py b/pymbolic/functions.py index 181c686..4a0ae78 100644 --- a/pymbolic/functions.py +++ b/pymbolic/functions.py @@ -1,5 +1,4 @@ -from __future__ import division -from __future__ import absolute_import +from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2009-2013 Andreas Kloeckner" @@ -24,21 +23,29 @@ THE SOFTWARE. """ - - import pymbolic.primitives as primitives +def sin(x): + return primitives.Call( + primitives.Lookup(primitives.Variable("math"), "sin"), (x,)) -def sin(x): - return primitives.Call(primitives.Lookup(primitives.Variable("math"), "sin"), (x,)) def cos(x): - return primitives.Call(primitives.Lookup(primitives.Variable("math"), "cos"), (x,)) + return primitives.Call( + primitives.Lookup(primitives.Variable("math"), "cos"), (x,)) + + def tan(x): - return primitives.Call(primitives.Lookup(primitives.Variable("math"), "tan"), (x,)) + return primitives.Call( + primitives.Lookup(primitives.Variable("math"), "tan"), (x,)) + + def log(x): - return primitives.Call(primitives.Lookup(primitives.Variable("math"), "log"), (x,)) -def exp(x): - return primitives.Call(primitives.Lookup(primitives.Variable("math"), "exp"), (x,)) + return primitives.Call( + primitives.Lookup(primitives.Variable("math"), "log"), (x,)) + +def exp(x): + return primitives.Call( + primitives.Lookup(primitives.Variable("math"), "exp"), (x,)) diff --git a/pymbolic/interop/ast.py b/pymbolic/interop/ast.py index 5455870..6a826f1 100644 --- a/pymbolic/interop/ast.py +++ b/pymbolic/interop/ast.py @@ -91,13 +91,13 @@ class ASTMapper(object): # {{{ mapper class ASTToPymbolic(ASTMapper): - def _add(x, y): + def _add(x, y): # noqa return p.Sum((x, y)) - def _sub(x, y): + def _sub(x, y): # noqa return p.Sum((x, p.Product(((-1), y)))) - def _mult(x, y): + def _mult(x, y): # noqa return p.Product((x, y)) bin_op_map = { @@ -116,7 +116,7 @@ class ASTToPymbolic(ASTMapper): ast.BitAnd: p.BitwiseAnd, } - def map_BinOp(self, expr): + def map_BinOp(self, expr): # noqa try: op_constructor = self.bin_op_map[type(expr.op)] except KeyError: @@ -127,7 +127,7 @@ class ASTToPymbolic(ASTMapper): return op_constructor(self.rec(expr.left), self.rec(expr.right)) - def _neg(x): + def _neg(x): # noqa return p.Product((-1), x) unary_op_map = { @@ -137,7 +137,7 @@ class ASTToPymbolic(ASTMapper): ast.USub: _neg, } - def map_UnaryOp(self, expr): + def map_UnaryOp(self, expr): # noqa try: op_constructor = self.unary_op_map[expr.op] except KeyError: @@ -148,7 +148,7 @@ class ASTToPymbolic(ASTMapper): return op_constructor(self.rec(expr.left), self.rec(expr.right)) - def map_IfExp(self, expr): + def map_IfExp(self, expr): # noqa # (expr test, expr body, expr orelse) return p.If(self.rec(expr.test), self.rec(expr.body), self.rec(expr.orelse)) @@ -165,7 +165,7 @@ class ASTToPymbolic(ASTMapper): # NotIn } - def map_Compare(self, expr): + def map_Compare(self, expr): # noqa # (expr left, cmpop* ops, expr* comparators) op, = expr.ops @@ -182,7 +182,7 @@ class ASTToPymbolic(ASTMapper): return p.Comparison(self.rec(expr.left), comp, self.rec(right)) - def map_Call(self, expr): + def map_Call(self, expr): # noqa # (expr func, expr* args, keyword* keywords) func = self.rec(expr.func) args = tuple(self.rec(arg) for arg in expr.args) @@ -194,25 +194,25 @@ class ASTToPymbolic(ASTMapper): else: return p.Call(func, args) - def map_Num(self, expr): + def map_Num(self, expr): # noqa # (object n) -- a number as a PyObject. return expr.n - def map_Str(self, expr): + def map_Str(self, expr): # noqa return expr.s - def map_Bytes(self, expr): + def map_Bytes(self, expr): # noqa return expr.s - def map_NameConstant(self, expr): + def map_NameConstant(self, expr): # noqa # (singleton value) return expr.value - def map_Attribute(self, expr): + def map_Attribute(self, expr): # noqa # (expr value, identifier attr, expr_context ctx) return p.Lookup(self.rec(expr.value), expr.attr) - def map_Subscript(self, expr): + def map_Subscript(self, expr): # noqa # (expr value, slice slice, expr_context ctx) def none_or_rec(x): if x is None: @@ -234,11 +234,11 @@ class ASTToPymbolic(ASTMapper): # def map_Starred(self, expr): - def map_Name(self, expr): + def map_Name(self, expr): # noqa # (identifier id, expr_context ctx) return p.Variable(expr.id) - def map_Tuple(self, expr): + def map_Tuple(self, expr): # noqa # (expr* elts, expr_context ctx) return tuple(self.rec(ti) for ti in expr.elts) diff --git a/pymbolic/interop/sympy.py b/pymbolic/interop/sympy.py index d479dcc..0167338 100644 --- a/pymbolic/interop/sympy.py +++ b/pymbolic/interop/sympy.py @@ -79,25 +79,25 @@ def make_cse(arg, prefix=None): # {{{ sympy -> pymbolic class SympyToPymbolicMapper(SympyMapper): - def map_Symbol(self, expr): + def map_Symbol(self, expr): # noqa return prim.Variable(expr.name) - def map_ImaginaryUnit(self, expr): + def map_ImaginaryUnit(self, expr): # noqa return 1j - def map_Float(self, expr): + def map_Float(self, expr): # noqa return float(expr) - def map_Pi(self, expr): + def map_Pi(self, expr): # noqa return float(expr) - def map_Add(self, expr): + def map_Add(self, expr): # noqa return prim.Sum(tuple(self.rec(arg) for arg in expr.args)) - def map_Mul(self, expr): + def map_Mul(self, expr): # noqa return prim.Product(tuple(self.rec(arg) for arg in expr.args)) - def map_Rational(self, expr): + def map_Rational(self, expr): # noqa num = self.rec(expr.p) denom = self.rec(expr.q) @@ -105,20 +105,20 @@ class SympyToPymbolicMapper(SympyMapper): return num return prim.Quotient(num, denom) - def map_Pow(self, expr): + def map_Pow(self, expr): # noqa return prim.Power(self.rec(expr.base), self.rec(expr.exp)) - def map_Subs(self, expr): + def map_Subs(self, expr): # noqa return prim.Substitution(self.rec(expr.expr), tuple(v.name for v in expr.variables), tuple(self.rec(v) for v in expr.point), ) - def map_Derivative(self, expr): + def map_Derivative(self, expr): # noqa return prim.Derivative(self.rec(expr.expr), tuple(v.name for v in expr.variables)) - def map_CSE(self, expr): + def map_CSE(self, expr): # noqa return prim.CommonSubexpression( self.rec(expr.args[0]), expr.prefix) diff --git a/pymbolic/mapper/flattener.py b/pymbolic/mapper/flattener.py index 37239e5..f301321 100644 --- a/pymbolic/mapper/flattener.py +++ b/pymbolic/mapper/flattener.py @@ -25,8 +25,6 @@ THE SOFTWARE. from pymbolic.mapper import IdentityMapper - - class FlattenMapper(IdentityMapper): def map_sum(self, expr): from pymbolic.primitives import flattened_sum @@ -37,7 +35,5 @@ class FlattenMapper(IdentityMapper): return flattened_product(self.rec(ch) for ch in expr.children) - - def flatten(expr): return FlattenMapper()(expr) diff --git a/pymbolic/mapper/flop_counter.py b/pymbolic/mapper/flop_counter.py index cb72bd4..445b04e 100644 --- a/pymbolic/mapper/flop_counter.py +++ b/pymbolic/mapper/flop_counter.py @@ -25,8 +25,6 @@ THE SOFTWARE. from pymbolic.mapper import CombineMapper - - class FlopCounter(CombineMapper): def combine(self, values): return sum(values) diff --git a/pymbolic/maxima.py b/pymbolic/maxima.py index 7735410..6c52dea 100644 --- a/pymbolic/maxima.py +++ b/pymbolic/maxima.py @@ -1,4 +1,4 @@ -from pymbolic.interop.maxima import * +from pymbolic.interop.maxima import * # noqa from warnings import warn warn("pymbolic.maxima is deprecated. Use pymbolic.interop.maxima instead", diff --git a/pymbolic/parser.py b/pymbolic/parser.py index 9a388d0..00ef94a 100644 --- a/pymbolic/parser.py +++ b/pymbolic/parser.py @@ -458,4 +458,5 @@ class Parser(object): pstate.raise_parse_error("leftover input after completed parse") return result + parse = Parser() diff --git a/pymbolic/sympy_interface.py b/pymbolic/sympy_interface.py index 644ba8b..ed6eb21 100644 --- a/pymbolic/sympy_interface.py +++ b/pymbolic/sympy_interface.py @@ -1,4 +1,4 @@ -from pymbolic.interop.sympy import * +from pymbolic.interop.sympy import * # noqa from warnings import warn warn("pymbolic.sympy_interface is deprecated. Use pymbolic.interop.sympy instead", diff --git a/setup.cfg b/setup.cfg index 2344964..4a1dc55 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,4 @@ [flake8] ignore = E126,E127,E128,E123,E226,E241,E242,W503,E402 max-line-length=85 +exclude=doc,pymbolic/polynomial.py diff --git a/test/simple.py b/test/simple.py index 5d4a0df..5d5e337 100644 --- a/test/simple.py +++ b/test/simple.py @@ -1,4 +1,4 @@ -from __future__ import division +from __future__ import division, print_function __copyright__ = "Copyright (C) 2009-2013 Andreas Kloeckner" @@ -31,8 +31,8 @@ y = var("y") expr2 = 3*x+5-y expr = parse("3*x+5-y") -print expr -print expr2 +print(expr) +print(expr2) dm = DependencyMapper() -print dm(expr) +print(dm(expr)) -- GitLab