[PATCH] Creating more structure in examples/all.py to differentiate examples that need a plotting window.

0 views
Skip to first unread message

andy....@gmail.com

unread,
Dec 8, 2008, 4:18:03 PM12/8/08
to sympy-...@googlegroups.com, Andy R. Terrel
From: Andy R. Terrel <ate...@uchicago.edu>

---
examples/all.py | 143 +++++++++++++++++++++++++++++++++++++------------------
1 files changed, 97 insertions(+), 46 deletions(-)

diff --git a/examples/all.py b/examples/all.py
index 65988af..3a395ab 100755
--- a/examples/all.py
+++ b/examples/all.py
@@ -1,57 +1,78 @@
#!/usr/bin/env python
-"""Runner script
-
-Runs all the known working examples.
-
- Usage:
-
- When all examples run:
- $ ./all > out
- $
-
- When some examples fail:
- $ ./all > out
- Traceback (most recent call last):
- File "./limits_examples.py", line 17, in ?
- [...]
- $
-
- Obviously, we want to achieve the first result.
+"""all.py
+
+Runs all the examples for testing purposes and reports success and failure
+to stderr. An example is marked successful if the running thread does not
+throw an exception, for threaded examples, such as plotting, one needs to
+check the stderr messages as well.
+
+ $ ./all.py [-hw]
+
+Options:
+ -h print this help message and exit
+ -w Also run examples requiring windowed environment.
+
+Example Usage:
+ When no examples fail:
+ $ ./all.py > out
+ SUCCESSFUL:
+ - beginner.basic
+ [...]
+ NO FAILED EXAMPLES
+ $
+
+ When examples fail:
+ $ ./all.py -W > out
+ Traceback (most recent call last):
+ File "./all.py", line 111, in run_examples
+ [...]
+ SUCCESSFUL:
+ - beginner.basic
+ [...]
+ FAILED:
+ - intermediate.mplot2D
+ [...]
+ $
+
+ Obviously, we want to achieve the first result.
"""

import imp
import os
import sys
import traceback
+import getopt

-WORKING_EXAMPLES = [
+TERMINAL_EXAMPLES = [
"beginner.basic",
"beginner.differentiation",
"beginner.expansion",
"beginner.functions",
"beginner.limits_examples",
- #"beginner.plotting_nice_plot",
"beginner.precision",
"beginner.print_pretty",
"beginner.series",
"beginner.substitution",
"beginner.expansion",
"intermediate.differential_equations",
- #"intermediate.mplot2d",
- #"intermediate.mplot3d",
- #"intermediate.print_gtk",
"intermediate.trees",
"intermediate.vandermonde",
"advanced.fem",
"advanced.gibbs_phenomenon",
"advanced.pidigits",
- #"advanced.plotting",
"advanced.qft",
"advanced.relativity",
]

-example_dir = os.path.dirname(__file__)
-example_modules = []
+WINDOWED_EXAMPLES = [
+ "beginner.plotting_nice_plot",
+ #"intermediate.print_gtk",
+ "intermediate.mplot2d",
+ "intermediate.mplot3d",
+ "advanced.plotting",
+ ]
+
+EXAMPLE_DIR = os.path.dirname(__file__)

def __import__(name, globals=None, locals=None, fromlist=None):
"""An alternative to the import function so that we can import
@@ -68,7 +89,7 @@ def __import__(name, globals=None, locals=None, fromlist=None):
# If any of the following calls raises an exception,
# there's a problem we can't handle -- let the caller handle it.
module_name = name.split('.')[-1]
- module_path = os.path.join(example_dir, *name.split('.')[:-1])
+ module_path = os.path.join(EXAMPLE_DIR, *name.split('.')[:-1])

fp, pathname, description = imp.find_module(module_name, [module_path])

@@ -79,37 +100,67 @@ def __import__(name, globals=None, locals=None, fromlist=None):
if fp:
fp.close()

-def load_example_modules ():
- """Loads modules based upon the given package name"""
- global example_modules

- for entry in WORKING_EXAMPLES:
- mod = __import__(entry)
- example_modules.append(mod)
+def load_example_module(example):
+ """Loads modules based upon the given package name"""
+ mod = __import__(example)
+ return mod

-def setup_path ():
- """Put example directories in the path to load easily"""
- sys.path.insert(0,example_dir)

