def make_stringifier(self, originating_stringifier=None): """Return a :class:`pymbolic.mapper.Mapper` instance that can be used to generate a human-readable representation of *self*. Usually a subclass of :class:`pymbolic.mapper.stringifier.StringifyMapper`. :arg originating_stringifier: If provided, the newly created stringifier should carry forward attributes and settings of *originating_stringifier*. """ if originating_stringifier is None: stringify_mapper_args = () else: stringify_mapper_args = (originating_stringifier.constant_mapper,) try: stringifier_class_getter = self.stringifier except AttributeError: from pymbolic.mapper.stringifier import StringifyMapper return StringifyMapper(*stringify_mapper_args) else: from warnings import warn warn("%s overrides 'stringifier', which is deprecated. " "Override 'make_stringifier' instead. " "Backward compatibility will go away " "sometime after June 2020." % type(self).__name__, DeprecationWarning) return stringifier_class_getter()(*stringify_mapper_args)
def assert_parse_roundtrip(expr): from pymbolic.mapper.stringifier import StringifyMapper strified = StringifyMapper()(expr) from pymbolic import parse parsed_expr = parse(strified) print(expr) print(parsed_expr) assert expr == parsed_expr
def test_latex_mapper(): from pymbolic import parse from pymbolic.mapper.stringifier import LaTeXMapper, StringifyMapper tm = LaTeXMapper() sm = StringifyMapper() equations = [] def add(expr): # Add an equation to the list of tests. equations.append(r"\[%s\] %% from: %s" % (tm(expr), sm(expr))) add(parse("a * b + c")) add(parse("f(a,b,c)")) add(parse("a ** b ** c")) add(parse("(a | b) ^ ~c")) add(parse("a << b")) add(parse("a >> b")) add(parse("a[i,j,k]")) add(parse("a[1:3]")) add(parse("a // b")) add(parse("not (a or b) and c")) add(parse("(a % b) % c")) add(parse("(a >= b) or (b <= c)")) add(prim.Min((1,)) + prim.Max((1, 2))) add(prim.Substitution(prim.Variable("x") ** 2, ("x",), (2,))) add(prim.Derivative(parse("x**2"), ("x",))) # Run LaTeX and ensure the file compiles. import os import tempfile import subprocess import shutil latex_dir = tempfile.mkdtemp("pymbolic") try: tex_file_path = os.path.join(latex_dir, "input.tex") with open(tex_file_path, "w") as tex_file: contents = LATEX_TEMPLATE % "\n".join(equations) tex_file.write(contents) try: subprocess.check_output( ["latex", "-interaction=nonstopmode", "-output-directory=%s" % latex_dir, tex_file_path], universal_newlines=True) except OSError: # FIXME: Should be FileNotFoundError on Py3 pytest.skip("latex command not found") except subprocess.CalledProcessError as err: assert False, str(err.output) finally: shutil.rmtree(latex_dir)
def make_stringifier(self, originating_stringifier=None): """Return a :class:`pymbolic.mapper.Mapper` instance that can be used to generate a human-readable representation of *self*. Usually a subclass of :class:`pymbolic.mapper.stringifier.StringifyMapper`. :arg originating_stringifier: If provided, the newly created stringifier should carry forward attributes and settings of *originating_stringifier*. """ from pymbolic.mapper.stringifier import StringifyMapper return StringifyMapper()
def map_foreign(self, expr, enclosing_prec): return StringifyMapper.map_foreign(self, expr, enclosing_prec)
def __init__(self): StringifyMapper.__init__(self, constant_mapper=_constant_mapper)
def assert_parse_roundtrip(expr_str): expr = parse(expr_str) from pymbolic.mapper.stringifier import StringifyMapper strified = StringifyMapper()(expr) assert strified == expr_str, (strified, expr_str)