From: W. Trevor King Date: Mon, 18 Oct 2010 21:18:21 +0000 (-0400) Subject: Add pysawsim/__init__.py and break invoke out into its own module. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ba587aff487e85e9fb21a4128c0e38ea6baa6335;p=sawsim.git Add pysawsim/__init__.py and break invoke out into its own module. --- diff --git a/pysawsim/__init__.py b/pysawsim/__init__.py new file mode 100644 index 0000000..cc179d9 --- /dev/null +++ b/pysawsim/__init__.py @@ -0,0 +1,46 @@ +# Copyright (C) 2010 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# The author may be contacted at on the Internet, or +# write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St., +# Philadelphia PA 19104, USA. + +"""Wrap `sawsim` to facilitate more complicated analysis. + +Testing pysawsim +================ + +pysawsim's test framework is build using doctest_, unittest_, and nose_. +`nosetests` (from the nose_ package) scans through the source tree, +searching out the various tests and running them. If you aren't +familiar with nose_, there is excellent documentation on its home +page. We use nose_ because its auto-discovery allows us to avoid +collecting all of our assorted tests into +:class:`unittest.TestSuite`\s and running them by hand. + +To run the test suite from the sawsim installation directory, just use:: + + nosetests --with-doctest --doctest-tests pysawsim/ + +.. _doctest: http://docs.python.org/library/doctest.html +.. _unittest: http://docs.python.org/library/unittest.html +.. _nose: http://somethingaboutorange.com/mrl/projects/nose/0.11.3/ + + +Adding tests to modules +----------------------- + +Just go crazy with doctests and unittests; nose_ will find them. +""" diff --git a/pysawsim/fit_force_histograms.py b/pysawsim/fit_force_histograms.py index 09e645c..2b64926 100755 --- a/pysawsim/fit_force_histograms.py +++ b/pysawsim/fit_force_histograms.py @@ -1,4 +1,22 @@ #!/usr/bin/python +# Copyright (C) 2009-2010 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# The author may be contacted at on the Internet, or +# write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St., +# Philadelphia PA 19104, USA. import os # HACK, for getpid() import os.path @@ -6,7 +24,6 @@ import pickle import numpy from scipy.optimize import leastsq import shutil -from subprocess import Popen, PIPE import sys import matplotlib @@ -18,42 +35,6 @@ FIGURE = pylab.figure() # avoid memory problems. # single instance. MEM_DEBUG = False -class CommandError(Exception): - def __init__(self, command, status, stdout, stderr): - strerror = ["Command failed (%d):\n %s\n" % (status, stderr), - "while executing\n %s" % command] - Exception.__init__(self, "\n".join(strerror)) - self.command = command - self.status = status - self.stdout = stdout - self.stderr = stderr - -def invoke(cmd_string, stdin=None, expect=(0,), cwd=None, verbose=True): - """ - expect should be a tuple of allowed exit codes. cwd should be - the directory from which the command will be executed. - """ - if cwd == None: - cwd = "." - if verbose == True: - print >> sys.stderr, "%s$ %s" % (cwd, cmd_string) - try : - if sys.platform != "win32" and False: - q = Popen(cmd_string, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd) - else: - # win32 don't have os.execvp() so have to run command in a shell - q = Popen(cmd_string, stdin=PIPE, stdout=PIPE, stderr=PIPE, - shell=True, cwd=cwd) - except OSError, e : - raise CommandError(args, status=e.args[0], stdout="", stderr=e) - output,error = q.communicate(input=stdin) - status = q.wait() - if verbose == True: - print >> sys.stderr, "%d\n%s%s" % (status, output, error) - if status not in expect: - raise CommandError(args, status, output, error) - return status, output, error - def rss(): """ For debugging memory usage. @@ -340,7 +321,7 @@ def parse_param_ranges_string(string): ranges.append([float(x) for x in range_number_strings]) return ranges -if __name__ == "__main__": +def main(): import optparse usage = "%prog [options] velocity_file" @@ -392,3 +373,6 @@ if __name__ == "__main__": plot_residuals=options.plot_residuals, contour=options.contour_plot, run_repeat_simulations=options.simulations) + +if __name__ == "__main__": + main() diff --git a/pysawsim/invoke.py b/pysawsim/invoke.py new file mode 100644 index 0000000..d7c10b5 --- /dev/null +++ b/pysawsim/invoke.py @@ -0,0 +1,61 @@ +# Copyright (C) 2009-2010 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# The author may be contacted at on the Internet, or +# write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St., +# Philadelphia PA 19104, USA. + +"""Functions for running external commands in subprocesses. +""" + +from subprocess import Popen, PIPE +import sys + + +class CommandError(Exception): + def __init__(self, command, status, stdout, stderr): + strerror = ["Command failed (%d):\n %s\n" % (status, stderr), + "while executing\n %s" % command] + Exception.__init__(self, "\n".join(strerror)) + self.command = command + self.status = status + self.stdout = stdout + self.stderr = stderr + +def invoke(cmd_string, stdin=None, expect=(0,), cwd=None, verbose=True): + """ + expect should be a tuple of allowed exit codes. cwd should be + the directory from which the command will be executed. + """ + if cwd == None: + cwd = "." + if verbose == True: + print >> sys.stderr, "%s$ %s" % (cwd, cmd_string) + try : + if sys.platform != "win32" and False: + q = Popen(cmd_string, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd) + else: + # win32 don't have os.execvp() so have to run command in a shell + q = Popen(cmd_string, stdin=PIPE, stdout=PIPE, stderr=PIPE, + shell=True, cwd=cwd) + except OSError, e : + raise CommandError(args, status=e.args[0], stdout="", stderr=e) + output,error = q.communicate(input=stdin) + status = q.wait() + if verbose == True: + print >> sys.stderr, "%d\n%s%s" % (status, output, error) + if status not in expect: + raise CommandError(args, status, output, error) + return status, output, error