-def run_examples():
+def run_examples(windowed=False):
+ """Run example in list of modules"""
success = []
fail = []
- for mod in example_modules:
+ examples = TERMINAL_EXAMPLES
+ if windowed:
+ examples += WINDOWED_EXAMPLES
+ for example in examples:
print "="*79
- print "Running: ", mod.__name__
+ print "Running: ", example
try:
+ mod = load_example_module(example)
mod.main()
- success.append(mod.__name__)
+ success.append(example)
except:
traceback.print_exc()
- fail.append(mod.__name__)
- print "SUCCESS: ", success
- print "FAIL: ", fail
+ fail.append(example)
+ if success:
+ print >> sys.stderr, "SUCCESSFUL: "
+ for example in success:
+ print >> sys.stderr, " -", example
+ else:
+ print >> sys.stderr, "NO SUCCESSFUL EXAMPLES"
+ if fail:
+ print >> sys.stderr, "FAILED: "
+ for example in fail:
+ print >> sys.stderr, " -", example
+ else:
+ print >> sys.stderr, "NO FAILED EXAMPLES"
+

def main (*args, **kws):
- setup_path()
- load_example_modules()
- run_examples()
+ """Main script runner"""
+
+ use_windowed = False
+ try:
+ opts, remainder = getopt.getopt(args, "hw")
+ print opts
+ for opt_key, opt_val in opts:
+ print opt_key == '-w'
+ if opt_key == '-w':
+ use_windowed = True
+ elif opt_key == "-h":
+ print __doc__
+ sys.exit(0)
+ else:
+ raise getopt.GetoptError, "option %s not processed" % opt_key
+ except getopt.GetoptError, message:
+ print >> sys.stderr, message
+ print >> sys.stderr, "Use -h option for usage.\n"
+ sys.exit(1)
+
+ run_examples(use_windowed)
+

if __name__ == "__main__":
main(*sys.argv[1:])
--
1.6.0.3

andy....@gmail.com

unread,
Dec 8, 2008, 4:18:04 PM12/8/08
to sympy-...@googlegroups.com, Andy R. Terrel
From: Andy R. Terrel <ate...@uchicago.edu>

---
examples/all.py | 2 +-
examples/intermediate/print_gtk.py | 11 +++----
sympy/printing/mathml.py | 48 +++++++++++++++++++---------------
sympy/printing/tests/test_mathml.py | 18 ++++++++----
4 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/examples/all.py b/examples/all.py
index 3a395ab..0b6f9f1 100755
--- a/examples/all.py
+++ b/examples/all.py
@@ -66,7 +66,7 @@ TERMINAL_EXAMPLES = [

WINDOWED_EXAMPLES = [
"beginner.plotting_nice_plot",
- #"intermediate.print_gtk",
+ "intermediate.print_gtk",
"intermediate.mplot2d",
"intermediate.mplot3d",
"advanced.plotting",
diff --git a/examples/intermediate/print_gtk.py b/examples/intermediate/print_gtk.py
index aca5ff9..9d07fb0 100755
--- a/examples/intermediate/print_gtk.py
+++ b/examples/intermediate/print_gtk.py
@@ -4,17 +4,16 @@
Demonstrates printing with gtkmathview using mathml
"""

-from sympy import Symbol, integrate, exp
-from sympy.printing import print_gtk
+from sympy import Integral, Limit, print_gtk, sin, Symbol

def main():
x = Symbol('x')

- #l1 = limit(sin(x)/x, x, 0, evaluate=False)
- #print_gtk(l1)
+ example_limit = Limit(sin(x)/x, x, 0)
+ print_gtk(example_limit)

- l2 = integrate(exp(x), (x,0,1), evaluate=False)
- print_gtk(l2)
+ example_integral = Integral(x, (x, 0, 1))
+ print_gtk(example_integral)

if __name__ == "__main__":
main()
diff --git a/sympy/printing/mathml.py b/sympy/printing/mathml.py
index 1629786..ec6c88c 100644
--- a/sympy/printing/mathml.py
+++ b/sympy/printing/mathml.py
@@ -52,34 +52,40 @@ class MathMLPrinter(Printer):

x_1 = self.dom.createElement('bvar')
x_2 = self.dom.createElement('lowlimit')
- x_1.appendChild(self._print(e.x))
- x_2.appendChild(self._print(e.x0))
+ x_1.appendChild(self._print(e.args[1]))
+ x_2.appendChild(self._print(e.args[2]))

x.appendChild(x_1)
x.appendChild(x_2)
- x.appendChild(self._print(e.e))
+ x.appendChild(self._print(e.args[0]))

return x

- def _print_Integral(self, e):
- # FIXME doesn't work -- needs to be updated to the new Integral class
- x = self.dom.createElement('apply')
- x.appendChild(self.dom.createElement(self.mathml_tag(e)))
-
- x_1 = self.dom.createElement('bvar')
- x_2 = self.dom.createElement('lowlimit')
- x_3 = self.dom.createElement('uplimit')

- #x_1.appendChild(self._print(e.x))
- #x_2.appendChild(self._print(e.a))
- #x_3.appendChild(self._print(e.b))
-
- x.appendChild(x_1)
- x.appendChild(x_2)
- x.appendChild(x_3)
- x.appendChild(self._print(e.f))
-
- return x
+ def _print_Integral(self, e):
+ def lime_recur(limits):
+ x = self.dom.createElement('apply')
+ x.appendChild(self.dom.createElement(self.mathml_tag(e)))
+ bvar_elem = self.dom.createElement('bvar')
+ bvar_elem.appendChild(self._print(limits[0][0]))
+ x.appendChild(bvar_elem)
+
+ if limits[0][1]:
+ low_elem = self.dom.createElement('lowlimit')
+ low_elem.appendChild(self._print(limits[0][1][0]))
+ x.appendChild(low_elem)
+ up_elem = self.dom.createElement('uplimit')
+ up_elem.appendChild(self._print(limits[0][1][1]))
+ x.appendChild(up_elem)
+ if len(limits) == 1:
+ x.appendChild(self._print(e.function))
+ else:
+ x.appendChild(lime_recur(limits[1:]))
+ return x
+
+ limits = list(e.limits)
+ limits.reverse()
+ return lime_recur(limits)

def _print_Symbol(self, sym):
x = self.dom.createElement(self.mathml_tag(sym))
diff --git a/sympy/printing/tests/test_mathml.py b/sympy/printing/tests/test_mathml.py
index 7b5955a..6d7f55c 100644
--- a/sympy/printing/tests/test_mathml.py
+++ b/sympy/printing/tests/test_mathml.py
@@ -1,7 +1,6 @@
-from sympy import Symbol, sin, diff
+from sympy import diff, Integral, Limit, sin, Symbol
from sympy.printing.mathml import mathml, MathMLPrinter
from xml.dom.minidom import parseString
-from sympy.utilities.pytest import XFAIL

x = Symbol('x')
mp = MathMLPrinter()
@@ -50,16 +49,23 @@ def test_mathml_functions():
assert mml_2.childNodes[1].nodeName == 'bvar'
assert mml_2.childNodes[1].childNodes[0].nodeName == 'ci' # below bvar there's <ci>x/ci>

-@XFAIL
def test_mathml_limits():
# XXX No unevaluated limits
- mml_1 = mp._print(limit(sin(x)/x, x, 0, evaluate=False))
+ lim_fun = sin(x)/x
+ mml_1 = mp._print(Limit(lim_fun, x, 0))
assert mml_1.childNodes[0].nodeName == 'limit'
assert mml_1.childNodes[1].nodeName == 'bvar'
- assert mml_1.childNodes[1].childNodes[0].nodeName == 'ci'
+ assert mml_1.childNodes[2].nodeName == 'lowlimit'
+ assert mml_1.childNodes[3].toxml() == mp._print(lim_fun).toxml()

def test_mathml_integrals():
- pass #TODO
+ integrand = x
+ mml_1 = mp._print(Integral(integrand, (x, 0, 1)))
+ assert mml_1.childNodes[0].nodeName == 'int'
+ assert mml_1.childNodes[1].nodeName == 'bvar'
+ assert mml_1.childNodes[2].nodeName == 'lowlimit'
+ assert mml_1.childNodes[3].nodeName == 'uplimit'
+ assert mml_1.childNodes[4].toxml() == mp._print(integrand).toxml()

def test_mathml_matrices():
pass #TODO
--
1.6.0.3

Andy R. Terrel

unread,
Dec 8, 2008, 4:29:08 PM12/8/08
to sympy-patches
Actually it fixes issue 763. =D

-- Andy

On Dec 8, 3:18 pm, andy.ter...@gmail.com wrote:
> From: Andy R. Terrel <ater...@uchicago.edu>

Ondrej Certik

unread,
Dec 9, 2008, 7:12:51 AM12/9/08
to sympy-...@googlegroups.com
+1, thanks!

Ondrej Certik

unread,
Dec 9, 2008, 7:13:26 AM12/9/08
to sympy-...@googlegroups.com
+1, thanks!

On Mon, Dec 8, 2008 at 10:18 PM, <andy....@gmail.com> wrote:
>
Reply all
Reply to author
Forward
0 new messages