From f47607d7256d3edff50da4be86b3de14cf628a24 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Sun, 17 Dec 2017 13:00:17 -0300 Subject: [PATCH] Python measures(all=False, group="Body") Similar to R measures returning a dict with a list for each measure field in the requested group (Body or Hrv). --- src/Python/SIP/Bindings.cpp | 74 +++++++++++++++++++- src/Python/SIP/Bindings.h | 1 + src/Python/SIP/goldencheetah.sip | 1 + src/Python/SIP/sipAPIgoldencheetah.h | 76 +++++++++++---------- src/Python/SIP/sipgoldencheetahBindings.cpp | 38 ++++++++++- src/Python/SIP/sipgoldencheetahcmodule.cpp | 2 + 6 files changed, 154 insertions(+), 38 deletions(-) diff --git a/src/Python/SIP/Bindings.cpp b/src/Python/SIP/Bindings.cpp index e69966614..adbb68759 100644 --- a/src/Python/SIP/Bindings.cpp +++ b/src/Python/SIP/Bindings.cpp @@ -759,7 +759,6 @@ Bindings::pmc(bool all, QString metric) const } else { unsigned int index=0; - QDate start = pmcData.start(); for(int k=0; k < pmcData.days(); k++) { // day today @@ -790,6 +789,79 @@ Bindings::pmc(bool all, QString metric) const return NULL; } +PyObject* +Bindings::measures(bool all, QString group) const +{ + Context *context = python->contexts.value(threadid()); + + // return a dict with Measures data for all or the current season + if (context && context->athlete && context->athlete->measures) { + + // import datetime if necessary + if (PyDateTimeAPI == NULL) PyDateTime_IMPORT; + + // get the currently selected date range + DateRange range(context->currentDateRange()); + + // convert the group symbol to an index, default to Body=0 + int groupIdx = context->athlete->measures->getGroupSymbols().indexOf(group); + if (groupIdx < 0) groupIdx = 0; + + // Update range for all + if (all) { + range.from = context->athlete->measures->getStartDate(groupIdx); + range.to = context->athlete->measures->getEndDate(groupIdx); + } + + // how many entries ? + unsigned int size = range.from.daysTo(range.to) + 1; + + // returning a dict with + // date, field1, field2, ... + PyObject* ans = PyDict_New(); + + // DATE - 1 a day from start + PyObject* datelist = PyList_New(size); + QDate start = range.from; + for(unsigned int k=0; kathlete->measures->getFieldSymbols(groupIdx); + QVector fields(fieldSymbols.count()); + for (int i=0; i= range.from && start.addDays(k) <= range.to) { + + for (int fieldIdx=0; fieldIdxathlete->measures->getFieldValue(groupIdx, start.addDays(k), fieldIdx))); + + index++; + } + } + + // add to the dict + for (int fieldIdx=0; fieldIdxapi_malloc #define sipFree sipAPI_goldencheetah->api_free diff --git a/src/Python/SIP/sipgoldencheetahBindings.cpp b/src/Python/SIP/sipgoldencheetahBindings.cpp index 2c02d468c..296ff0f72 100644 --- a/src/Python/SIP/sipgoldencheetahBindings.cpp +++ b/src/Python/SIP/sipgoldencheetahBindings.cpp @@ -374,6 +374,41 @@ static PyObject *meth_Bindings_pmc(PyObject *sipSelf, PyObject *sipArgs, PyObjec } +extern "C" {static PyObject *meth_Bindings_measures(PyObject *, PyObject *, PyObject *);} +static PyObject *meth_Bindings_measures(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds) +{ + PyObject *sipParseErr = NULL; + + { + bool a0 = 0; + ::QString a1def = QString("Body"); + ::QString* a1 = &a1def; + int a1State = 0; + ::Bindings *sipCpp; + + static const char *sipKwdList[] = { + sipName_all, + sipName_group, + }; + + if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, sipKwdList, NULL, "B|bJ1", &sipSelf, sipType_Bindings, &sipCpp, &a0, sipType_QString,&a1, &a1State)) + { + PyObject * sipRes; + + sipRes = sipCpp->measures(a0,*a1); + sipReleaseType(a1,sipType_QString,a1State); + + return sipRes; + } + } + + /* Raise an exception if the arguments couldn't be parsed. */ + sipNoMethod(sipParseErr, sipName_Bindings, sipName_measures, NULL); + + return NULL; +} + + extern "C" {static PyObject *meth_Bindings_activityMeanmax(PyObject *, PyObject *, PyObject *);} static PyObject *meth_Bindings_activityMeanmax(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds) { @@ -546,6 +581,7 @@ static PyMethodDef methods_Bindings[] = { {SIP_MLNAME_CAST(sipName_activityMetrics), (PyCFunction)meth_Bindings_activityMetrics, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_athlete), meth_Bindings_athlete, METH_VARARGS, NULL}, {SIP_MLNAME_CAST(sipName_build), meth_Bindings_build, METH_VARARGS, NULL}, + {SIP_MLNAME_CAST(sipName_measures), (PyCFunction)meth_Bindings_measures, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_metrics), (PyCFunction)meth_Bindings_metrics, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_pmc), (PyCFunction)meth_Bindings_pmc, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_seasonMeanmax), (PyCFunction)meth_Bindings_seasonMeanmax, METH_VARARGS|METH_KEYWORDS, NULL}, @@ -573,7 +609,7 @@ sipClassTypeDef sipTypeDef_goldencheetah_Bindings = { { sipNameNr_Bindings, {0, 0, 1}, - 15, methods_Bindings, + 16, methods_Bindings, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, diff --git a/src/Python/SIP/sipgoldencheetahcmodule.cpp b/src/Python/SIP/sipgoldencheetahcmodule.cpp index 5911db661..e46c970f9 100644 --- a/src/Python/SIP/sipgoldencheetahcmodule.cpp +++ b/src/Python/SIP/sipgoldencheetahcmodule.cpp @@ -25,6 +25,7 @@ const char sipStrings_goldencheetah[] = { '_', '_', 'g', 'e', 't', 'i', 't', 'e', 'm', '_', '_', 0, 's', 'e', 'r', 'i', 'e', 's', 'L', 'a', 's', 't', 0, 's', 'e', 'r', 'i', 'e', 's', 'N', 'a', 'm', 'e', 0, + 'm', 'e', 'a', 's', 'u', 'r', 'e', 's', 0, 't', 'h', 'r', 'e', 'a', 'd', 'i', 'd', 0, 'B', 'i', 'n', 'd', 'i', 'n', 'g', 's', 0, 'w', 'e', 'b', 'p', 'a', 'g', 'e', 0, @@ -38,6 +39,7 @@ const char sipStrings_goldencheetah[] = { 'm', 'e', 't', 'r', 'i', 'c', 0, 'f', 'i', 'l', 't', 'e', 'r', 0, 's', 'e', 'r', 'i', 'e', 's', 0, + 'g', 'r', 'o', 'u', 'p', 0, 'b', 'u', 'i', 'l', 'd', 0, 't', 'y', 'p', 'e', 0, 'u', 'r', 'l', 0,