⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib_kernel.cpp

📁 orange源码 数据挖掘技术
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    if (!convertFromPython(arg, valType))
      return PYNULL;
    PVariable var = PyOrange_AsVariable(self);
    return Value_FromVariableValue(var, var->specialValue(valType));
  PyCATCH
}



PyObject *EnumVariable_getitem_sq(PyObject *self, int index)
{ PyTRY
    CAST_TO(TEnumVariable, var)
    if (!var->values || (index<0) || (index>=int(var->values->size())))
      PYERROR(PyExc_IndexError, "index out of range", PYNULL);
    return Value_FromVariableValue(PyOrange_AsVariable(self), TValue(index));
  PyCATCH
}


PyObject *IntVariable_getitem_sq(PyObject *self, int index)
{ PyTRY
    CAST_TO(TIntVariable, var);
    int maxInd = var->endValue - var->startValue;
    if (maxInd<0)
      PYERROR(PyExc_IndexError, "interval not specified", PYNULL);
    if ((index<0) || (index>maxInd))
      PYERROR(PyExc_IndexError, "index out of range", PYNULL);
    return Value_FromVariableValue(PyOrange_AsVariable(self), TValue(var->startValue+index));
  PyCATCH
}


PyObject *FloatVariable_getitem_sq(PyObject *self, int index)
{ PyTRY
    CAST_TO(TFloatVariable, var);
    if ((var->stepValue<=0) || (var->startValue>var->endValue))
      PYERROR(PyExc_IndexError, "interval not specified", PYNULL);
    
    float maxInd = (var->endValue - var->startValue)/var->stepValue;

    if ((index<0) || (index>maxInd))
      PYERROR(PyExc_IndexError, "index out of range", PYNULL);
    return Value_FromVariableValue(PyOrange_AsVariable(self), TValue(var->startValue+var->stepValue*index));
  PyCATCH
}


bool convertFromPythonWithVariable(PyObject *obj, string &str)
{ return convertFromPythonWithML(obj, str, *FindOrangeType(typeid(TVariable))); }


bool varListFromDomain(PyObject *boundList, PDomain domain, TVarList &boundSet, bool allowSingle, bool checkForIncludance)
{ if (PyOrVarList_Check(boundList)) {
    PVarList variables = PyOrange_AsVarList(boundList);
    if (checkForIncludance)
      const_PITERATE(TVarList, vi, variables)
        if (!domain || (domain->getVarNum(*vi, false)==ILLEGAL_INT)) {
          PyErr_Format(PyExc_IndexError, "variable '%s' does not exist in the domain", (*vi)->name.c_str());
          return false;
        }
    boundSet=variables.getReference();
    return true;
  }
  
  if (PySequence_Check(boundList)) {
    PyObject *iterator = PyObject_GetIter(boundList);
    if (iterator) {
      for(PyObject *item = PyIter_Next(iterator); item; item = PyIter_Next(iterator)) {
        PVariable variable=varFromArg_byDomain(item, domain, checkForIncludance);
        Py_DECREF(item);
        if (!variable) {
          Py_DECREF(iterator);
          return false;
        }
        boundSet.push_back(variable);
      }
        
      Py_DECREF(iterator);
      return true;
    }
  }
      
  else if (allowSingle) {
    PVariable variable=varFromArg_byDomain(boundList, domain, checkForIncludance);
    if (variable) {
      boundSet.push_back(variable);
      return true;
    }
  }
  PYERROR(PyExc_TypeError, "invalid argument (list of variables expected)", false);
}


// Given a parameter from Python and a domain, it returns a variable.
// Python's parameter can be a string name, an index or Variable
PVariable varFromArg_byDomain(PyObject *obj, PDomain domain, bool checkForIncludance)
{ PVariable var;
  if (domain) {
    PyTRY
      if (PyString_Check(obj)) {
        const char *attr = PyString_AS_STRING(obj);
        PVariable res = domain->getVar(attr, true, false);
        if (!res)
          PyErr_Format(PyExc_IndexError, "attribute '%s' not found", attr);
        return res;
      }
      if (PyInt_Check(obj)) {
        int idx = PyInt_AsLong(obj);

        if (idx<0) {
          PVariable res = domain->getMetaVar(idx, false);
          if (!res)
            PyErr_Format(PyExc_IndexError, "meta attribute %i not found", idx);
          return res;
        }

        if (idx>=int(domain->variables->size()))
          PYERROR(PyExc_IndexError, "index out of range", PVariable());

        return domain->getVar(idx);
      }
    PyCATCH_r(PVariable())
  }

  if (PyOrVariable_Check(obj)) {
    PVariable var(PyOrange_AsVariable(obj));
    if (checkForIncludance)
      if (!domain || (domain->getVarNum(var, false)==ILLEGAL_INT))
        PYERROR(PyExc_IndexError, "variable does not exist in the domain", PVariable());
    return var;
  }

  PYERROR(PyExc_TypeError, "invalid type for variable", PVariable());
}


bool varListFromVarList(PyObject *boundList, PVarList varlist, TVarList &boundSet, bool allowSingle, bool checkForIncludance)
{ if (PyOrVarList_Check(boundList)) {
    PVarList variables = PyOrange_AsVarList(boundList);
    if (checkForIncludance)
      const_PITERATE(TVarList, vi, variables) {
        TVarList::const_iterator fi(varlist->begin()), fe(varlist->end());
        for(; (fi!=fe) && (*fi != *vi); fi++);
        if (fi==fe) {
          PyErr_Format(PyExc_IndexError, "variable '%s' does not exist in the domain", (*vi)->name.c_str());
          return false;
        }
      }
    boundSet = variables.getReference();
    return true;
  }
  
  if (PyList_Check(boundList)) {
    for(int pos=0, max=PyList_Size(boundList); pos<max; pos++) {
      PyObject *li=PyList_GetItem(boundList, pos);
      if (!li)
        PYERROR(PyExc_TypeError, "can't read the argument list", false);
      PVariable variable = varFromArg_byVarList(li, varlist, checkForIncludance);
      if (!variable)
        return false;
      boundSet.push_back(variable);
    }
    return true;
  }
  else if (allowSingle) {
    PVariable variable = varFromArg_byVarList(boundList, varlist, checkForIncludance);
    if (!variable)
      return false;
    boundSet.push_back(variable);
    return true;
  }

  PYERROR(PyExc_TypeError, "invalid attribute for list of variables", false);
}


// Given a parameter from Python and a list of variables, it returns a variable.
// Python's parameter can be a string name, an index or Variable
PVariable varFromArg_byVarList(PyObject *obj, PVarList varlist, bool checkForIncludance)
{ PVariable var;
  if (varlist) {
    PyTRY
      if (PyString_Check(obj)) {
        char *s = PyString_AS_STRING(obj);
        TVarList::const_iterator fi(varlist->begin()), fe(varlist->end());
        for(; (fi!=fe) && ((*fi)->name != s); fi++);
        if (fi==fe) {
          PyErr_Format(PyExc_IndexError, "variable '%s' does not exist in the domain", s);
          return PVariable();
        }
        else
          return *fi;
      }
    PyCATCH_r(PVariable())
  }

  if (PyOrVariable_Check(obj)) {
    PVariable var(PyOrange_AsVariable(obj));
    if (checkForIncludance) {
      TVarList::const_iterator fi(varlist->begin()), fe(varlist->end());
      for(; (fi!=fe) && (*fi != var); fi++);
      if (fi==fe)
        PYERROR(PyExc_IndexError, "variable does not exist in the domain", PVariable());
    }
    return var;
  }

  PYERROR(PyExc_TypeError, "invalid type for variable", PVariable());
}


bool varNumFromVarDom(PyObject *pyvar, PDomain domain, int &attrNo)
{ 
  PVariable var=varFromArg_byDomain(pyvar, domain);
  if (!var)
    return false; // varFromArg_byDomain has already set the error message

  PITERATE(TVarList, vi, domain->attributes)
    if (*vi==var) {
      attrNo = vi-domain->attributes->begin();
      return true;
    }

  attrNo = domain->getMetaNum(var, false);
  return attrNo != ILLEGAL_INT;
}


bool weightFromArg_byDomain(PyObject *pyweight, PDomain domain, int &weightID)
{
  if (!pyweight || (pyweight == Py_None))
    weightID = 0;

  else if (PyInt_Check(pyweight))
    weightID =  PyInt_AsLong(pyweight);

  else {
    PVariable var = varFromArg_byDomain(pyweight, domain);
    if (!var)
      PYERROR(PyExc_TypeError, "invalid or unknown weight attribute", false);
  
    weightID = domain->getVarNum(var);
  }

  return true;
}


static PExampleGenerator *ptw_examplegenerator;

