From b58242074aeec2bce87523993814b839ab1e708e Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Mon, 14 Jan 2008 00:26:26 -0500 Subject: [PATCH] Fix inheritance order on IdentityMapper. Add test. (+) Mapper: Variable is an AlgebraicLeaf. Fix NonrecursiveIdentityMapper. --- src/mapper/__init__.py | 10 +++++++--- src/mapper/substitutor.py | 8 ++++---- test/test_pymbolic.py | 7 ++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/mapper/__init__.py b/src/mapper/__init__.py index f0fd178..efbca3d 100644 --- a/src/mapper/__init__.py +++ b/src/mapper/__init__.py @@ -18,6 +18,9 @@ class Mapper(object): else: return self.map_foreign(expr, *args, **kwargs) + def map_variable(self, expr, *args, **kwargs): + return self.map_algebraic_leaf(self, expr, *args, **kwargs) + def map_subscript(self, expr, *args, **kwargs): return self.map_algebraic_leaf(self, expr, *args, **kwargs) @@ -161,9 +164,10 @@ class IdentityMapperBase(object): -class IdentityMapper(RecursiveMapper, IdentityMapperBase): +class IdentityMapper(IdentityMapperBase, RecursiveMapper): def handle_unsupported_expression(self, expr, *args, **kwargs): return expr -class NonrecursiveIdentityMapper(Mapper, IdentityMapperBase): - pass +class NonrecursiveIdentityMapper(IdentityMapperBase, Mapper): + def handle_unsupported_expression(self, expr, *args, **kwargs): + return expr diff --git a/src/mapper/substitutor.py b/src/mapper/substitutor.py index 3329da3..2aaffc8 100644 --- a/src/mapper/substitutor.py +++ b/src/mapper/substitutor.py @@ -5,23 +5,23 @@ import pymbolic.mapper class SubstitutionMapper(pymbolic.mapper.IdentityMapper): def __init__(self, variable_assignments): - self.Assignments = variable_assignments + self.assignments = variable_assignments def map_variable(self, expr): try: - return self.Assignments[expr] + return self.assignments[expr] except KeyError: return expr def map_subscript(self, expr): try: - return self.Assignments[expr] + return self.assignments[expr] except KeyError: return pymbolic.mapper.IdentityMapper.map_subscript(self, expr) def map_lookup(self, expr): try: - return self.Assignments[expr] + return self.assignments[expr] except KeyError: return pymbolic.mapper.IdentityMapper.map_lookup(self, expr) diff --git a/test/test_pymbolic.py b/test/test_pymbolic.py index 4b14c56..5c0aaf3 100644 --- a/test/test_pymbolic.py +++ b/test/test_pymbolic.py @@ -9,8 +9,13 @@ class TestPymbolic(unittest.TestCase): x = var("x") u = (x+1)**5 - print expand(u) + expand(u) + def test_substitute(self): + from pymbolic import parse, substitute, evaluate + u = parse("5+x.min**2") + xmin = parse("x.min") + assert evaluate(substitute(u, {xmin:25})) == 630 if __name__ == '__main__': unittest.main() -- GitLab