pythonIO.h

Go to the documentation of this file.
00001 
00016 #ifndef _DLR_PYTHONIO_H_
00017 #define _DLR_PYTHONIO_H_
00018 
00019 #include <iostream>
00020 #include <string>
00021 
00022 namespace dlr {
00023 
00024   namespace utilities {
00025     
00046     template<class ArrayType>
00047     std::string
00048     toPythonList(const ArrayType& array0);
00049 
00050   
00062     template<class ArrayType>
00063     std::string
00064     toPythonNumericArray(const ArrayType& array0) {
00065       return toPythonNumeric2DArray(array0);
00066     }
00067 
00068 
00084     template<class ArrayType>
00085     std::string
00086     toPythonNumeric1DArray(const ArrayType& array0);
00087 
00088   
00112     template<class ArrayType>
00113     std::string
00114     toPythonNumeric2DArray(const ArrayType& array0);
00115 
00116   } // namespace utilities
00117   
00118 } // namespace dlr
00119 
00120 
00121 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00122 
00123 namespace dlr {
00124     
00125   using utilities::toPythonList;
00126   using utilities::toPythonNumericArray;
00127   using utilities::toPythonNumeric1DArray;
00128   using utilities::toPythonNumeric2DArray;
00129   
00130 } // namespace dlr
00131 
00132 
00133 /* ================ Function definitions follow ================ */
00134 
00135 namespace dlr {
00136 
00137   namespace utilities {
00138     
00139     template<class ArrayType>
00140     std::string
00141     toPythonList(const ArrayType& array0)
00142     {
00143       // We'll use an ostringstream to format the string.
00144       std::ostringstream outputStream;
00145 
00146       // Python string syntax starts with a square-bracket.
00147       outputStream << "[";
00148 
00149       // We'll iterate over every element in the array.
00150       size_t count = 0;
00151       while(count < array0.size()) {
00152         // This variable will keep track of how many entries have been
00153         // written to each line, so that we can politely insert carriage
00154         // returns.
00155         size_t lineCount = 0;
00156         // Loop just enough to complete one line.
00157         while((count < array0.size()) & (lineCount < 20)) {
00158           // If it's not the first entry, we need separating punctuation.
00159           if(count != 0) {
00160             // Usually a comma is all that's necessary.
00161             outputStream << ", ";
00162 
00163             // If we just finished a line, we'll need a carriage return and
00164             // an indent.
00165             if(lineCount == 0) {
00166               outputStream << "\n\t\t";
00167             }
00168           }
00169 
00170           // Write the next element.
00171           outputStream << array0[count];
00172 
00173           // Update the counters.
00174           ++count;
00175           ++lineCount;
00176         }
00177       }
00178 
00179       // Add the closing square-bracket.
00180       outputStream << "]";
00181 
00182       // And return the formatted string.
00183       return outputStream.str();
00184     }
00185 
00186 
00187     template<class ArrayType>
00188     std::string
00189     toPythonNumeric1DArray(const ArrayType& array0)
00190     {
00191       // We'll use an ostringstream to format the string.
00192       std::ostringstream outputStream;
00193 
00194       // Start with the appropriate syntax for a Numeric array,
00195       // and the the opening square-bracket of the top-level list.
00196       outputStream << "array(";
00197       outputStream << toPythonList(array0);
00198 
00199       // Close the open syntax.
00200       outputStream << ")\n";
00201 
00202       // And return the formatted string.
00203       return outputStream.str();
00204     }
00205 
00206 
00207     template<class ArrayType>
00208     std::string
00209     toPythonNumeric2DArray(const ArrayType& array0)
00210     {
00211       // We'll use an ostringstream to format the string.
00212       std::ostringstream outputStream;
00213 
00214       // Start with the appropriate syntax for a Numeric array,
00215       // and the the opening square-bracket of the top-level list.
00216       outputStream << "array([";
00217 
00218       // Now iterate over each row.
00219       for(size_t index0 = 0; index0 < array0.rows(); ++index0) {
00220         if(index0 != 0) {
00221           outputStream << ",\n\t";
00222         }
00223         outputStream << toPythonList(array0.row(index0));
00224       }
00225 
00226       // Close the open syntax.
00227       outputStream << "])\n";
00228 
00229       // And return the formatted string.
00230       return outputStream.str();
00231     }  
00232 
00233   } // namespace utilities
00234 
00235 } // namespace dlr
00236 
00237 #endif // #ifndef _DLR_IMAGEIO_H_

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2