From: Anthony Geay Date: Mon, 23 Mar 2015 08:19:36 +0000 (+0100) Subject: DataArrayDouble.__str__ and DataArrayInt.__str__ no more print all the tuples when... X-Git-Tag: V7_6_0a1~6 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=b0fe79ff08d1396a0da5389f61fec7f73d2f9fe0;p=modules%2Fmed.git DataArrayDouble.__str__ and DataArrayInt.__str__ no more print all the tuples when there are more than 1000 tuples. --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 260464e2f..0a296ebd1 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -1146,7 +1146,9 @@ bool DataArrayDouble::isMonotonic(bool increasing, double eps) const /*! * Returns a textual and human readable representation of \a this instance of * DataArrayDouble. This text is shown when a DataArrayDouble is printed in Python. - * \return std::string - text describing \a this DataArrayDouble. + * \return std::string - text describing \a this DataArrayDouble. + * + * \sa reprNotTooLong, reprZip */ std::string DataArrayDouble::repr() const { @@ -1162,6 +1164,18 @@ std::string DataArrayDouble::reprZip() const return ret.str(); } +/*! + * This method is close to repr method except that when \a this has more than 1000 tuples, all tuples are not + * printed out to avoid to consume too much space in interpretor. + * \sa repr + */ +std::string DataArrayDouble::reprNotTooLong() const +{ + std::ostringstream ret; + reprNotTooLongStream(ret); + return ret.str(); +} + void DataArrayDouble::writeVTK(std::ostream& ofs, int indent, const std::string& nameInFile, DataArrayByte *byteArr) const { static const char SPACE[4]={' ',' ',' ',' '}; @@ -1211,6 +1225,12 @@ void DataArrayDouble::reprZipStream(std::ostream& stream) const reprZipWithoutNameStream(stream); } +void DataArrayDouble::reprNotTooLongStream(std::ostream& stream) const +{ + stream << "Name of double array : \"" << _name << "\"\n"; + reprNotTooLongWithoutNameStream(stream); +} + void DataArrayDouble::reprWithoutNameStream(std::ostream& stream) const { DataArray::reprWithoutNameStream(stream); @@ -1225,6 +1245,13 @@ void DataArrayDouble::reprZipWithoutNameStream(std::ostream& stream) const _mem.reprZip(getNumberOfComponents(),stream); } +void DataArrayDouble::reprNotTooLongWithoutNameStream(std::ostream& stream) const +{ + DataArray::reprWithoutNameStream(stream); + stream.precision(17); + _mem.reprNotTooLong(getNumberOfComponents(),stream); +} + void DataArrayDouble::reprCppStream(const std::string& varName, std::ostream& stream) const { int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents(); @@ -6188,7 +6215,9 @@ void DataArrayInt::iota(int init) /*! * Returns a textual and human readable representation of \a this instance of * DataArrayInt. This text is shown when a DataArrayInt is printed in Python. - * \return std::string - text describing \a this DataArrayInt. + * \return std::string - text describing \a this DataArrayInt. + * + * \sa reprNotTooLong, reprZip */ std::string DataArrayInt::repr() const { @@ -6204,6 +6233,18 @@ std::string DataArrayInt::reprZip() const return ret.str(); } +/*! + * This method is close to repr method except that when \a this has more than 1000 tuples, all tuples are not + * printed out to avoid to consume too much space in interpretor. + * \sa repr + */ +std::string DataArrayInt::reprNotTooLong() const +{ + std::ostringstream ret; + reprNotTooLongStream(ret); + return ret.str(); +} + void DataArrayInt::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const { static const char SPACE[4]={' ',' ',' ',' '}; @@ -6257,6 +6298,12 @@ void DataArrayInt::reprZipStream(std::ostream& stream) const reprZipWithoutNameStream(stream); } +void DataArrayInt::reprNotTooLongStream(std::ostream& stream) const +{ + stream << "Name of int array : \"" << _name << "\"\n"; + reprNotTooLongWithoutNameStream(stream); +} + void DataArrayInt::reprWithoutNameStream(std::ostream& stream) const { DataArray::reprWithoutNameStream(stream); @@ -6269,6 +6316,13 @@ void DataArrayInt::reprZipWithoutNameStream(std::ostream& stream) const _mem.reprZip(getNumberOfComponents(),stream); } +void DataArrayInt::reprNotTooLongWithoutNameStream(std::ostream& stream) const +{ + DataArray::reprWithoutNameStream(stream); + stream.precision(17); + _mem.reprNotTooLong(getNumberOfComponents(),stream); +} + void DataArrayInt::reprCppStream(const std::string& varName, std::ostream& stream) const { int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents(); diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 7c5d98a20..3cfdf0069 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -71,6 +71,7 @@ namespace ParaMEDMEM void repr(int sl, std::ostream& stream) const; bool reprHeader(int sl, std::ostream& stream) const; void reprZip(int sl, std::ostream& stream) const; + void reprNotTooLong(int sl, std::ostream& stream) const; void fillWithValue(const T& val); T *fromNoInterlace(int nbOfComp) const; T *toNoInterlace(int nbOfComp) const; @@ -228,11 +229,14 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT bool isMonotonic(bool increasing, double eps) const; MEDCOUPLING_EXPORT std::string repr() const; MEDCOUPLING_EXPORT std::string reprZip() const; + MEDCOUPLING_EXPORT std::string reprNotTooLong() const; MEDCOUPLING_EXPORT void writeVTK(std::ostream& ofs, int indent, const std::string& nameInFile, DataArrayByte *byteArr) const; MEDCOUPLING_EXPORT void reprStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprZipStream(std::ostream& stream) const; + MEDCOUPLING_EXPORT void reprNotTooLongStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprWithoutNameStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprZipWithoutNameStream(std::ostream& stream) const; + MEDCOUPLING_EXPORT void reprNotTooLongWithoutNameStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprCppStream(const std::string& varName, std::ostream& stream) const; MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const; @@ -467,11 +471,14 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void iota(int init=0); MEDCOUPLING_EXPORT std::string repr() const; MEDCOUPLING_EXPORT std::string reprZip() const; + MEDCOUPLING_EXPORT std::string reprNotTooLong() const; MEDCOUPLING_EXPORT void writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const; MEDCOUPLING_EXPORT void reprStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprZipStream(std::ostream& stream) const; + MEDCOUPLING_EXPORT void reprNotTooLongStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprWithoutNameStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprZipWithoutNameStream(std::ostream& stream) const; + MEDCOUPLING_EXPORT void reprNotTooLongWithoutNameStream(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprCppStream(const std::string& varName, std::ostream& stream) const; MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const; diff --git a/src/MEDCoupling/MEDCouplingMemArray.txx b/src/MEDCoupling/MEDCouplingMemArray.txx index 503a63b8d..693e6e4f0 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.txx +++ b/src/MEDCoupling/MEDCouplingMemArray.txx @@ -258,6 +258,50 @@ namespace ParaMEDMEM stream << "No data !\n"; } + /*! + * \param [in] sl is typically the number of components + */ + template + void MemArray::reprNotTooLong(int sl, std::ostream& stream) const + { + if(reprHeader(sl,stream)) + { + const T *data=getConstPointer(); + if(_nb_of_elem!=0 && sl!=0) + { + std::size_t nbOfTuples=_nb_of_elem/std::abs(sl); + if(nbOfTuples<=1000) + { + for(std::size_t i=0;i(stream," ")); + stream << "\n"; + data+=sl; + } + } + else + {// too much tuples -> print the 3 first tuples and 3 last. + stream << "Tuple #0 : "; + std::copy(data,data+sl,std::ostream_iterator(stream," ")); stream << "\n"; + stream << "Tuple #1 : "; + std::copy(data+sl,data+2*sl,std::ostream_iterator(stream," ")); stream << "\n"; + stream << "Tuple #2 : "; + std::copy(data+2*sl,data+3*sl,std::ostream_iterator(stream," ")); stream << "\n"; + stream << "...\n"; + stream << "Tuple #" << nbOfTuples-3 << " : "; + std::copy(data+(nbOfTuples-3)*sl,data+(nbOfTuples-2)*sl,std::ostream_iterator(stream," ")); stream << "\n"; + stream << "Tuple #" << nbOfTuples-2 << " : "; + std::copy(data+(nbOfTuples-2)*sl,data+(nbOfTuples-1)*sl,std::ostream_iterator(stream," ")); stream << "\n"; + stream << "Tuple #" << nbOfTuples-1 << " : "; + std::copy(data+(nbOfTuples-1)*sl,data+nbOfTuples*sl,std::ostream_iterator(stream," ")); stream << "\n"; + } + } + else + stream << "Empty Data\n"; + } + } + template void MemArray::fillWithValue(const T& val) { diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 83a159cf3..ace68a110 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -16365,6 +16365,31 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertRaises(InterpKernelException,m.getLocationFromCellId,5) self.assertRaises(InterpKernelException,m.getLocationFromCellId,-1) pass + + def testSwig2DataArrayPrintNotTooLong1(self): + """ Now that DataArrayDouble and DataArrayInt and pickelized they can appear in YACS ports. Avoid to have too heavy string representation of them.""" + d=DataArrayDouble(2000) ; d.iota() ; d.rearrange(2) + st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() + self.assertEqual(st0,st1) # 1000 tuples ( >=0 and <= 1000) -> str(d)==d.repr() + self.assertEqual(st1,st2) + # + d=DataArrayDouble(2002) ; d.iota() ; d.rearrange(2) + st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() + self.assertNotEqual(st0,st1) # 1001 tuples ( > 1000) -> str(d)==d.reprNotTooLong() + self.assertEqual(st1,st2) + self.assertIn(len(st2),xrange(0,1000)) # no more than 1000 characters + ## Now for DataArrayInt + d=DataArrayInt(2000) ; d.iota() ; d.rearrange(2) + st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() + self.assertEqual(st0,st1) # 1000 tuples ( >=0 and <= 1000) -> str(d)==d.repr() + self.assertEqual(st1,st2) + # + d=DataArrayInt(2002) ; d.iota() ; d.rearrange(2) + st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() + self.assertNotEqual(st0,st1) # 1001 tuples ( > 1000) -> str(d)==d.reprNotTooLong() + self.assertEqual(st1,st2) + self.assertIn(len(st2),xrange(0,1000)) # no more than 1000 characters + pass pass if __name__ == '__main__': diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index cbf759e94..29d4aa914 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -540,6 +540,7 @@ namespace ParaMEDMEM bool isMonotonic(bool increasing, double eps) const throw(INTERP_KERNEL::Exception); std::string repr() const throw(INTERP_KERNEL::Exception); std::string reprZip() const throw(INTERP_KERNEL::Exception); + std::string reprNotTooLong() const throw(INTERP_KERNEL::Exception); bool isEqual(const DataArrayDouble& other, double prec) const throw(INTERP_KERNEL::Exception); bool isEqualWithoutConsideringStr(const DataArrayDouble& other, double prec) const throw(INTERP_KERNEL::Exception); DataArrayInt *convertToIntArr() const throw(INTERP_KERNEL::Exception); @@ -752,7 +753,7 @@ namespace ParaMEDMEM std::string __str__() const throw(INTERP_KERNEL::Exception) { - return self->repr(); + return self->reprNotTooLong(); } double __float__() const throw(INTERP_KERNEL::Exception) @@ -2600,6 +2601,7 @@ namespace ParaMEDMEM void replaceOneValByInThis(int valToBeReplaced, int replacedBy) throw(INTERP_KERNEL::Exception); std::string repr() const throw(INTERP_KERNEL::Exception); std::string reprZip() const throw(INTERP_KERNEL::Exception); + std::string reprNotTooLong() const throw(INTERP_KERNEL::Exception); DataArrayInt *invertArrayO2N2N2O(int newNbOfElem) const throw(INTERP_KERNEL::Exception); DataArrayInt *invertArrayN2O2O2N(int oldNbOfElem) const throw(INTERP_KERNEL::Exception); DataArrayInt *invertArrayO2N2N2OBis(int newNbOfElem) const throw(INTERP_KERNEL::Exception); @@ -2806,7 +2808,7 @@ namespace ParaMEDMEM std::string __str__() const throw(INTERP_KERNEL::Exception) { - return self->repr(); + return self->reprNotTooLong(); } int __len__() const throw(INTERP_KERNEL::Exception)