]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
__repr__ of DataArray instances
authorageay <ageay>
Tue, 26 Mar 2013 16:31:01 +0000 (16:31 +0000)
committerageay <ageay>
Tue, 26 Mar 2013 16:31:01 +0000 (16:31 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling/MEDCouplingMemArrayChar.cxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingCommon.i

index 81cdfe77c22810a1fb0ba02968c7ee14783ae848..571cbcf2d567c2d055a28f8d5b592b158739a280 100644 (file)
@@ -1063,6 +1063,58 @@ void DataArrayDouble::reprCppStream(const char *varName, std::ostream& stream) c
   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
 }
 
+/*!
+ * Method that gives a quick overvien of \a this for python.
+ */
+void DataArrayDouble::reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
+{
+  static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
+  stream << "DataArrayDouble C++ instance at " << this << ". ";
+  if(isAllocated())
+    {
+      int nbOfCompo=(int)_info_on_compo.size();
+      if(nbOfCompo>=1)
+        {
+          int nbOfTuples=getNumberOfTuples();
+          stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
+          const double *data=begin();
+          std::ostringstream oss2; oss2 << "[";
+          oss2.precision(17);
+          std::string oss2Str(oss2.str());
+          bool isFinished=true;
+          for(int i=0;i<nbOfTuples && isFinished;i++)
+            {
+              if(nbOfCompo>1)
+                {
+                  oss2 << "(";
+                  for(int j=0;j<nbOfCompo;j++,data++)
+                    {
+                      oss2 << *data;
+                      if(j!=nbOfCompo-1) oss2 << ", ";
+                    }
+                  oss2 << ")";
+                }
+              else
+                oss2 << *data++;
+              if(i!=nbOfTuples-1) oss2 << ", ";
+              std::string oss3Str(oss2.str());
+              if(oss3Str.length()<MAX_NB_OF_BYTE_IN_REPR)
+                oss2Str=oss3Str;
+              else
+                isFinished=false;
+            }
+          stream << oss2Str;
+          if(!isFinished)
+            stream << "... ";
+          stream << "]";
+        }
+      else
+        stream << "Number of components : 0.";
+    }
+  else
+    stream << "*** No data allocated ****";
+}
+
 /*!
  * Equivalent to DataArrayDouble::isEqual except that if false the reason of
  * mismatch is given.
@@ -5524,6 +5576,57 @@ void DataArrayInt::reprCppStream(const char *varName, std::ostream& stream) cons
   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
 }
 
+/*!
+ * Method that gives a quick overvien of \a this for python.
+ */
+void DataArrayInt::reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
+{
+  static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
+  stream << "DataArrayInt C++ instance at " << this << ". ";
+  if(isAllocated())
+    {
+      int nbOfCompo=(int)_info_on_compo.size();
+      if(nbOfCompo>=1)
+        {
+          int nbOfTuples=getNumberOfTuples();
+          stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
+          const int *data=begin();
+          std::ostringstream oss2; oss2 << "[";
+          std::string oss2Str(oss2.str());
+          bool isFinished=true;
+          for(int i=0;i<nbOfTuples && isFinished;i++)
+            {
+              if(nbOfCompo>1)
+                {
+                  oss2 << "(";
+                  for(int j=0;j<nbOfCompo;j++,data++)
+                    {
+                      oss2 << *data;
+                      if(j!=nbOfCompo-1) oss2 << ", ";
+                    }
+                  oss2 << ")";
+                }
+              else
+                oss2 << *data++;
+              if(i!=nbOfTuples-1) oss2 << ", ";
+              std::string oss3Str(oss2.str());
+              if(oss3Str.length()<MAX_NB_OF_BYTE_IN_REPR)
+                oss2Str=oss3Str;
+              else
+                isFinished=false;
+            }
+          stream << oss2Str;
+          if(!isFinished)
+            stream << "... ";
+          stream << "]";
+        }
+      else
+        stream << "Number of components : 0.";
+    }
+  else
+    stream << "*** No data allocated ****";
+}
+
 /*!
  * Modifies \a this one-dimensional array so that each value \a v = \a indArrBg[ \a v ],
  * i.e. a current value is used as in index to get a new value from \a indArrBg.
index cfebca035b6805619958a35f8958ae7a9cd6e1ec..46108f22ed1d2cdc1381d6903ed17857b9d564c8 100644 (file)
@@ -138,6 +138,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT virtual void reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT virtual void reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception) = 0;
     MEDCOUPLING_EXPORT virtual void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception) = 0;
+    MEDCOUPLING_EXPORT virtual void reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception) = 0;
   protected:
     DataArray() { }
   protected:
@@ -195,6 +196,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT void reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool isEqual(const DataArrayDouble& other, double prec) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DataArrayDouble& other, double prec, std::string& reason) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool isEqualWithoutConsideringStr(const DataArrayDouble& other, double prec) const throw(INTERP_KERNEL::Exception);
@@ -416,6 +418,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT void reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void transformWithIndArr(const int *indArrBg, const int *indArrEnd) throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT DataArrayInt *transformWithIndArrR(const int *indArrBg, const int *indArrEnd) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void splitByValueRange(const int *arrBg, const int *arrEnd,
@@ -699,6 +702,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT void reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception);
   private:
     ~DataArrayByte() { }
@@ -754,6 +758,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT void reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception);
+    MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception);
   private:
     ~DataArrayAsciiChar() { }
index 7663146f9f5b233b48344fda2a64b453f307c286..6abb6bb56602604d6a0adb57d3cae4add7b760d8 100644 (file)
@@ -1847,7 +1847,18 @@ void DataArrayByte::reprZipStream(std::ostream& stream) const throw(INTERP_KERNE
 void DataArrayByte::reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
 {
   DataArray::reprWithoutNameStream(stream);
-  _mem.repr(getNumberOfComponents(),stream);
+  if(_mem.reprHeader(getNumberOfComponents(),stream))
+    {
+      const char *data=begin();
+      int nbOfTuples=getNumberOfTuples();
+      int nbCompo=getNumberOfComponents();
+      for(int i=0;i<nbOfTuples;i++,data+=nbCompo)
+        {
+          stream << "Tuple #" << i << " : ";
+          std::copy(data,data+nbCompo,std::ostream_iterator<int>(stream," "));//it is not a bug int here not char because it is not ASCII here contrary to DataArrayAsciiChar
+          stream << "\n";
+        }
+    }
 }
 
 void DataArrayByte::reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
@@ -1873,6 +1884,57 @@ void DataArrayByte::reprCppStream(const char *varName, std::ostream& stream) con
   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
 }
 
+/*!
+ * Method that gives a quick overvien of \a this for python.
+ */
+void DataArrayByte::reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
+{
+  static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
+  stream << "DataArrayByte C++ instance at " << this << ". ";
+  if(isAllocated())
+    {
+      int nbOfCompo=(int)_info_on_compo.size();
+      if(nbOfCompo>=1)
+        {
+          int nbOfTuples=getNumberOfTuples();
+          stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
+          const char *data=begin();
+          std::ostringstream oss2; oss2 << "[";
+          std::string oss2Str(oss2.str());
+          bool isFinished=true;
+          for(int i=0;i<nbOfTuples && isFinished;i++)
+            {
+              if(nbOfCompo>1)
+                {
+                  oss2 << "(";
+                  for(int j=0;j<nbOfCompo;j++,data++)
+                    {
+                      oss2 << (int)*data;
+                      if(j!=nbOfCompo-1) oss2 << ", ";
+                    }
+                  oss2 << ")";
+                }
+              else
+                { oss2 << (int)*data; data++; }
+              if(i!=nbOfTuples-1) oss2 << ", ";
+              std::string oss3Str(oss2.str());
+              if(oss3Str.length()<MAX_NB_OF_BYTE_IN_REPR)
+                oss2Str=oss3Str;
+              else
+                isFinished=false;
+            }
+          stream << oss2Str;
+          if(!isFinished)
+            stream << "... ";
+          stream << "]";
+        }
+      else
+        stream << "Number of components : 0.";
+    }
+  else
+    stream << "*** No data allocated ****";
+}
+
 bool DataArrayByte::isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception)
 {
   const DataArrayByte *otherC=dynamic_cast<const DataArrayByte *>(&other);
@@ -2147,6 +2209,65 @@ void DataArrayAsciiChar::reprCppStream(const char *varName, std::ostream& stream
   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
 }
 
+/*!
+ * Method that gives a quick overvien of \a this for python.
+ */
+void DataArrayAsciiChar::reprQuickOverview(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
+{
+  static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
+  stream << "DataArrayAsciiChar C++ instance at " << this << ". ";
+  if(isAllocated())
+    {
+      int nbOfCompo=(int)_info_on_compo.size();
+      if(nbOfCompo>=1)
+        {
+          int nbOfTuples=getNumberOfTuples();
+          stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
+          const char *data=begin();
+          std::ostringstream oss2; oss2 << "[";
+          std::string oss2Str(oss2.str());
+          bool isFinished=true;
+          for(int i=0;i<nbOfTuples && isFinished;i++)
+            {
+              bool isAscii=true;
+              for(int j=0;j<nbOfCompo;j++)
+                if(data[j]<32) isAscii=false;
+              if(isAscii)
+                {
+                  oss2 << "\'";
+                  for(int j=0;j<nbOfCompo;j++,data++)
+                    oss2 << *data;
+                  oss2 << "\'";
+                }
+              else
+                {
+                  oss2 << "(";
+                  for(int j=0;j<nbOfCompo;j++,data++)
+                    {
+                      oss2 << (int)*data;
+                      if(j!=nbOfCompo-1) oss2 << ", ";
+                    }
+                  oss2 << ")";
+                }
+              if(i!=nbOfTuples-1) oss2 << ", ";
+              std::string oss3Str(oss2.str());
+              if(oss3Str.length()<MAX_NB_OF_BYTE_IN_REPR)
+                oss2Str=oss3Str;
+              else
+                isFinished=false;
+            }
+          stream << oss2Str;
+          if(!isFinished)
+            stream << "... ";
+          stream << "]";
+        }
+      else
+        stream << "Number of components : 0.";
+    }
+  else
+    stream << "*** No data allocated ****";
+}
+
 bool DataArrayAsciiChar::isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception)
 {
   const DataArrayAsciiChar *otherC=dynamic_cast<const DataArrayAsciiChar *>(&other);
index 29a1816aee6e14d7072cf42ebf75354baa6239d5..098bdc63ed11947f2d0241edcf65bf6fe086b5c1 100644 (file)
@@ -11782,6 +11782,64 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertTrue(m4.getBarycenterAndOwner().isEqual(DataArrayDouble([0.3333333333333333,0.3333333333333333,0.,0.3333333333333333,0.3333333333333333,0.,0.5,0.,0.,0.5,0.5,0.,0.,0.5,0.,0.3333333333333333,0.3333333333333333,2.,0.5,0.,1.,0.5,0.5,1.,0.,0.5,1.,0.5,0.5,0.],10,3),1e-13))
         pass
 
+    def testSwigRepr1(self):
+        d=DataArrayDouble()
+        self.assertTrue(len(d.__repr__())<100)
+        d.alloc(1000,0) ; self.assertTrue(len(d.__repr__())<100)
+        for i in xrange(100):
+            d.alloc(i,1) ; d.iota(1.1234567890123456) ; d*=1e123
+            self.assertTrue(len(d.__repr__())<500)
+            pass
+        for i in xrange(50):
+            d.alloc(i,2) ; d.rearrange(1) ; d.iota(1.1234567890123456) ; d.rearrange(2) ; d*=1e123
+            self.assertTrue(len(d.__repr__())<500)
+            pass
+        d.alloc(4000,1) ; d.iota() ; self.assertTrue(len(d.__repr__())<500)
+        for i in xrange(2,4):
+            d.alloc(362880,1) ; d.iota() ; d.rearrange(i) ; self.assertTrue(len(d.__repr__())<500)
+            pass
+        d.alloc(0,9)
+        self.assertTrue(len(d.__repr__())<100)
+        #
+        d=DataArrayInt()
+        self.assertTrue(len(d.__repr__())<100)
+        d.alloc(1000,0) ; self.assertTrue(len(d.__repr__())<100)
+        for i in xrange(100):
+            d.alloc(i,1) ; d.iota(123456789)
+            self.assertTrue(len(d.__repr__())<500)
+            pass
+        for i in xrange(50):
+            d.alloc(i,2) ; d.rearrange(1) ; d.iota(123456789) ; d.rearrange(2)
+            self.assertTrue(len(d.__repr__())<500)
+            pass
+        d.alloc(4000,1) ; d.iota() ; self.assertTrue(len(d.__repr__())<500)
+        for i in xrange(2,10):
+            d.alloc(362880,1) ; d.iota() ; d.rearrange(i) ; self.assertTrue(len(d.__repr__())<500)
+            pass
+        d.alloc(0,9)
+        self.assertTrue(len(d.__repr__())<100)
+        #
+        d=DataArrayAsciiChar()
+        d.alloc(1000,0) ; self.assertTrue(len(d.__repr__())<100)
+        d.alloc(2,16) ; d[:]='1234567890ABCDEF'
+        self.assertTrue(len(d.__repr__())<500)
+        d.alloc(2000,16) ; d[:]='1234567890ABCDEF'
+        self.assertTrue(len(d.__repr__())<500)
+        d.alloc(0,16) ; d[:]='1234567890ABCDEF'
+        self.assertTrue(len(d.__repr__())<100)
+        #
+        d=DataArrayByte()
+        self.assertTrue(len(d.__repr__())<100)
+        d.alloc(1000,0) ; self.assertTrue(len(d.__repr__())<100)
+        d.alloc(0,16) ; self.assertTrue(len(d.__repr__())<100)
+        d.alloc(5,1) ; d.fillWithValue(127)
+        self.assertTrue(len(d.__repr__())<200)
+        d.alloc(1000,1) ; d.fillWithValue(127)
+        self.assertTrue(len(d.__repr__())<500)
+        d.alloc(1000,3) ; d.fillWithValue(127)
+        self.assertTrue(len(d.__repr__())<500)
+        pass
+
     def setUp(self):
         pass
     pass
index f1adae767b96cdb17d92ce778d5bc7b6279b1055..f080f8266416e5109ff109400f6c3df6c8d8645c 100644 (file)
@@ -970,6 +970,14 @@ namespace ParaMEDMEM
 
 %extend ParaMEDMEM::DataArrayInt
 {
+  
+  std::string __repr__() const throw(INTERP_KERNEL::Exception)
+  {
+    std::ostringstream oss;
+    self->reprQuickOverview(oss);
+    return oss.str();
+  }
+  
   void pushBackValsSilent(PyObject *li) throw(INTERP_KERNEL::Exception)
   {
     int szArr,sw,iTypppArr;
@@ -1009,7 +1017,7 @@ namespace ParaMEDMEM
 
 %extend ParaMEDMEM::DataArrayChar
 {
-   int __len__() const throw(INTERP_KERNEL::Exception)
+  int __len__() const throw(INTERP_KERNEL::Exception)
    {
      if(self->isAllocated())
        {
@@ -1348,6 +1356,13 @@ namespace ParaMEDMEM
      return ParaMEDMEM_DataArrayByte_New__SWIG_1(elt0,nbOfTuples,nbOfComp);
    }
    
+   std::string __repr__() const throw(INTERP_KERNEL::Exception)
+   {
+     std::ostringstream oss;
+     self->reprQuickOverview(oss);
+     return oss.str();
+   }
+  
    int __int__() const throw(INTERP_KERNEL::Exception)
    {
      return (int) self->byteValue();
@@ -1646,6 +1661,13 @@ namespace ParaMEDMEM
      return ParaMEDMEM_DataArrayAsciiChar_New__SWIG_3(elt0,nbOfTuples,nbOfComp);
    }
 
+   std::string __repr__() const throw(INTERP_KERNEL::Exception)
+   {
+     std::ostringstream oss;
+     self->reprQuickOverview(oss);
+     return oss.str();
+   }
+
    DataArrayAsciiCharIterator *__iter__() throw(INTERP_KERNEL::Exception)
    {
      return self->iterator();
@@ -4098,6 +4120,13 @@ namespace ParaMEDMEM
      self->pushBackValsSilent(tmp,tmp+nbTuples);
    }
 
+   std::string __repr__() const throw(INTERP_KERNEL::Exception)
+   {
+     std::ostringstream oss;
+     self->reprQuickOverview(oss);
+     return oss.str();
+   }
+
    std::string __str__() const throw(INTERP_KERNEL::Exception)
    {
      return self->repr();