74: #line 988 "mxTools.pak" 75: def ifilter(condition, object, indices = NotGiven): 76: if indices is NotGiven: indices = trange(len(object)) 77: alist = [] 78: for index in indices: 79: value = object[index] 80: if condition(value): 81: alist.append((index,value)) 82: return alist 83:
829: #line 998 "mxTools.pak" 830: 831: Py_C_Function( mxTools_ifilter, 832: "ifilter(condition,object[,indices])\n\n" 833: "Returns a list of tuples (index,object[index]) such that\n" 834: "condition(object[item]) is true and index is found in\n" 835: "the sequence indices (defaulting to indices(object)).\n" 836: "Order is preserved. condition must be a callable object.") 837: { 838: int n; 839: register int index; 840: PyObject *t = 0; 841: PyObject *w; 842: PyObject *indices = 0; 843: PyObject *condition; 844: PyObject *argtuple = 0; 845: 846: Py_Get3Args("OO|O",condition,w,indices); 847: 848: if (!indices) { 849: n = PyObject_Length(w); 850: if (n < 0) 851: Py_Error(PyExc_TypeError, 852: "second argument must be have a __len__ method"); 853: } 854: else { 855: n = PyObject_Length(indices); 856: if (n < 0) 857: Py_Error(PyExc_TypeError, 858: "third argument must be a sequence"); 859: } 860: 861: t = PyList_New(0); 862: if (!t) 863: goto onError; 864: 865: argtuple = PyTuple_New(1); 866: if (!argtuple) 867: goto onError; 868: 869: if (!indices) 870: for (index = 0; index < n; index++) { 871: register PyObject *v; 872: register PyObject *x; 873: register PyObject *z; 874: 875: v = PyInt_FromLong((long)index); 876: if (!v) 877: goto onError; 878: x = PyObject_GetItem(w,v); 879: if (!x) { 880: Py_DECREF(v); 881: goto onError; 882: } 883: /* Replace the argtuple entry with the new item x */ 884: Py_XDECREF(PyTuple_GET_ITEM(argtuple,0)); 885: PyTuple_SET_ITEM(argtuple,0,x); 886: /* Add a tuple if condition says it's ok */ 887: z = PyEval_CallObject(condition,argtuple); 888: if (!z) 889: goto onError; 890: if (PyObject_IsTrue(z)) { 891: register PyObject *u; 892: 893: u = PyTuple_New(2); 894: if (!u) { 895: Py_DECREF(v); 896: Py_DECREF(z); 897: goto onError; 898: } 899: Py_INCREF(x); 900: PyTuple_SET_ITEM(u,0,v); 901: PyTuple_SET_ITEM(u,1,x); 902: PyList_Append(t,u); 903: Py_DECREF(u); 904: } 905: else 906: Py_DECREF(v); 907: Py_DECREF(z); 908: } 909: else 910: for (index = 0; index < n; index++) { 911: register PyObject *v; 912: register PyObject *x; 913: register PyObject *z; 914: 915: v = PySequence_GetItem(indices,index); 916: if (!v) 917: goto onError; 918: x = PyObject_GetItem(w,v); 919: if (!x) { 920: Py_DECREF(v); 921: goto onError; 922: } 923: /* Replace the argtuple entry with the new item x */ 924: Py_XDECREF(PyTuple_GET_ITEM(argtuple,0)); 925: PyTuple_SET_ITEM(argtuple,0,x); 926: /* Add a tuple if condition says it's ok */ 927: z = PyEval_CallObject(condition,argtuple); 928: if (!z) 929: goto onError; 930: if (PyObject_IsTrue(z)) { 931: register PyObject *u; 932: 933: u = PyTuple_New(2); 934: if (!u) { 935: Py_DECREF(v); 936: Py_DECREF(z); 937: goto onError; 938: } 939: Py_INCREF(x); 940: PyTuple_SET_ITEM(u,0,v); 941: PyTuple_SET_ITEM(u,1,x); 942: PyList_Append(t,u); 943: Py_DECREF(u); 944: } 945: else 946: Py_DECREF(v); 947: Py_DECREF(z); 948: } 949: 950: Py_DECREF(argtuple); 951: return t; 952: 953: onError: 954: Py_XDECREF(argtuple); 955: Py_XDECREF(t); 956: return NULL; 957: } 958: