From cc2f56c7a337254bb471ade645c75608340e8fe2 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Thu, 21 Dec 2017 15:19:14 -0300 Subject: [PATCH] Python season(all=False, compare=False) Similar to R season(), retuns a dict with name, from, to and color lists. --- src/Python/SIP/Bindings.cpp | 68 +++++++++++++++++++++ src/Python/SIP/Bindings.h | 3 + src/Python/SIP/goldencheetah.sip | 3 + src/Python/SIP/sipAPIgoldencheetah.h | 30 ++++----- src/Python/SIP/sipgoldencheetahBindings.cpp | 35 ++++++++++- src/Python/SIP/sipgoldencheetahcmodule.cpp | 1 + 6 files changed, 125 insertions(+), 15 deletions(-) diff --git a/src/Python/SIP/Bindings.cpp b/src/Python/SIP/Bindings.cpp index 5caee413a..bc0e2fdcc 100644 --- a/src/Python/SIP/Bindings.cpp +++ b/src/Python/SIP/Bindings.cpp @@ -8,6 +8,7 @@ #include "RideCache.h" #include "DataFilter.h" #include "PMCData.h" +#include "Season.h" #include "Bindings.h" @@ -936,6 +937,73 @@ Bindings::measures(bool all, QString group) const return NULL; } +PyObject* +Bindings::season(bool all, bool compare) const +{ + Context *context = python->contexts.value(threadid()); + if (context == NULL) return NULL; + + // import datetime if necessary + if (PyDateTimeAPI == NULL) PyDateTime_IMPORT; + + // dict for season: color, name, start, end + // XXX TODO type needs adding, but we need to unpick the + // phase/season object model first, will do later + PyObject* ans = PyDict_New(); + + // worklist of date ranges to return + // XXX TODO use a Season worklist one the phase/season + // object model is fixed + QList worklist; + + if (compare) { + // return a list, even if just one + if (context->isCompareDateRanges) { + foreach(CompareDateRange p, context->compareDateRanges) + worklist << DateRange(p.start, p.end, p.name, p.color); + } else { + // if compare not active just return current selection + worklist << context->currentDateRange(); + } + + } else if (all) { + // list all seasons + foreach(Season season, context->athlete->seasons->seasons) { + worklist << DateRange(season.start, season.end, season.name, QColor(127,127,127)); + } + + } else { + + // just the currently selected season please + worklist << context->currentDateRange(); + } + + PyObject* start = PyList_New(worklist.count()); + PyObject* end = PyList_New(worklist.count()); + PyObject* name = PyList_New(worklist.count()); + PyObject* color = PyList_New(worklist.count()); + + int index=0; + + foreach(DateRange p, worklist){ + + PyList_SET_ITEM(start, index, PyDate_FromDate(p.from.year(), p.from.month(), p.from.day())); + PyList_SET_ITEM(end, index, PyDate_FromDate(p.to.year(), p.to.month(), p.to.day())); + PyList_SET_ITEM(name, index, PyUnicode_FromString(p.name.toUtf8().constData())); + PyList_SET_ITEM(color, index, PyUnicode_FromString(p.color.name().toUtf8().constData())); + index++; + } + + // list into a data.frame + PyDict_SetItemString(ans, "start", start); + PyDict_SetItemString(ans, "end", end); + PyDict_SetItemString(ans, "name", name); + PyDict_SetItemString(ans, "color", color); + + // return it + return ans; +} + int Bindings::webpage(QString url) const { diff --git a/src/Python/SIP/Bindings.h b/src/Python/SIP/Bindings.h index ab9b4b44b..b27280022 100644 --- a/src/Python/SIP/Bindings.h +++ b/src/Python/SIP/Bindings.h @@ -30,6 +30,9 @@ class Bindings { // working with activities PyObject* activities(QString filter=QString()) const; + // working with seasons + PyObject* season(bool all=false, bool compare=false) const; + // working with data series bool seriesPresent(int type, PyObject* activity=NULL) const; int seriesLast() const; diff --git a/src/Python/SIP/goldencheetah.sip b/src/Python/SIP/goldencheetah.sip index 36949dd2c..c44410b07 100644 --- a/src/Python/SIP/goldencheetah.sip +++ b/src/Python/SIP/goldencheetah.sip @@ -119,6 +119,9 @@ public: // working with activities PyObject* activities(QString filter=QString()) /TransferBack/; + // working with seasons + PyObject* season(bool all=false, bool compare=false) /TransferBack/; + // working with series bool seriesPresent(int type=10, PyObject* activity=NULL) const; QString seriesName(int type=10) const; diff --git a/src/Python/SIP/sipAPIgoldencheetah.h b/src/Python/SIP/sipAPIgoldencheetah.h index 435ac2c3f..f77910c4f 100644 --- a/src/Python/SIP/sipAPIgoldencheetah.h +++ b/src/Python/SIP/sipAPIgoldencheetah.h @@ -63,20 +63,22 @@ #define sipName_metric &sipStrings_goldencheetah[250] #define sipNameNr_series 257 #define sipName_series &sipStrings_goldencheetah[257] -#define sipNameNr_filter 264 -#define sipName_filter &sipStrings_goldencheetah[264] -#define sipNameNr_group 271 -#define sipName_group &sipStrings_goldencheetah[271] -#define sipNameNr_build 277 -#define sipName_build &sipStrings_goldencheetah[277] -#define sipNameNr_type 283 -#define sipName_type &sipStrings_goldencheetah[283] -#define sipNameNr_url 288 -#define sipName_url &sipStrings_goldencheetah[288] -#define sipNameNr_pmc 292 -#define sipName_pmc &sipStrings_goldencheetah[292] -#define sipNameNr_all 296 -#define sipName_all &sipStrings_goldencheetah[296] +#define sipNameNr_season 264 +#define sipName_season &sipStrings_goldencheetah[264] +#define sipNameNr_filter 271 +#define sipName_filter &sipStrings_goldencheetah[271] +#define sipNameNr_group 278 +#define sipName_group &sipStrings_goldencheetah[278] +#define sipNameNr_build 284 +#define sipName_build &sipStrings_goldencheetah[284] +#define sipNameNr_type 290 +#define sipName_type &sipStrings_goldencheetah[290] +#define sipNameNr_url 295 +#define sipName_url &sipStrings_goldencheetah[295] +#define sipNameNr_pmc 299 +#define sipName_pmc &sipStrings_goldencheetah[299] +#define sipNameNr_all 303 +#define sipName_all &sipStrings_goldencheetah[303] #define sipMalloc sipAPI_goldencheetah->api_malloc #define sipFree sipAPI_goldencheetah->api_free diff --git a/src/Python/SIP/sipgoldencheetahBindings.cpp b/src/Python/SIP/sipgoldencheetahBindings.cpp index 06fa7e024..51c2ec06d 100644 --- a/src/Python/SIP/sipgoldencheetahBindings.cpp +++ b/src/Python/SIP/sipgoldencheetahBindings.cpp @@ -151,6 +151,38 @@ static PyObject *meth_Bindings_activities(PyObject *sipSelf, PyObject *sipArgs, } +extern "C" {static PyObject *meth_Bindings_season(PyObject *, PyObject *, PyObject *);} +static PyObject *meth_Bindings_season(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds) +{ + PyObject *sipParseErr = NULL; + + { + bool a0 = 0; + bool a1 = 0; + ::Bindings *sipCpp; + + static const char *sipKwdList[] = { + sipName_all, + sipName_compare, + }; + + if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, sipKwdList, NULL, "B|bb", &sipSelf, sipType_Bindings, &sipCpp, &a0, &a1)) + { + PyObject * sipRes; + + sipRes = sipCpp->season(a0,a1); + + return sipRes; + } + } + + /* Raise an exception if the arguments couldn't be parsed. */ + sipNoMethod(sipParseErr, sipName_Bindings, sipName_season, NULL); + + return NULL; +} + + extern "C" {static PyObject *meth_Bindings_seriesPresent(PyObject *, PyObject *, PyObject *);} static PyObject *meth_Bindings_seriesPresent(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds) { @@ -622,6 +654,7 @@ static PyMethodDef methods_Bindings[] = { {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_season), (PyCFunction)meth_Bindings_season, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_seasonMeanmax), (PyCFunction)meth_Bindings_seasonMeanmax, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_seasonMetrics), (PyCFunction)meth_Bindings_seasonMetrics, METH_VARARGS|METH_KEYWORDS, NULL}, {SIP_MLNAME_CAST(sipName_series), (PyCFunction)meth_Bindings_series, METH_VARARGS|METH_KEYWORDS, NULL}, @@ -647,7 +680,7 @@ sipClassTypeDef sipTypeDef_goldencheetah_Bindings = { { sipNameNr_Bindings, {0, 0, 1}, - 17, methods_Bindings, + 18, 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 439484a12..2bf881d19 100644 --- a/src/Python/SIP/sipgoldencheetahcmodule.cpp +++ b/src/Python/SIP/sipgoldencheetahcmodule.cpp @@ -40,6 +40,7 @@ const char sipStrings_goldencheetah[] = { 'Q', 'S', 't', 'r', 'i', 'n', 'g', 0, 'm', 'e', 't', 'r', 'i', 'c', 0, 's', 'e', 'r', 'i', 'e', 's', 0, + 's', 'e', 'a', 's', 'o', 'n', 0, 'f', 'i', 'l', 't', 'e', 'r', 0, 'g', 'r', 'o', 'u', 'p', 0, 'b', 'u', 'i', 'l', 'd', 0,