[MOAB-dev] r2974 - in MOAB/trunk/tools/iMesh/python: . doc

jvporter at wisc.edu jvporter at wisc.edu
Tue Jun 30 18:11:39 CDT 2009


Author: jvporter
Date: 2009-06-30 18:11:38 -0500 (Tue, 30 Jun 2009)
New Revision: 2974

Added:
   MOAB/trunk/tools/iMesh/python/numpy_extensions.h
   MOAB/trunk/tools/iMesh/python/numpy_extensions.inl
Modified:
   MOAB/trunk/tools/iMesh/python/doc/imesh.rst
   MOAB/trunk/tools/iMesh/python/iMesh.c
   MOAB/trunk/tools/iMesh/python/iMesh_Python.h
   MOAB/trunk/tools/iMesh/python/iMesh_entSet.inl
   MOAB/trunk/tools/iMesh/python/iMesh_iter.inl
   MOAB/trunk/tools/iMesh/python/iMesh_tag.inl
   MOAB/trunk/tools/iMesh/python/setup.py
Log:
Add automatic forwarding for Mesh.rootSet


Modified: MOAB/trunk/tools/iMesh/python/doc/imesh.rst
===================================================================
--- MOAB/trunk/tools/iMesh/python/doc/imesh.rst	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/doc/imesh.rst	2009-06-30 23:11:38 UTC (rev 2974)
@@ -39,22 +39,6 @@
       the relative cost of retrieving adjacencies between entities of dimension
       i to entities of dimension j.
 
-   .. method:: load(filename[, options])
-
-      Load a mesh from a file. Equivalent to ``rootSet.load(filename,
-      options)``.
-
-      :param filename: File name from which the mesh is to be loaded
-      :param options: Implementation-specific options string
-
-   .. method:: save(filename[, options])
-
-      Save the mesh to a file. Equivalent to ``rootSet.load(filename,
-      options)``.
-
-      :param filename: File name to which the mesh is to be saved
-      :param options: Implementation-specific options string
-
    .. method:: areEHValid(doReset)
 
       Return whether entity handles have changed since last reset or since
@@ -227,7 +211,18 @@
       :param entities: Entity or entity set being queried
       :return: Array of :class:`Tag`\ s associated with ``entities``
 
+Forwards
+--------
 
+In addition to the methods listed above, :class:`Mesh` automatically forwards
+method calls to the root :class:`EntitySet`. Thus, ::
+
+  mesh.getEntities(iBase.type.all, iMesh.topology.all)
+
+is equivalent to::
+
+  mesh.rootSet.getEntities(iBase.type.all, iMesh.topology.all)
+
 EntitySet
 =========
 
@@ -576,4 +571,4 @@
 
    .. data:: all
 
-      Allows the user to request information about all the topology types
\ No newline at end of file
+      Allows the user to request information about all the topology types

Modified: MOAB/trunk/tools/iMesh/python/iMesh.c
===================================================================
--- MOAB/trunk/tools/iMesh/python/iMesh.c	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/iMesh.c	2009-06-30 23:11:38 UTC (rev 2974)
@@ -2,7 +2,13 @@
 #include "iMesh_Python.h"
 #include "iBase_Python.h"
 
-int checkError(iMesh_Instance mesh,int err)
+/* TODO: these are never freed! */
+static PyObject *g_helper_module;
+static PyObject *g_adj_list;
+static PyObject *g_ind_adj_list;
+
+static int
+checkError(iMesh_Instance mesh,int err)
 {
     if(err)
     {
@@ -16,22 +22,8 @@
         return 0;
 }
 
-PyObject *
-PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth)
-{
-    PyObject *ret = PyArray_FromAny(obj,PyArray_DescrFromType(typenum),
-                                    min_depth,max_depth,NPY_C_CONTIGUOUS,NULL);
-    PyErr_Clear();
-    return ret;
-}
-
-/* TODO: these are never freed! */
-static PyObject *g_helper_module;
-static PyObject *g_adj_list;
-static PyObject *g_ind_adj_list;
-
 /* NOTE: steals references to adj and offsets */
-PyObject *
+static PyObject *
 AdjacencyList_New(PyObject *adj,PyObject *offsets)
 {
     PyObject *res;
@@ -46,7 +38,7 @@
 }
 
 /* NOTE: steals references to ents, adj, indices, and offsets */
-PyObject *
+static PyObject *
 IndexedAdjacencyList_New(PyObject *ents, PyObject *adj,PyObject *indices,
                          PyObject *offsets)
 {
@@ -64,7 +56,7 @@
     return res;
 }
 
