Skip to content

Commit

Permalink
Merged in garth/fix-issue-99 (pull request #101)
Browse files Browse the repository at this point in the history
Fix Python deprecation
  • Loading branch information
garth-wells committed Sep 30, 2018
2 parents 09d5ec8 + a699da0 commit d0bc236
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions ufl/algorithms/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
#
# Modified by Anders Logg, 2009-2010

from inspect import getargspec
from ufl.log import error
from ufl.classes import Variable, all_ufl_classes
import inspect

from ufl.algorithms.map_integrands import map_integrands
from ufl.classes import Variable, all_ufl_classes
from ufl.log import error


def is_post_handler(function):
"Is this a handler that expects transformed children as input?"
insp = getargspec(function)
insp = inspect.getfullargspec(function)
num_args = len(insp[0]) + int(insp[1] is not None)
visit_children_first = num_args > 2
return visit_children_first
Expand All @@ -51,7 +52,7 @@ def __init__(self, variable_cache=None):
# first time this is run for a particular class
cache_data = Transformer._handlers_cache.get(type(self))
if not cache_data:
cache_data = [None]*len(all_ufl_classes)
cache_data = [None] * len(all_ufl_classes)
# For all UFL classes
for classobject in all_ufl_classes:
# Iterate over the inheritance chain
Expand All @@ -63,27 +64,32 @@ def __init__(self, variable_cache=None):
handler_name = c._ufl_handler_name_
function = getattr(self, handler_name, None)
if function:
cache_data[classobject._ufl_typecode_] = handler_name, is_post_handler(function)
cache_data[
classobject.
_ufl_typecode_] = handler_name, is_post_handler(
function)
break
Transformer._handlers_cache[type(self)] = cache_data

# Build handler list for this particular class (get functions
# bound to self)
self._handlers = [(getattr(self, name), post) for (name, post) in cache_data]
self._handlers = [(getattr(self, name), post)
for (name, post) in cache_data]
# Keep a stack of objects visit is called on, to ease
# backtracking
self._visit_stack = []

def print_visit_stack(self):
print("/"*80)
print("/" * 80)
print("Visit stack in Transformer:")

def sstr(s):
ss = str(type(s)) + " ; "
n = 160 - len(ss)
return ss + str(s)[:n]

print("\n".join(sstr(s) for s in self._visit_stack))
print("\\"*80)
print("\\" * 80)

def visit(self, o):
# Update stack
Expand Down Expand Up @@ -224,7 +230,8 @@ def variable(self, o):
def apply_transformer(e, transformer, integral_type=None):
"""Apply transformer.visit(expression) to each integrand
expression in form, or to form if it is an Expr."""
return map_integrands(lambda expr: transformer.visit(expr), e, integral_type)
return map_integrands(lambda expr: transformer.visit(expr), e,
integral_type)


def ufl2ufl(e):
Expand Down

0 comments on commit d0bc236

Please sign in to comment.