int ptw_weightByDomainCB(PyObject *args, void *weight)
{ 
  PDomain dom = ptw_examplegenerator ? (*ptw_examplegenerator)->domain : PDomain();
  ptw_examplegenerator = NULL;
  return weightFromArg_byDomain(args, dom, *(int *)weight) ? 1 : 0;
}

converter pt_weightByGen(PExampleGenerator &peg)
{ 
  ptw_examplegenerator = &peg;
  return ptw_weightByDomainCB;
}


PyObject *StringValue_new(PyTypeObject *type, PyObject *args, PyObject *) BASED_ON(SomeValue, "(string)")
{ char *s;
  if (!PyArg_ParseTuple(args, "s:StringValue", &s))
    return PYNULL;

  return WrapNewOrange(mlnew TStringValue(s), type);
}


PyObject *StringValue__reduce__(PyObject *self)
{
  return Py_BuildValue("O(s)", (PyObject *)(self->ob_type), SELF_AS(TStringValue).value.c_str());
}


/* ************ ATTRIBUTED FLOAT LIST ************ */

PyObject *AttributedFloatList_new(PyTypeObject *type, PyObject *args, PyObject *keywds) BASED_ON(FloatList, "(attributes, list)") ALLOWS_EMPTY
{
  PyObject *ob1 = NULL, *ob2 = NULL;
  if (!PyArg_UnpackTuple(args, "AttributedFloatList.new", 0, 2, &ob1, &ob2))
    return PYNULL;

  PyObject *wabl = ListOfUnwrappedMethods<PAttributedFloatList, TAttributedFloatList, float>::_new(type, ob2 ? ob2 : ob1, keywds);

  if (ob2) {
    PVarList attributes = PVarList_FromArguments(ob1);
    if (!attributes)
      return PYNULL;

    PyOrange_AsAttributedFloatList(wabl)->attributes = attributes;
  }

  return wabl;
}


PyObject * /*no pyxtract!*/ FloatList_getitem_sq(TPyOrange *self, int index);
int        /*no pyxtract!*/ FloatList_setitem_sq(TPyOrange *self, int index, PyObject *item);

int AttributedList_getIndex(const int &listsize, PVarList attributes, PyObject *index)
{
  int res;

  if (!listsize)
    PYERROR(PyExc_IndexError, "the list is empty", ILLEGAL_INT);

  if (PyInt_Check(index)) {
    res = (int)PyInt_AsLong(index);
    if (res < 0)
      res += listsize;
  }

  else {
    if (!attributes)
      PYERROR(PyExc_AttributeError, "variable list not defined, need integer indices", ILLEGAL_INT);

    if (PyOrVariable_Check(index)) {
      PVariable var = PyOrange_AsVariable(index);
      TVarList::const_iterator vi(attributes->begin()), ve(attributes->end());
      int ind = 0;
      for(; vi!=ve; vi++, ind++)
        if (*vi == var) {
          res = ind;
          break;
        }

      if (vi == ve) {
        PyErr_Format(PyExc_AttributeError, "attribute '%s' not found in the list", var->name.c_str());
        return ILLEGAL_INT;
      }
    }
    
    else if (PyString_Check(index)) {
      const char *name = PyString_AsString(index);
      TVarList::const_iterator vi(attributes->begin()), ve(attributes->end());
      int ind = 0;
      for(; vi!=ve; vi++, ind++)
        if ((*vi)->name == name) {
          res = ind;
          break;
        }

      if (vi == ve) {
        PyErr_Format(PyExc_AttributeError, "attribute '%s' not found in the list", name);
        return ILLEGAL_INT;
      }
    }

    else {
      PyErr_Format(PyExc_TypeError, "cannot index the list by '%s'", index->ob_type->tp_name);
      return ILLEGAL_INT;
    }
  }

  if ((res >= listsize) || (res < 0)) {
    PyErr_Format(PyExc_IndexError, "index %i out of range 0-%i", res, listsize-1);
    return ILLEGAL_INT;
  }

  return res;
}


PyObject *AttributedFloatList_getitem(TPyOrange *self, PyObject *index)
{
  PyTRY 
    CAST_TO(TAttributedFloatList, aflist)

    const int ind = AttributedList_getIndex(aflist->size(), aflist->attributes, index);
    if (ind == ILLEGAL_INT)
      return PYNULL;

    return FloatList_getitem_sq(self, ind);
  PyCATCH
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -