source: orange/source/pyxtract/defvectors.py @ 10668:6d3d21a83558

Revision 10668:6d3d21a83558, 10.3 KB checked in by Janez Demšar <janez.demsar@…>, 2 years ago (diff)

Added 'objects' as a recognized attribute of IntList (fixes #1156)

Line 
1### writes a text file with code that defines methods for sequence slots
2
3from pyprops import ClassDefinition
4classes = {None: None}
5
6definition ="""
7$wrappedlistname$ P$pyname$_FromArguments(PyObject *arg) { return $classname$::P_FromArguments(arg); }
8PyObject *$pyname$_FromArguments(PyTypeObject *type, PyObject *arg) { return $classname$::_FromArguments(type, arg); }
9PyObject *$pyname$_new(PyTypeObject *type, PyObject *arg, PyObject *kwds) BASED_ON(Orange$displayname, "(<list of $pyelement$>)") ALLOWS_EMPTY { return $classname$::_new(type, arg, kwds); }
10PyObject *$pyname$_getitem_sq(TPyOrange *self, Py_ssize_t index) { return $classname$::_getitem(self, index); }
11int       $pyname$_setitem_sq(TPyOrange *self, Py_ssize_t index, PyObject *item) { return $classname$::_setitem(self, index, item); }
12PyObject *$pyname$_getslice(TPyOrange *self, Py_ssize_t start, Py_ssize_t stop) { return $classname$::_getslice(self, start, stop); }
13int       $pyname$_setslice(TPyOrange *self, Py_ssize_t start, Py_ssize_t stop, PyObject *item) { return $classname$::_setslice(self, start, stop, item); }
14Py_ssize_t       $pyname$_len_sq(TPyOrange *self) { return $classname$::_len(self); }
15PyObject *$pyname$_richcmp(TPyOrange *self, PyObject *object, int op) { return $classname$::_richcmp(self, object, op); }
16PyObject *$pyname$_concat(TPyOrange *self, PyObject *obj) { return $classname$::_concat(self, obj); }
17PyObject *$pyname$_repeat(TPyOrange *self, Py_ssize_t times) { return $classname$::_repeat(self, times); }
18PyObject *$pyname$_str(TPyOrange *self) { return $classname$::_str(self); }
19PyObject *$pyname$_repr(TPyOrange *self) { return $classname$::_str(self); }
20int       $pyname$_contains(TPyOrange *self, PyObject *obj) { return $classname$::_contains(self, obj); }
21PyObject *$pyname$_append(TPyOrange *self, PyObject *item) PYARGS(METH_O, "($pyelement$) -> None") { return $classname$::_append(self, item); }
22PyObject *$pyname$_extend(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "(sequence) -> None") { return $classname$::_extend(self, obj); }
23PyObject *$pyname$_count(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "($pyelement$) -> int") { return $classname$::_count(self, obj); }
24PyObject *$pyname$_filter(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "([filter-function]) -> $pyname$") { return $classname$::_filter(self, args); }
25PyObject *$pyname$_index(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "($pyelement$) -> int") { return $classname$::_index(self, obj); }
26PyObject *$pyname$_insert(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "(index, item) -> None") { return $classname$::_insert(self, args); }
27PyObject *$pyname$_native(TPyOrange *self) PYARGS(METH_NOARGS, "() -> list") { return $classname$::_native(self); }
28PyObject *$pyname$_pop(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "() -> $pyelement$") { return $classname$::_pop(self, args); }
29PyObject *$pyname$_remove(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "($pyelement$) -> None") { return $classname$::_remove(self, obj); }
30PyObject *$pyname$_reverse(TPyOrange *self) PYARGS(METH_NOARGS, "() -> None") { return $classname$::_reverse(self); }
31PyObject *$pyname$_sort(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "([cmp-func]) -> None") { return $classname$::_sort(self, args); }
32PyObject *$pyname$__reduce__(TPyOrange *self, PyObject *) { return $classname$::_reduce(self); }
33"""
34
35wdefinition = "\nextern PyTypeObject PyOr$pyelement$_Type_inh;\n" + definition
36
37udefinition = """
38bool convertFromPython(PyObject *, $elementname$ &);
39PyObject *convertToPython(const $elementname$ &);
40""" \
41+ definition
42
43# removed from udefinition
44# #define $listname$ _TOrangeVector<$elementname$>
45# typedef GCPtr< $listname$ > $wrappedlistname$;
46
47outf = open("lib_vectors_auto.txt", "wt")
48
49def normalList(displayname, name, goesto):
50  return (displayname,) + tuple([x % name for x in ("%sList", "%s", "P%sList", "T%sList", "P%s")] + [goesto])
51
52
53#  list name in Python,    element name in Py, wrapped list name in C, list name in C,         list element name in C, interface file
54for (displayname, pyname, pyelementname, wrappedlistname, listname, elementname, goesto) in \
55  [("",                               "ValueList",           "Value",            "PValueList",           "TValueList",           "TValue",               "cls_value.cpp"),
56   ("Orange.data.variable.Variables", "VarList",             "Variable",         "PVarList",             "TVarList",             "PVariable",            "lib_kernel.cpp"),
57   ("",                               "VarListList",         "VarList",          "PVarListList",         "TVarListList",         "PVarList",             "lib_kernel.cpp"),
58   ("",                               "DomainDistributions", "Distribution",     "PDomainDistributions", "TDomainDistributions", "PDistribution",        "lib_kernel.cpp"),
59   normalList("", "Distribution", "lib_kernel.cpp"),
60   normalList("", "ExampleGenerator", "lib_kernel.cpp"),
61   normalList("Orange.classification.ClassifierList", "Classifier", "lib_kernel.cpp"),
62   
63   ("",                               "DomainBasicAttrStat", "BasicAttrStat",    "PDomainBasicAttrStat", "TDomainBasicAttrStat", "PBasicAttrStat",       "lib_components.cpp"),
64   ("",                               "DomainContingency",   "Contingency",      "PDomainContingency",   "TDomainContingency",   "PContingencyClass",    "lib_components.cpp"),
65   normalList("", "ValueFilter", "lib_components.cpp"),
66   normalList("", "Filter", "lib_components.cpp"),
67   normalList("Orange.clustering.hierarchical.HierarchicalClusterList", "HierarchicalCluster", "lib_components.cpp"),
68   
69   ("Orange.associate.AssociationRules",  "AssociationRules", "AssociationRule",  "PAssociationRules",    "TAssociationRules",    "PAssociationRule",     "lib_learner.cpp"),
70   normalList("Orange.classification.tree.NodeList", "TreeNode", "lib_learner.cpp"),
71   normalList("Orange.classification.tree.C45NodeList", "C45TreeNode", "lib_learner.cpp"),
72   normalList("Orange.classification.rules.RuleList", "Rule", "lib_learner.cpp"),
73   normalList("", "ConditionalProbabilityEstimator", "lib_components.cpp"),
74   normalList("", "ProbabilityEstimator", "lib_components.cpp"),
75   normalList("", "EVDist", "lib_learner.cpp"),
76
77   normalList("", "Heatmap", "orangene.cpp"),
78   normalList("", "SOMNode", "som.cpp")
79   ]:
80  outf.write("**** This goes to '%s' ****\n" % goesto)
81  outf.write(wdefinition.replace("$displayname", ((" - "+displayname) if displayname else ""))
82                        .replace("$pyname$", pyname)
83                        .replace("$classname$", "ListOfWrappedMethods<%s, %s, %s, &PyOr%s_Type>" % (wrappedlistname, listname, elementname, pyelementname))
84                        .replace("$pyelement$", pyelementname)
85                        .replace("$wrappedlistname$", wrappedlistname)
86                        .replace(">>", "> >")
87             +"\n\n"
88            )
89
90  classes[listname] = ClassDefinition(listname, "TOrange") 
91
92
93
94coutf = open("lib_vectors.cpp", "wt")
95
96coutf.write("""/*
97    This file is part of Orange.
98
99    Orange is free software; you can redistribute it and/or modify
100    it under the terms of the GNU General Public License as published by
101    the Free Software Foundation; either version 2 of the License, or
102    (at your option) any later version.
103
104    Orange is distributed in the hope that it will be useful,
105    but WITHOUT ANY WARRANTY; without even the implied warranty of
106    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
107    GNU General Public License for more details.
108
109    You should have received a copy of the GNU General Public License
110    along with Orange; if not, write to the Free Software
111    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
112
113    Authors: Janez Demsar, Blaz Zupan, 1996--2002
114    Contact: janez.demsar@fri.uni-lj.si
115*/
116
117
118#include "orvector.hpp"
119#include "cls_orange.hpp"
120#include "vectortemplates.hpp"
121#include "externs.px"
122
123#include "distance_dtw.hpp"
124""")
125
126for (pyname, pyelementname, wrappedlistname, listname, elementname, wrapped) in \
127  [("BoolList",         "bool",     "PBoolList",         "TBoolList",         "bool", 0),
128   ("IntList",          "int",      "PIntList",          "TIntList",          "int", 0),
129   ("FloatList",        "float",    "PFloatList",        "TFloatList",        "float", 0),
130   ("FloatListList",    "FloatList","PFloatListList",    "TFloatListList",    "PFloatList", 1),
131   ("StringList",       "string",   "PStringList",       "TStringList",       "string", 0),
132   ("LongList",         "int",      "PLongList",         "TLongList",         "long", 0),
133   ("_Filter_index",     "int",     "PFilter_index",     "TFilter_index",     "FOLDINDEXTYPE", 0),
134   ("AlignmentList",    "Alignment", "PAlignmentList",  "TAlignmentList",    "TAlignment", 0),
135
136   ("IntFloatList",     "tuple(int, float)",   "PIntFloatList",    "TIntFloatList",     "pair<int, float>", 0),
137   ("FloatFloatList",   "tuple(float, float)", "PFloatFloatList",  "TFloatFloatList",   "pair<float, float>", 0),
138   ]:
139  if (pyname[0]=="_"):
140    pyname = pyname[1:]
141    outfile=outf
142  else:
143    outfile=coutf
144  if wrapped:
145    classname = "ListOfWrappedMethods<%s, %s, %s, &PyOr%s_Type>" % (wrappedlistname, listname, elementname, pyelementname)
146  else:
147    classname = "ListOfUnwrappedMethods<%s, %s, %s>" % (wrappedlistname, listname, elementname)
148
149  outfile.write((wrapped and wdefinition or udefinition)
150                       .replace("$displayname", " - Orange.core."+pyname)
151                       .replace("$pyname$", pyname)
152                       .replace("$classname$", classname)
153                       .replace("$pyelement$", pyelementname)
154                       .replace("$wrappedlistname$", wrappedlistname)
155                       .replace("$listname$", listname)
156                       .replace("$elementname$", elementname)
157                       .replace(">>", "> >")
158             +"\n\n"
159            )
160
161  classes[listname] = ClassDefinition(listname, "TOrange")
162
163coutf.write("""// I hate to do this for *all* IntLists, but see no other way around
164RECOGNIZED_ATTRIBUTES(IntList, "objects");
165
166#include "lib_vectors.px"
167""")
168
169outf.close()
170coutf.close()
171
172import pickle
173
174classes["TAttributedFloatList"] = ClassDefinition("TAttributedFloatList", "TFloatList")
175classes["TAttributedBoolList"] = ClassDefinition("TAttributedBoolList", "TBoolList")
176
177import os
178if not os.path.exists("../orange/ppp"):
179  os.mkdir("../orange/ppp")
180pickle.dump(classes, file("../orange/ppp/lists", "wt"))
Note: See TracBrowser for help on using the repository browser.