-iMeshEntitySet_Object *
+static iMeshEntitySet_Object *
 iMeshEntitySet_New(iMesh_Object *mesh)
 {
     iMeshEntitySet_Object *o = iMeshEntitySet_NewRaw();
@@ -73,7 +65,7 @@
     return o;
 }
 
-iMeshTag_Object *
+static iMeshTag_Object *
 iMeshTag_New(iMesh_Object *mesh)
 {
     iMeshTag_Object *o = iMeshTag_NewRaw();
@@ -110,50 +102,6 @@
 }
 
 static PyObject *
-iMeshObj_load(iMesh_Object *self,PyObject *args)
-{
-    const char *name = 0;
-    const char *options = "";
-    iBase_EntitySetHandle root;
-    int err;
-
-    if(!PyArg_ParseTuple(args,"s|s",&name,&options))
-        return NULL;
-
-    iMesh_getRootSet(self->mesh,&root,&err);
-    if(checkError(self->mesh,err))
-        return NULL;
-
-    iMesh_load(self->mesh,root,name,options,&err,strlen(name),strlen(options));
-    if(checkError(self->mesh,err))
-        return NULL;
-
-    Py_RETURN_NONE;
-}
-
-static PyObject *
-iMeshObj_save(iMesh_Object *self,PyObject *args)
-{
-    const char *name = 0;
-    const char *options = "";
-    iBase_EntitySetHandle root;
-    int err;
-
-    if(!PyArg_ParseTuple(args,"s|s",&name,&options))
-        return NULL;
-
-    iMesh_getRootSet(self->mesh,&root,&err);
-    if(checkError(self->mesh,err))
-        return NULL;
-
-    iMesh_save(self->mesh,root,name,options,&err,strlen(name),strlen(options));
-    if(checkError(self->mesh,err))
-        return NULL;
-
-    Py_RETURN_NONE;
-}
-
-static PyObject *
 iMeshObj_getRootSet(iMesh_Object *self,void *closure)
 {
     iMeshEntitySet_Object *rootset = iMeshEntitySet_New(self);
@@ -931,12 +879,6 @@
 
 
 static PyMethodDef iMesh_methods[] = {
-    { "load", (PyCFunction)iMeshObj_load, METH_VARARGS,
-      "Load a mesh from a file"
-    },
-    { "save", (PyCFunction)iMeshObj_save, METH_VARARGS,
-      "Save the mesh to a file"
-    },
     { "areEHValid", (PyCFunction)iMeshObj_areEHValid, METH_VARARGS,
       "Return whether entity handles have changed since last reset or since "
       "instance construction"
@@ -1010,6 +952,25 @@
     {0}
 };
 
+static PyObject * iMeshObj_getAttr(PyObject *self,PyObject *attr_name)
+{
+    PyObject *ret;
+
+    ret = PyObject_GenericGetAttr(self,attr_name);
+    if(ret)
+        return ret;
+    else
+    {
+        PyErr_Clear();
+        PyObject *root = iMeshObj_getRootSet((iMesh_Object*)self,0);
+        if(!root)
+            return NULL;
+        ret = PyObject_GetAttr(root,attr_name);
+        Py_DECREF(root);
+        return ret;
+    }
+}
+
 static PyTypeObject iMesh_Type = {
     PyObject_HEAD_INIT(NULL)
     0,                            /* ob_size */
@@ -1028,7 +989,7 @@
     0,                            /* tp_hash */
     0,                            /* tp_call */
     0,                            /* tp_str */
-    0,                            /* tp_getattro */
+    iMeshObj_getAttr,             /* tp_getattro */
     0,                            /* tp_setattro */
     0,                            /* tp_as_buffer */
     Py_TPFLAGS_DEFAULT |
@@ -1091,57 +1052,6 @@
 ENUM_TYPE(iMeshTopology,"iMesh.Topology","");
 
 
-static void
-ArrDeallocObj_dealloc(ArrDealloc_Object *self)
-{
-    free(self->memory);
-    Py_XDECREF(self->base);
-
-    self->ob_type->tp_free((PyObject *)self);
-}
-
-static PyTypeObject ArrDealloc_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                          /* ob_size */
-    "arrdealloc",                               /* tp_name */
-    sizeof(ArrDealloc_Object),                  /* tp_basicsize */
-    0,                                          /* tp_itemsize */
-    (destructor)ArrDeallocObj_dealloc,          /* tp_dealloc */
-    0,                                          /* tp_print */
-    0,                                          /* tp_getattr */
-    0,                                          /* tp_setattr */
-    0,                                          /* tp_compare */
-    0,                                          /* tp_repr */
-    0,                                          /* tp_as_number */
-    0,                                          /* tp_as_sequence */
-    0,                                          /* tp_as_mapping */
-    0,                                          /* tp_hash */
-    0,                                          /* tp_call */
-    0,                                          /* tp_str */
-    0,                                          /* tp_getattro */
-    0,                                          /* tp_setattro */
-    0,                                          /* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
-    "Internal deallocator object",              /* tp_doc */
-};
-
-PyObject *
-PyArray_NewFromMallocBaseStrided(int nd,npy_intp *dims,npy_intp *strides,
-                                 int typenum,void *data,PyObject *base)
-{
-    ArrDealloc_Object *newobj;
-    PyObject *arr = PyArray_New(&PyArray_Type,nd,dims,typenum,strides,data,0,
-                                NPY_CARRAY,NULL);
-
-    newobj = PyObject_New(ArrDealloc_Object,&ArrDealloc_Type);
-    newobj->memory = data;
-    Py_XINCREF(base);
-    newobj->base = base;
-
-    PyArray_BASE(arr) = (PyObject*)newobj;
-    return arr;
-}
-
 PyMODINIT_FUNC initiMesh(void)
 {
     PyObject *m;
@@ -1229,3 +1139,4 @@
 #include "iMesh_entSet.inl"
 #include "iMesh_iter.inl"
 #include "iMesh_tag.inl"
+#include "numpy_extensions.inl"

Modified: MOAB/trunk/tools/iMesh/python/iMesh_Python.h
===================================================================
--- MOAB/trunk/tools/iMesh/python/iMesh_Python.h	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/iMesh_Python.h	2009-06-30 23:11:38 UTC (rev 2974)
@@ -10,33 +10,8 @@
 #include "iBase_Python.h"
 
 #define PY_ARRAY_UNIQUE_SYMBOL itaps_ARRAY_API
-#include <numpy/arrayobject.h>
+#include "numpy_extensions.h"
 
-typedef struct
-{
-    PyObject_HEAD
-    PyObject *base;
-    void *memory;
-} ArrDealloc_Object;
-
-
-#define PyArray_NewFromMalloc(nd,dims,typenum,data) \
-    PyArray_NewFromMallocBaseStrided(nd,dims,NULL,typenum,data,0)
-
-#define PyArray_NewFromMallocBase(nd,dims,typenum,data,base) \
-    PyArray_NewFromMallocBaseStrided(nd,dims,NULL,typenum,data,base)
-
-#define PyArray_NewFromMallocStrided(nd,dims,strides,typenum,data) \
-    PyArray_NewFromMallocBaseStrided(nd,dims,strides,typenum,data,0)
-
-static PyObject *
-PyArray_NewFromMallocBaseStrided(int nd,npy_intp *dims,npy_intp *strides,
-                                 int typenum,void *data,PyObject *base);
-
-static PyObject *
-PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth);
-
-
 static int checkError(iMesh_Instance mesh,int err);
 static enum iBase_TagValueType char_to_type(char c);
 static char type_to_char(enum iBase_TagValueType t);

Modified: MOAB/trunk/tools/iMesh/python/iMesh_entSet.inl
===================================================================
--- MOAB/trunk/tools/iMesh/python/iMesh_entSet.inl	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/iMesh_entSet.inl	2009-06-30 23:11:38 UTC (rev 2974)
@@ -1,6 +1,3 @@
-#define NO_IMPORT_ARRAY
-#define NO_IMPORT_IBASE
-
 #include "errors.h"
 #include "iMesh_Python.h"
 #include "iBase_Python.h"

Modified: MOAB/trunk/tools/iMesh/python/iMesh_iter.inl
===================================================================
--- MOAB/trunk/tools/iMesh/python/iMesh_iter.inl	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/iMesh_iter.inl	2009-06-30 23:11:38 UTC (rev 2974)
@@ -1,6 +1,3 @@
-#define NO_IMPORT_ARRAY
-#define NO_IMPORT_ITER
-
 #include "iMesh_Python.h"
 #include "iBase_Python.h"
 #include "structmember.h"

Modified: MOAB/trunk/tools/iMesh/python/iMesh_tag.inl
===================================================================
--- MOAB/trunk/tools/iMesh/python/iMesh_tag.inl	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/iMesh_tag.inl	2009-06-30 23:11:38 UTC (rev 2974)
@@ -1,6 +1,3 @@
-#define NO_IMPORT_ARRAY
-#define NO_IMPORT_IBASE
-
 #include "iMesh_Python.h"
 #include "iBase_Python.h"
 #include "errors.h"

Added: MOAB/trunk/tools/iMesh/python/numpy_extensions.h
===================================================================
--- MOAB/trunk/tools/iMesh/python/numpy_extensions.h	                        (rev 0)
+++ MOAB/trunk/tools/iMesh/python/numpy_extensions.h	2009-06-30 23:11:38 UTC (rev 2974)
@@ -0,0 +1,31 @@
+#ifndef PYTAPS_NUMPY_EXTENSIONS_H
+#define PYTAPS_NUMPY_EXTENSIONS_H
+
+#include <numpy/arrayobject.h>
+
+typedef struct
+{
+    PyObject_HEAD
+    PyObject *base;
+    void *memory;
+} ArrDealloc_Object;
+
+static PyTypeObject ArrDealloc_Type;
+
+#define PyArray_NewFromMalloc(nd,dims,typenum,data) \
+    PyArray_NewFromMallocBaseStrided(nd,dims,NULL,typenum,data,0)
+
+#define PyArray_NewFromMallocBase(nd,dims,typenum,data,base) \
+    PyArray_NewFromMallocBaseStrided(nd,dims,NULL,typenum,data,base)
+
+#define PyArray_NewFromMallocStrided(nd,dims,strides,typenum,data) \
+    PyArray_NewFromMallocBaseStrided(nd,dims,strides,typenum,data,0)
+
+static PyObject *
+PyArray_NewFromMallocBaseStrided(int nd,npy_intp *dims,npy_intp *strides,
+                                 int typenum,void *data,PyObject *base);
+
+static PyObject *
+PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth);
+
+#endif

Added: MOAB/trunk/tools/iMesh/python/numpy_extensions.inl
===================================================================
--- MOAB/trunk/tools/iMesh/python/numpy_extensions.inl	                        (rev 0)
+++ MOAB/trunk/tools/iMesh/python/numpy_extensions.inl	2009-06-30 23:11:38 UTC (rev 2974)
@@ -0,0 +1,59 @@
+static PyObject *
+PyArray_TryFromObject(PyObject *obj,int typenum,int min_depth,int max_depth)
+{
+    PyObject *ret = PyArray_FromAny(obj,PyArray_DescrFromType(typenum),
+                                    min_depth,max_depth,NPY_C_CONTIGUOUS,NULL);
+    PyErr_Clear();
+    return ret;
+}
+
+static void
+ArrDeallocObj_dealloc(ArrDealloc_Object *self)
+{
+    free(self->memory);
+    Py_XDECREF(self->base);
+
+    self->ob_type->tp_free((PyObject *)self);
+}
+
+static PyTypeObject ArrDealloc_Type = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                          /* ob_size */
+    "arrdealloc",                               /* tp_name */
+    sizeof(ArrDealloc_Object),                  /* tp_basicsize */
+    0,                                          /* tp_itemsize */
+    (destructor)ArrDeallocObj_dealloc,          /* tp_dealloc */
+    0,                                          /* tp_print */
+    0,                                          /* tp_getattr */
+    0,                                          /* tp_setattr */
+    0,                                          /* tp_compare */
+    0,                                          /* tp_repr */
+    0,                                          /* tp_as_number */
+    0,                                          /* tp_as_sequence */
+    0,                                          /* tp_as_mapping */
+    0,                                          /* tp_hash */
+    0,                                          /* tp_call */
+    0,                                          /* tp_str */
+    0,                                          /* tp_getattro */
+    0,                                          /* tp_setattro */
+    0,                                          /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
+    "Internal deallocator object",              /* tp_doc */
+};
+
+static PyObject *
+PyArray_NewFromMallocBaseStrided(int nd,npy_intp *dims,npy_intp *strides,
+                                 int typenum,void *data,PyObject *base)
+{
+    ArrDealloc_Object *newobj;
+    PyObject *arr = PyArray_New(&PyArray_Type,nd,dims,typenum,strides,data,0,
+                                NPY_CARRAY,NULL);
+
+    newobj = PyObject_New(ArrDealloc_Object,&ArrDealloc_Type);
+    newobj->memory = data;
+    Py_XINCREF(base);
+    newobj->base = base;
+
+    PyArray_BASE(arr) = (PyObject*)newobj;
+    return arr;
+}

Modified: MOAB/trunk/tools/iMesh/python/setup.py
===================================================================
--- MOAB/trunk/tools/iMesh/python/setup.py	2009-06-26 22:39:15 UTC (rev 2973)
+++ MOAB/trunk/tools/iMesh/python/setup.py	2009-06-30 23:11:38 UTC (rev 2974)
@@ -48,7 +48,8 @@
 iMesh = Extension('itaps.iMesh',
                   depends      = ['common.h', 'errors.h', 'iMesh_Python.h',
                                   'iBase_Python.h', 'iMesh_entSet.inl',
-                                  'iMesh_iter.inl', 'iMesh_tag.inl'],
+                                  'iMesh_iter.inl', 'iMesh_tag.inl',
+                                  'numpy_extensions.h', 'numpy_extensions.inl'],
                   sources      = ['iMesh.c']
                   )
 



More information about the moab-dev mailing list