[cig-commits] [commit] master: Add get/set item Python accessors (269aee9)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Wed Oct 8 17:06:41 PDT 2014


Repository : https://github.com/geodynamics/vc

On branch  : master
Link       : https://github.com/geodynamics/vc/compare/23464fca3efa2b6ad7ee0ce8f60c225b18b49741...e4325192ad1118379f46ba66899cb98143d09e04

>---------------------------------------------------------------

commit 269aee9b561e92ae377e8c35c71280bca36c1733
Author: Eric Heien <emheien at ucdavis.edu>
Date:   Wed Oct 8 00:40:19 2014 -0700

    Add get/set item Python accessors
    
    Includes exceptions and unit tests for exceptions, and str/repr
    functions for Python


>---------------------------------------------------------------

269aee9b561e92ae377e8c35c71280bca36c1733
 quakelib/python/quakelib.i    | 73 +++++++++++++++++++++++++++++++++----------
 quakelib/test/UtilUnitTest.py | 13 ++++++++
 2 files changed, 69 insertions(+), 17 deletions(-)

diff --git a/quakelib/python/quakelib.i b/quakelib/python/quakelib.i
index 3ae200d..2ffa0ad 100644
--- a/quakelib/python/quakelib.i
+++ b/quakelib/python/quakelib.i
@@ -2,6 +2,7 @@
 %include "std_string.i"
 %include "std_vector.i"
 %include "std_map.i"
+%include "exception.i"
 %{
 #include "QuakeLib.h"
 #include "QuakeLibIO.h"
@@ -27,35 +28,69 @@ using namespace quakelib;
 %include "QuakeLibOkada.h"
 %include "QuakeLibEQSim.h"
 
+%exception {
+    try {
+        $action
+    } catch (std::out_of_range &e) {
+        PyErr_SetString(PyExc_IndexError, const_cast<char*>(e.what()));
+        SWIG_fail;
+    }
+}
+
 // Create aliases for 2D and 3D vector templates
 %template(Vec2) quakelib::Vec<2>;
 %template(Vec3) quakelib::Vec<3>;
 %extend quakelib::Vec<2> {
-	double __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, double new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+	double __getitem__(unsigned int i) { return (*$self)[i]; };
+	void __setitem__(unsigned int i, double new_val) { (*$self)[i] = new_val; };
+    unsigned int __len__(void) { return 2; };
 };
 
 %extend quakelib::Vec<3> {
-	double __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, double new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+	double __getitem__(unsigned int i) { return (*$self)[i]; };
+	void __setitem__(unsigned int i, double new_val) { (*$self)[i] = new_val; };
+    unsigned int __len__(void) { return 3; };
 };
 
 %template(TensorRow3) quakelib::TensorRow<3>;
 %template(Tensor33) quakelib::Tensor<3,3>;
 
-%extend quakelib::TensorRow<3> {
-	double __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, double new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+%extend quakelib::ModelEvent {
+	char *__str__(void) {
+		static char			tmp[1024];
+		std::stringstream	ss;
+		std::string			res;
+		ss << (*$self);
+		res = ss.str();
+		res.resize(1023);
+		sprintf(tmp, "%s", res.c_str());
+		return tmp;
+	}
 };
 
-%extend quakelib::Tensor<3,3> {
-	TensorRow<3> __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, TensorRow<3> new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+%extend quakelib::ModelEventSet {
+    ModelEvent __getitem__(unsigned int i) { return (*$self)[i]; };
+	void __setitem__(unsigned int i, ModelEvent new_val) { (*$self)[i] = new_val; };
+    unsigned int __len__(void) { return $self->size(); };
 };
 
-%extend quakelib::ModelEventSet {
-    ModelEvent __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, ModelEvent new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+%extend quakelib::ModelSweeps {
+    SweepData __getitem__(unsigned int i) { return (*$self)[i]; };
+	void __setitem__(unsigned int i, SweepData new_val) { (*$self)[i] = new_val; };
+    unsigned int __len__(void) { return $self->size(); };
+};
+
+%extend quakelib::SweepData {
+	char *__str__(void) {
+		static char tmp[1024];
+		sprintf(tmp, "quakelib.SweepData(%d,%d,%g)", $self->_sweep_number, $self->_element_id, $self->_slip);
+		return tmp;
+	}
+	char *__repr__(void) {
+		static char tmp[1024];
+		sprintf(tmp, "quakelib.SweepData(%d,%d,%g)", $self->_sweep_number, $self->_element_id, $self->_slip);
+		return tmp;
+	}
 };
 
 %template(RectBound2) quakelib::RectBound<2>;
@@ -179,8 +214,9 @@ using namespace quakelib;
 		return tmp;
 	}
 
-    TensorRow<3> __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, TensorRow<3> new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+    TensorRow<3> __getitem__(unsigned int i) { return (*$self)[i]; };
+	void __setitem__(unsigned int i, TensorRow<3> new_val) { (*$self)[i] = new_val; };
+    unsigned int __len__(void) { return 3; };
 };
 
 %extend quakelib::TensorRow<3> {
@@ -201,8 +237,9 @@ using namespace quakelib;
 		return tmp;
 	}
 
-    double __getitem__(unsigned int i) throw(std::out_of_range) { return (*$self)[i]; };
-	void __setitem__(unsigned int i, double new_val) throw(std::out_of_range) { (*$self)[i] = new_val; };
+    double __getitem__(unsigned int i) { return (*$self)[i]; };
+	void __setitem__(unsigned int i, double new_val) { (*$self)[i] = new_val; };
+    unsigned int __len__(void) { return 3; };
 };
 
 %extend quakelib::RectBound<2> {
@@ -259,6 +296,8 @@ using namespace quakelib;
 	}
 };
 
+%exception;
+
 // Map a Python sequence into any sized C double array
 %typemap(in) double[ANY](double temp[$1_dim0]) {
   int i;
diff --git a/quakelib/test/UtilUnitTest.py b/quakelib/test/UtilUnitTest.py
index 20d6cfe..625ab79 100755
--- a/quakelib/test/UtilUnitTest.py
+++ b/quakelib/test/UtilUnitTest.py
@@ -82,6 +82,19 @@ class TestVectors(unittest.TestCase):
         self.assertEqual(x, xp)
         self.assertEqual(y, yp)
 
+    # Check that get/set indices throw appropriate exceptions
+    def test_index_error(self):
+        x = quakelib.Vec3(1, 2, 3)
+        y = quakelib.Vec2(1, 2)
+        with self.assertRaises(OverflowError): x[-1]
+        with self.assertRaises(OverflowError): x[-1] = 0
+        with self.assertRaises(IndexError): x[3]
+        with self.assertRaises(IndexError): x[3] = 0
+        with self.assertRaises(OverflowError): y[-1]
+        with self.assertRaises(OverflowError): y[-1] = 0
+        with self.assertRaises(IndexError): y[2]
+        with self.assertRaises(IndexError): y[2] = 0
+
 class TestLatLonDepth(unittest.TestCase):
     # Ensure that out-of-bounds assignment and equality work correctly
     def test_assign(self):



More information about the CIG-COMMITS mailing list