Commit 15896398 authored by Stephan Rave's avatar Stephan Rave

[tools] add print_source and source_repr

parent 817951ee
......@@ -57,13 +57,8 @@ class rule:
condition_type = None
def __repr__(self):
try:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter
return highlight(self.source, PythonLexer(), Terminal256Formatter())
except ImportError:
return self.source
from pymor.tools.formatsrc import source_repr
return source_repr(self.action)
@property
def action_description(self):
......
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
from inspect import getsource
from pymor.core.config import is_jupyter
def format_source(obj):
source = getsource(obj)
if is_jupyter():
from IPython.display import display, Code
return Code(source, language='python')
else:
try:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter
return highlight(self.source, PythonLexer(), Terminal256Formatter())
except ImportError:
return source
def print_source(obj):
source = format_source(obj)
if is_jupyter():
display(source)
else:
print(source)
def source_repr(obj):
source = format_source(obj)
if is_jupyter():
display(source)
return ''
else:
return source
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment