extend Python api of units:

+ get list of all available schemas
+ get currently set schema
+ translate a quantity to a given schema
This commit is contained in:
wmayer
2017-08-13 16:55:00 +02:00
parent 2cd535e015
commit 892e78d445
4 changed files with 146 additions and 17 deletions

View File

@@ -24,6 +24,7 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <memory>
#endif
#include <CXX/Objects.hxx>
@@ -68,12 +69,24 @@ PyMethodDef UnitsApi::Methods[] = {
//},
{"parseQuantity", (PyCFunction) UnitsApi::sParseQuantity ,1,
"parseQuantity(string) -> Base.Quantity()\n\n"
"calculate a mathematical expression with units to a quantity object. \n"
"can be used for simple unit translation like: \n"
" parseQuantity('10m')\n"
" or for more complex espressions:\n"
" parseQuantity('sin(pi)/50.0 m/s^2')\n"
"parseQuantity(string) -> Base.Quantity()\n\n"
"calculate a mathematical expression with units to a quantity object. \n"
"can be used for simple unit translation like: \n"
"parseQuantity('10m')\n"
"or for more complex espressions:\n"
"parseQuantity('sin(pi)/50.0 m/s^2')\n"
},
{"listSchemas", (PyCFunction) UnitsApi::sListSchemas ,1,
"listSchemas() -> a tuple of schemas\n\n"
"listSchemas(int) -> description of the given schema\n\n"
},
{"getSchema", (PyCFunction) UnitsApi::sGetSchema ,1,
"getSchema() -> int\n\n"
"The int is the position of the tuple returned by listSchemas"
},
{"schemaTranslate", (PyCFunction) UnitsApi::sSchemaTranslate ,1,
"schemaTranslate(Quantity, int) -> tuple\n\n"
"Translate a quantity to a given schema"
},
{NULL, NULL, 0, NULL} /* Sentinel */
@@ -148,3 +161,67 @@ PyObject* UnitsApi::sParseQuantity(PyObject * /*self*/, PyObject *args,PyObject
return new QuantityPy(new Quantity(rtn));
}
PyObject* UnitsApi::sListSchemas(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{
if (PyArg_ParseTuple(args, "")) {
int num = NumUnitSystemTypes;
Py::Tuple tuple(num);
for (int i=0; i<num; i++) {
tuple.setItem(i, Py::String(UnitsApi::getDescription(static_cast<UnitSystem>(i))));
}
return Py::new_reference_to(tuple);
}
PyErr_Clear();
int index;
if (PyArg_ParseTuple(args, "i", &index)) {
int num = NumUnitSystemTypes;
if (index < 0 || index >= num) {
PyErr_SetString(PyExc_ValueError, "invalid schema value");
return 0;
}
return Py_BuildValue("s", UnitsApi::getDescription(static_cast<UnitSystem>(index)));
}
PyErr_SetString(PyExc_TypeError, "int or empty argument list expected");
return 0;
}
PyObject* UnitsApi::sGetSchema(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
return Py_BuildValue("i", static_cast<int>(actSystem));
}
PyObject* UnitsApi::sSchemaTranslate(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{
PyObject* q;
int index;
if (!PyArg_ParseTuple(args, "O!i", &(QuantityPy::Type), &q, &index))
return 0;
Quantity quant;
quant = *static_cast<Base::QuantityPy*>(q)->getQuantityPtr();
std::unique_ptr<UnitsSchema> schema(createSchema(static_cast<UnitSystem>(index)));
if (!schema) {
PyErr_SetString(PyExc_ValueError, "invalid schema value");
return 0;
}
double factor;
QString uus;
QString uss = schema->schemaTranslate(quant, factor, uus);
Py::Tuple res(3);
res[0] = Py::String(uss.toUtf8(),"utf-8");
res[1] = Py::Float(factor);
res[2] = Py::String(uus.toUtf8(),"utf-8");
return Py::new_reference_to(res);
}