120: #line 1453 "mxTools.pak" 121: def forall(condition,sequence): 122: return count(condition, sequence) == len(sequence) 123:
1243: #line 1457 "mxTools.pak" 1244: 1245: Py_C_Function( mxTools_forall, 1246: "forall(condition,sequence)\n\n" 1247: "Return 1 if and only if condition is true for all\n" 1248: "of the items in sequence and 0 otherwise. condition\n" 1249: "must be a callable object.") 1250: { 1251: PyObject *condition; 1252: PyObject *list; 1253: PyObject *argtuple = 0; 1254: register int i; 1255: int n; 1256: int length; 1257: 1258: Py_Get2Args("OO",condition,list); 1259: 1260: length = PySequence_Length(list); 1261: if (length < 0) 1262: Py_Error(PyExc_TypeError, 1263: "second argument must be a sequence"); 1264: 1265: argtuple = PyTuple_New(1); 1266: if (!argtuple) 1267: goto onError; 1268: 1269: for(i = 0, n = 1; i < length; i++) { 1270: register PyObject *v; 1271: register PyObject *w; 1272: 1273: v = PySequence_GetItem(list,i); 1274: if (!v) 1275: goto onError; 1276: 1277: /* Replace the argtuple entry with the new item */ 1278: Py_XDECREF(PyTuple_GET_ITEM(argtuple,0)); 1279: PyTuple_SET_ITEM(argtuple,0,v); 1280: 1281: /* Let's see what condition thinks about this item */ 1282: w = PyEval_CallObject(condition,argtuple); 1283: if (!w) 1284: goto onError; 1285: if (!PyObject_IsTrue(w)) { 1286: n = 0; 1287: Py_DECREF(w); 1288: break; 1289: } 1290: Py_DECREF(w); 1291: } 1292: 1293: Py_DECREF(argtuple); 1294: return PyInt_FromLong((long)n); 1295: onError: 1296: Py_XDECREF(argtuple); 1297: return NULL; 1298: } 1299: