📄 cls_example.cpp
字号:
else
forSpecial = PyString_FromString(".");
PyObject *list=PyList_New(0);
TExample::const_iterator ei=example.begin();
const_PITERATE(TVarList, vi, example.domain->attributes)
PyList_Append(list, toValue(*(ei++), *vi, natvt, forDK, forDC, forSpecial));
PyObject *res;
if (example.domain->classVar) {
PyObject *pyclass = toValue(example.getClass(), example.domain->classVar, natvt, forDK, forDC, forSpecial);
if (tuples)
res = Py_BuildValue("NN", list, pyclass);
else {
PyList_Append(list, pyclass);
res = list;
}
}
else {
if (tuples)
res = Py_BuildValue("NO", list, Py_None);
else
res = list;
}
Py_DECREF(forDK);
Py_DECREF(forDC);
Py_DECREF(forSpecial);
return res;
}
PyObject *Example_native(TPyExample *pex, PyObject *args, PyObject *keyws) PYARGS(METH_VARARGS | METH_KEYWORDS, "([nativity, tuple=, substituteDC=, substituteDK=, substituteOther=]) -> list; Converts an example to a list")
{ PyTRY
int natvt=1;
if (args && !PyArg_ParseTuple(args, "|i", &natvt))
PYERROR(PyExc_TypeError, "invalid arguments (no arguments or an integer expected)", PYNULL);
bool tuples = false;
PyObject *pytuples = keyws ? PyDict_GetItemString(keyws, "tuple") : PYNULL;
tuples = pytuples && (PyObject_IsTrue(pytuples) != 0);
PyObject *forDC = keyws ? PyDict_GetItemString(keyws, "substituteDC") : PYNULL;
PyObject *forDK = keyws ? PyDict_GetItemString(keyws, "substituteDK") : PYNULL;
PyObject *forSpecial = keyws ? PyDict_GetItemString(keyws, "substituteOther") : PYNULL;
return convertToPythonNative(PyExample_AS_ExampleReference(pex), natvt, tuples, forDK, forDC, forSpecial);
PyCATCH
}
#include "slist.hpp"
void Example_pack(const TExample &example, TCharBuffer &buf, PyObject *&otherValues)
{
for(TValue *vali = example.values; vali != example.values_end; vali++)
Value_pack(*vali, buf, otherValues);
buf.writeInt(example.meta.size() | (example.name ? 1<<31 : 0));
if (example.name) {
if (!otherValues)
otherValues = PyList_New(0);
PyObject *pyname = PyString_FromString(example.name->c_str());
PyList_Append(otherValues, pyname);
Py_DECREF(pyname);
}
const_ITERATE(TMetaValues, mi, example.meta) {
buf.writeInt((int)(mi->first));
buf.writeChar(mi->second.varType);
Value_pack(mi->second, buf, otherValues);
}
}
void Example_unpack(TExample &example, TCharBuffer &buf, PyObject *&otherValues, int &otherValuesIndex)
{
TVarList::const_iterator vi = example.domain->variables->begin();
for(TValue *vali = example.values; vali != example.values_end; vali++, vi++) {
vali->varType = (*vi)->varType;
Value_unpack(*vali, buf, otherValues, otherValuesIndex);
}
int nMetas = buf.readInt();
if (nMetas & (1<<31)) {
PyObject *pyname = PyList_GetItem(otherValues, otherValuesIndex++);
example.name = new string(PyString_AsString(pyname));
}
for(int i = nMetas & 0x7fffffff; i--; ) {
const int id = buf.readInt();
TValue val((const unsigned char &)(buf.readChar()));
Value_unpack(val, buf, otherValues, otherValuesIndex);
example.meta.setValue(id, val);
}
}
PyObject *Example__reduce__(TPyExample *pex)
{
PyTRY
if (pex->lock)
PYERROR(PyExc_TypeError, "examples that reference tables cannot be pickled", PYNULL);
TExample &example = PyExample_AS_ExampleReference(pex);
TCharBuffer buf(1024);
PyObject *otherValues = NULL;
Example_pack(example, buf, otherValues);
if (!otherValues) {
otherValues = Py_None;
Py_INCREF(otherValues);
}
if (pex->example.counter->orange_dict)
return Py_BuildValue("O(Ns#N)O", getExportedFunction("__pickleLoaderExample"),
WrapOrange(example.domain),
buf.buf, buf.length(),
otherValues,
pex->example.counter->orange_dict);
else
return Py_BuildValue("O(Ns#N)", getExportedFunction("__pickleLoaderExample"),
WrapOrange(example.domain),
buf.buf, buf.length(),
otherValues);
PyCATCH
}
PyObject *__pickleLoaderExample(PyObject *, PyObject *args) PYARGS(METH_VARARGS, "(domain, packed_values, other_values)")
{
PyTRY
PDomain domain;
char *pbuf;
int bufSize;
PyObject *otherValues;
if (!PyArg_ParseTuple(args, "O&s#O:__pickleLoaderExample", cc_Domain, &domain, &pbuf, &bufSize, &otherValues))
return NULL;
TExample *newEx = new TExample(domain);
PExample wex = newEx;
int otherValuesIndex = 0;
TCharBuffer buf(pbuf);
Example_unpack(*newEx, buf, otherValues, otherValuesIndex);
return Example_FromWrappedExample(wex);
PyCATCH
}
inline void addValue(string &res, const TValue &val, PVariable var)
{ string str;
var->val2str(val, str);
if (var->varType!=TValue::FLOATVAR) res+="'"+str+"'";
else res+=str;
}
string TPyExample2string(TPyExample *pex)
{ PExample example = PyExample_AS_Example(pex);
string res("[");
TVarList::iterator vi(example->domain->variables->begin());
PITERATE(TExample, ei, example) {
if (ei!=example->begin())
res+=", ";
addValue(res, *ei, *(vi++));
}
res+="]";
int madded=0;
ITERATE(TMetaValues, mi, example->meta) {
res+= (madded++) ? ", " : ", {";
TMetaDescriptor *desc=example->domain->metas[(*mi).first];
if (desc) {
res+="\""+desc->variable->name+"\":";
addValue(res, (*mi).second, desc->variable);
}
else
if ((*mi).second.varType==TValue::FLOATVAR) {
char buf[128];
sprintf(buf, "%i:%.2f", int((*mi).first), (*mi).second.floatV);
res += buf;
}
else
res+="???";
}
if (madded) res+="}";
return res;
}
PyObject *Example_repr(TPyExample *pex)
{ PyTRY
return PyString_FromString(TPyExample2string(pex).c_str());
PyCATCH
}
PyObject *Example_str(TPyExample *pex)
{ PyTRY
return PyString_FromString(TPyExample2string(pex).c_str());
PyCATCH
}
PyObject *Example_get_domain(TPyExample *self)
{ PyTRY
return WrapOrange(PyExample_AS_Example(self)->domain);
PyCATCH
}
int Example_cmp(TPyExample *one, TPyExample *another)
{ PyTRY
return PyExample_AS_Example(one)->compare(PyExample_AS_ExampleReference(another));
PyCATCH_1
}
int Example_hash(TPyExample *ex)
{ PyTRY
return PyExample_AS_Example(ex)->sumValues();
PyCATCH_1
}
int Example_len(TPyExample *pex)
{ PyTRY
return PyExample_AS_Example(pex)->domain->variables->size();
PyCATCH_1
}
PyObject *Example_getattr(TPyExample *self, PyObject *name)
{
if (!PyString_Check(name) || strcmp(PyString_AsString(name), "name"))
return PyObject_GenericGetAttr((PyObject *)self, name);
const string *ename = self->example->name;
return PyString_FromString(ename ? ename->c_str() : "");
}
int Example_setattr(TPyExample *self, PyObject *name, PyObject *text)
{
if (!PyString_Check(name) || strcmp(PyString_AsString(name), "name"))
return PyObject_GenericSetAttr((PyObject *)self, name, text);
string *&ename = self->example->name;
if (text == Py_None) {
if (ename) {
delete ename;
ename = NULL;
}
return 0;
}
if (PyString_Check(text)) {
if (ename)
delete ename;
ename = new string(PyString_AsString(text));
return 0;
}
PYERROR(PyExc_AttributeError, "Example.name must be a string", -1);
}
extern PyTypeObject PyExampleIter_Type;
class TPyExampleIter {
public:
PyObject_HEAD
long index;
TPyExample *example; /* Set to NULL when iterator is exhausted */
};
PyObject *PyExampleIter_New(TPyExample *ex)
{
TPyExampleIter *self = PyObject_GC_New(TPyExampleIter, &PyExampleIter_Type);
if (self == NULL)
return NULL;
self->index = 0;
Py_INCREF(ex);
self->example = ex;
PyObject_GC_Track(self);
return (PyObject *)self;
}
static void PyExampleIter_Dealloc(TPyExampleIter *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->example);
PyObject_GC_Del(self);
}
static int PyExampleIter_Traverse(TPyExampleIter *self, visitproc visit, void *arg)
{
return self->example ? visit((PyObject *)(self->example), arg) : 0;
}
int PyExampleIter_Clear(TPyExampleIter *self)
{ Py_XDECREF((PyObject *)(self->example));
self->example = NULL;
return 0;
}
static PyObject *PyExampleIter_Iternext(TPyExampleIter *self)
{
if (!self->example)
return NULL;
if (self->index >= PyExample_AS_ExampleReference(self->example).domain->variables->size()) {
Py_DECREF(self->example);
self->example = NULL;
PYERROR(PyExc_StopIteration, "", PYNULL);
}
TExample &ex = PyExample_AS_ExampleReference(self->example);
PyObject *result = Value_FromVariableValue(ex.domain->getVar(self->index), ex[self->index]);
self->index++;
return result;
}
PyObject *PyExampleIter_Iter(PyObject *self)
{ Py_INCREF(self);
return self;
}
PyTypeObject PyExampleIter_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"orange.Example iterator",
sizeof(TPyExampleIter),
0,
(destructor)PyExampleIter_Dealloc,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
PyObject_GenericGetAttr,
0, 0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
0,
(traverseproc)PyExampleIter_Traverse,
(inquiry)PyExampleIter_Clear, 0, 0,
PyExampleIter_Iter,
(iternextfunc)PyExampleIter_Iternext,
0, 0, 0, 0, 0, 0, 0,
};
#include "cls_example.px"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -