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

📄 cls_example.cpp

📁 orange源码 数据挖掘技术
💻 CPP
📖 第 1 页 / 共 3 页
字号:


PyObject *Example_getmeta(TPyExample *pex, PyObject *index) PYARGS(METH_O, "(id | var) -> Value; Gets a meta-value")
{ PyTRY
    PVariable var;
    int idx = getMetaIdFromPy(PyExample_AS_Example(pex), index, var);
    if (!idx)
      return PYNULL; 

    return convertToPythonNative(PyExample_AS_Example(pex)->getMeta(idx), var);

    // This could be better, but I wouldn't dare to change this
    // return Value_FromVariableValue(var, PyExample_AS_Example(pex)->getMeta(idx));
  PyCATCH
}


PyObject *Example_getmetas(TPyExample *pex, PyObject *args) PYARGS(METH_VARARGS, "([key-type]) -> dictionary with a copy of example's meta attributes")
{
  PyTRY
    PyTypeObject *keytype = &PyInt_Type;
    if (!PyArg_ParseTuple(args, "|O:Example.getmetas", &keytype))
      return NULL;

    if ((keytype != &PyInt_Type) && (keytype != &PyString_Type) && (keytype != (PyTypeObject *)&PyOrVariable_Type))
      PYERROR(PyExc_TypeError, "invalid key type (should be nothing, int, str, or orange.Variable)", NULL);
      
    PExample ex = PyExample_AS_Example(pex);
    const TDomain &dom = ex->domain.getReference();

    PyObject *res = PyDict_New();

    try {
      const_ITERATE(TMetaValues, mi, ex->meta) {
        PyObject *key;
        PVariable variable = dom.getMetaVar(mi->first, false);

        if (keytype == &PyInt_Type)
          key = PyInt_FromLong(mi->first);
        else {
          if (!variable)
            continue;
          if (keytype == &PyString_Type)
            key = PyString_FromString(variable->name.c_str());
          else
            key = WrapOrange(variable);
        }

        PyObject *value = Value_FromVariableValue(variable, mi->second);
        PyDict_SetItem(res, key, value);
        Py_DECREF(key);
        Py_DECREF(value);
      }

      return res;
    }
    catch (...) {
      Py_DECREF(res);
      throw;
    }

  PyCATCH

}


PyObject *Example_hasmeta(TPyExample *pex, PyObject *index) PYARGS(METH_O, "(id | var) -> bool")
{ PyTRY
    PVariable var;
    int idx = getMetaIdFromPy(PyExample_AS_Example(pex), index, var);
    PyErr_Clear();
    return PyInt_FromLong(idx && PyExample_AS_Example(pex)->hasMeta(idx) ? 1 : 0);
  PyCATCH
}


PyObject *Example_setvalue(TPyExample *pex, PyObject *vala) PYARGS(METH_O, "(Value) -> None")
{ PyTRY
    if (!PyOrValue_Check(vala))
      PYERROR(PyExc_TypeError, "Example.setvalue: orange.Value expected", PYNULL);

    PVariable var = PyValue_AS_Variable(vala);
    if (!var)
      PYERROR(PyExc_TypeError, "Example.setvalue: values 'variable' should not be None", PYNULL);

    PExample example=PyExample_AS_Example(pex);
    int idx = example->domain->getVarNum(var);

    if (idx>=0)
      example->operator[](idx) = PyValue_AS_Value(vala);
    else
      example->setMeta(idx, PyValue_AS_Value(vala));

    RETURN_NONE;
  PyCATCH
}


PyObject *Example_setmeta(TPyExample *pex, PyObject *args) PYARGS(METH_VARARGS, "(Value, int) | (variable, value); Sets a meta-value")
{ PyTRY
    PExample example=PyExample_AS_Example(pex);
  
    PyObject *par1, *par2=PYNULL;
    if (!PyArg_ParseTuple(args, "O|O", &par1, &par2))
      PYERROR(PyExc_TypeError, "invalid arguments", PYNULL);

    int idx=0;

    if (PyOrValue_Check(par1)) {
      // first parameter is a PyValue
      // second parameter is an index and is accepted iff variable not among domain's meta variables
      TMetaDescriptor *desc=PyValue_AS_Variable(par1) ? example->domain->metas[PyValue_AS_Variable(par1)] : (TMetaDescriptor *)NULL;
      if (desc) 
        if (par2)
          PYERROR(PyExc_TypeError, "second argument (index) not expected", PYNULL)
        else
          idx=desc->id;
      else
       if (!par2)
         PYERROR(PyExc_TypeError, "second argument (index) needed", PYNULL)
       else
         if (!PyInt_Check(par2))
           PYERROR(PyExc_TypeError, "invalid index type (int expected)", PYNULL)
         else {
           idx = int(PyInt_AsLong(par2));
           if (idx>=0)
             PYERROR(PyExc_TypeError, "invalid meta-id index (negative integer expected)", PYNULL);
         }

      example->setMeta(idx, PyValue_AS_Value(par1));
    }

    else if (!par2)
      PYERROR(PyExc_TypeError, "invalid arguments (second argument missing or the first is of wrong type)", PYNULL)

    else if (PyOrVariable_Check(par1) || PyString_Check(par1) || PyInt_Check(par1)) {
      // first parameter will denote the variable, the second the value
      int idx;
      PVariable var;

      if (PyInt_Check(par1)) {
        idx = PyInt_AsLong(par1);
        TMetaDescriptor *desc=example->domain->metas[idx];
        if (desc)
          var = desc->variable;
      }

      else {
        TMetaDescriptor *desc=example->domain->metas[
            PyOrVariable_Check(par1) ? PyOrange_AsVariable(par1)->name
                                     : string(PyString_AsString(par1))];
        if (!desc)
          PYERROR(PyExc_TypeError, "invalid variable", PYNULL);
        idx = desc->id;
        var = desc->variable;
      }

      if (idx>=0)
        PYERROR(PyExc_TypeError, "invalid meta-id index (negative integer expected)", PYNULL);

      TValue val;
      if (!convertFromPython(par2, val, var))
        return PYNULL;
      example->setMeta(idx, val);
    }

    else
      PYERROR(PyExc_TypeError, "invalid arguments", PYNULL)

    RETURN_NONE;
  PyCATCH
}
  

PyObject *Example_removemeta(TPyExample *pex, PyObject *index) PYARGS(METH_O, "(id); Removes a meta-value")
{ PyTRY
    PVariable var;
    int idx = getMetaIdFromPy(PyExample_AS_Example(pex), index, var);
    if (!idx)
      return PYNULL; 

    PyExample_AS_Example(pex)->removeMeta(idx);
    RETURN_NONE;
  PyCATCH
}



PyObject *Example_getclass(TPyExample *pex) PYARGS(METH_NOARGS, "()  -> Value; Returns example's class")
{ PyTRY
      const TExample &example = PyExample_AS_ExampleReference(pex);
      const PVariable &classVar = example.domain->classVar;

      if (!classVar)
        raiseError("class-less domain");

      return Value_FromVariableValue(classVar, example.getClass());
  PyCATCH
}


PyObject *Example_setclass(TPyExample *pex, PyObject *val) PYARGS(METH_O, "(value); Sets example's class")
{ PyTRY
    PExample &example=PyExample_AS_Example(pex);
    PVariable &classVar = example->domain->classVar;

    if (!classVar)
      PYERROR(PyExc_SystemError, "classless domain", PYNULL);

    TValue value;
    if (!convertFromPython(val, value, classVar)) 
      return PYNULL;
    example->setClass(value);

    RETURN_NONE;
  PyCATCH
}


PyObject *Example_compatible(TPyExample *pex, PyObject *args) PYARGS(METH_VARARGS, "(example[, ignoreClass]); Returns true if examples are compatible")
{ PyTRY
    PExample example;
    int ic = 0;
    if (!PyArg_ParseTuple(args, "O&|i", cc_Example, &example, &ic))
      PYERROR(PyExc_TypeError, "example and, optionally, a flag for ignoring the class expected", PYNULL)

    return PyInt_FromLong(PyExample_AS_Example(pex)->compatible(example.getReference(), ic != 0) ? 1 : 0);
  PyCATCH
}


PyObject *PyExampleIter_New(TPyExample *);

PyObject *Example_iter(TPyExample *pex)
{ return PyExampleIter_New(pex);
}


int getAttributeIndex(PDomain domain, PyObject *vara)
{
    if (PyInt_Check(vara)) {
      int ind = int(PyInt_AsLong(vara));
      if (ind >= (int)(domain->variables->size())) {
        PyErr_Format(PyExc_IndexError, "index %i to large (> %i)", ind, domain->variables->size()-1);
        return ILLEGAL_INT;
      }

      // Exception: example[-1] gives class value
      return ind==-1 ? domain->variables->size()-1 : ind;
    }

    PVariable var=varFromArg_byDomain(vara, domain);
    if (!var) 
      PYERROR(PyExc_TypeError, "invalid arguments or unknown attribute name", ILLEGAL_INT);

    return domain->getVarNum(var);
}


PyObject *Example_getitem(TPyExample *pex, PyObject *vara)
{ PyTRY
    PExample example = PyExample_AS_Example(pex);

    int ind = getAttributeIndex(example->domain, vara);
    if (ind==ILLEGAL_INT)
      return PYNULL;

    /* getVar will return NULL if ind is meta-attribute not registered with the domain.
       That's OK - we don't need PVariable (Value_FromValue would do exactly the same).
       operator[] will raise an exception if meta-value is requested and the example
       doesn't have it. */
    return Value_FromVariableValue(example->domain->getVar(ind, false), example->operator[](ind));
  PyCATCH
}


int Example_setitem(TPyExample *pex, PyObject *vara, PyObject *vala)
{ PyTRY
    PExample example = PyExample_AS_Example(pex);
    const int ind = getAttributeIndex(example->domain, vara);
    if (ind==ILLEGAL_INT)
      return -1;

    PVariable var = example->domain->getVar(ind, false);

    if (PyOrValue_Check(vala)) {
      if (PyValue_AS_Variable(vala) && var && (PyValue_AS_Variable(vala)!=var)) {
          string vals;
          PyValue_AS_Variable(vala)->val2str(PyValue_AS_Value(vala), vals);
          if (ind>=0)
            var->str2val(vals, example->operator[](ind));
          else {
            TValue val;
            var->str2val(vals, val);
            example->setMeta(ind, val);
          }
        }
      else {
        if (ind>=0)
          example->operator[](ind) = PyValue_AS_Value(vala);
        else
          example->setMeta(ind, PyValue_AS_Value(vala));
      }
    }

    else {
      TValue value;
      if (!convertFromPython(vala, value, var)) 
        return -1;
      if (ind>=0)
        example->operator[](ind) = value;
      else
        example->setMeta(ind, value);
    }

    return 0;

  PyCATCH_1
}



inline PyObject *toValue(const TValue &val, PVariable var, int natvt, PyObject *forDK, PyObject *forDC, PyObject *forSpecial)
{ switch (natvt) {
    case -1:
      return (val.varType==TValue::INTVAR)
            ? PyInt_FromLong(long(val.intV))
            : PyFloat_FromDouble(double(val.floatV));

    case  0:
      if (val.isSpecial()) {
        PyObject *res;
        if (val.isDC())
          res = forDC;
        else if (val.isDK())
          res = forDK;
        else
          res = forSpecial;
        Py_INCREF(res);
        return res;
      }   
      else
        return convertToPythonNative(val, var);

    default:
      return Value_FromVariableValue(var, val);
  }
}

PyObject *convertToPythonNative(const TExample &example, int natvt, bool tuples, PyObject *forDK, PyObject *forDC, PyObject *forSpecial)
{
  if (forDK)
    Py_INCREF(forDK);
  else
    forDK = PyString_FromString("?");

  if (forDC)
    Py_INCREF(forDC);
  else
    forDC = PyString_FromString("~");

  if (forSpecial)
    Py_INCREF(forSpecial);

⌨️ 快捷键说明

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