pyhon DeckKeyword: New constructor takes parserkeyword and list of records.
python Deckkeyword: can init with empty records. python Deckkeyword: can record simple records, but not defaults. python deckvalue: can take defauløt values.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/Deck/DeckValue.hpp>
|
||||
#include <opm/parser/eclipse/Deck/DeckItem.hpp>
|
||||
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
|
||||
#include <opm/parser/eclipse/Deck/DeckRecord.hpp>
|
||||
@@ -10,6 +11,8 @@
|
||||
#include "export.hpp"
|
||||
#include "converters.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -56,11 +59,80 @@ struct DeckRecordIterator
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
bool is_number(const std::string& s)
|
||||
{
|
||||
return !s.empty() && std::find_if(s.begin(),
|
||||
s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
|
||||
}
|
||||
|
||||
|
||||
void push_string_as_deck_value(std::vector<DeckValue>& record, const std::string str) {
|
||||
|
||||
int max_indx = str.length() - 1;
|
||||
if (str[max_indx] == '*') {
|
||||
int it;
|
||||
|
||||
if (max_indx == 0)
|
||||
it = 1;
|
||||
else {
|
||||
std::string substr = str.substr(0, max_indx);
|
||||
if (is_number(substr)) {
|
||||
it = std::stoi(substr);
|
||||
}
|
||||
else
|
||||
throw py::type_error("DeckKeyword: string values ending in '*' can only be preceeded by a number.");
|
||||
}
|
||||
|
||||
for (int i = 0; i < it; i++)
|
||||
record.push_back( DeckValue() );
|
||||
}
|
||||
else
|
||||
record.push_back( DeckValue(str) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void python::common::export_DeckKeyword(py::module& module) {
|
||||
py::class_< DeckKeyword >( module, "DeckKeyword")
|
||||
.def(py::init<const ParserKeyword& >())
|
||||
|
||||
.def(py::init([](const ParserKeyword& parser_keyword, py::list record_list) {
|
||||
|
||||
std::vector< std::vector<DeckValue> > value_record_list;
|
||||
|
||||
for (py::handle record_obj : record_list) {
|
||||
py::list record = record_obj.cast<py::list>();
|
||||
std::vector<DeckValue> value_record;
|
||||
|
||||
for (py::handle value_obj : record) {
|
||||
|
||||
try {
|
||||
int val_int = value_obj.cast<int>();
|
||||
value_record.push_back( DeckValue(val_int) );
|
||||
}
|
||||
catch (std::exception e_int) {
|
||||
try {
|
||||
double val_double = value_obj.cast<double>();
|
||||
value_record.push_back( DeckValue(val_double) );
|
||||
}
|
||||
catch(std::exception e_double) {
|
||||
try {
|
||||
std::string val_string = value_obj.cast<std::string>();
|
||||
push_string_as_deck_value(value_record, val_string);
|
||||
}
|
||||
catch(std::exception e_string) {
|
||||
throw py::type_error("DeckKeyword: tried to add unkown type to record.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
value_record_list.push_back( value_record );
|
||||
}
|
||||
return DeckKeyword(parser_keyword, value_record_list);
|
||||
} ) )
|
||||
|
||||
.def( "__repr__", &DeckKeyword::name )
|
||||
.def( "__str__", &str<DeckKeyword> )
|
||||
.def("__iter__", [] (const DeckKeyword &keyword) { return py::make_iterator(keyword.begin(), keyword.end()); }, py::keep_alive<0,1>())
|
||||
|
||||
Reference in New Issue
Block a user