From 895697e4bd8e92951178b93abc4a2b8a4361288e Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 26 Mar 2013 16:31:01 +0000 Subject: [PATCH] __repr__ of DataArray instances --- src/MEDCoupling/MEDCouplingMemArray.cxx | 103 +++++++++++++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 5 + src/MEDCoupling/MEDCouplingMemArrayChar.cxx | 123 +++++++++++++++++- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 58 +++++++++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 31 ++++- 5 files changed, 318 insertions(+), 2 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 81cdfe77c..571cbcf2d 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -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;i1) + { + oss2 << "("; + for(int j=0;jsetName(\"" << 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;i1) + { + oss2 << "("; + for(int j=0;j(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;i1) + { + oss2 << "("; + for(int j=0;j(&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(&other); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 29a1816ae..098bdc63e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -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 diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index f1adae767..f080f8266 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -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(); -- 2.39.2