116: #line 1391 "mxTools.pak" 117: def exists(condition,sequence): 118: return count(condition, sequence) != 0 119:
1186: #line 1395 "mxTools.pak" 1187: 1188: Py_C_Function( mxTools_exists, 1189: "exists(condition,sequence)\n" 1190: "Return 1 if and only if condition is true for at least one\n" 1191: "of the items in sequence and 0 otherwise. condition\n" 1192: "must be a callable object.") 1193: { 1194: PyObject *condition; 1195: PyObject *list; 1196: PyObject *argtuple = 0; 1197: register int i; 1198: int n; 1199: int length; 1200: 1201: Py_Get2Args("OO",condition,list); 1202: 1203: length = PySequence_Length(list); 1204: if (length < 0) 1205: Py_Error(PyExc_TypeError, 1206: "second argument must be a sequence"); 1207: 1208: argtuple = PyTuple_New(1); 1209: if (!argtuple) 1210: goto onError; 1211: 1212: for(i = 0, n = 0; i < length; i++) { 1213: register PyObject *v; 1214: register PyObject *w; 1215: 1216: v = PySequence_GetItem(list,i); 1217: if (!v) 1218: goto onError; 1219: 1220: /* Replace the argtuple entry with the new item */ 1221: Py_XDECREF(PyTuple_GET_ITEM(argtuple,0)); 1222: PyTuple_SET_ITEM(argtuple,0,v); 1223: 1224: /* Let's see what condition thinks about this item */ 1225: w = PyEval_CallObject(condition,argtuple); 1226: if (!w) 1227: goto onError; 1228: if (PyObject_IsTrue(w)) { 1229: n = 1; 1230: Py_DECREF(w); 1231: break; 1232: } 1233: Py_DECREF(w); 1234: } 1235: 1236: Py_DECREF(argtuple); 1237: return PyInt_FromLong((long)n); 1238: onError: 1239: Py_XDECREF(argtuple); 1240: return NULL; 1241: } 1242: