6.2.4.2.9. extract

Comienzo python section to interscript/core/mxTools.py[10 /26 ] Siguiente Previo Primero Último
    58: #line 887 "mxTools.pak"
    59: def extract (object, indices, defaults = NotGiven):
    60:   alist = []
    61:   seq = 0
    62:   for index in indices:
    63:     try:
    64:       alist.append(object[index])
    65:     except KeyboardInterrupt: raise
    66:     except:
    67:       if defaults is not NotGiven:
    68:         alist.append(defaults[seq])
    69:       else:
    70:         raise IndexError,"mxTools: extract: default not supplied"
    71:     seq = seq + 1
    72:   return alist
    73: 
End python section to interscript/core/mxTools.py[10]
Comienzo C section to interscript/core/mxTools.c[11 /30 ] Siguiente Previo Primero Último
   745: #line 903 "mxTools.pak"
   746: 
   747: Py_C_Function( mxTools_extract,
   748:                "extract(object,indices[,defaults])\n\n"
   749:                "Returns a list of entries object[index] for each index\n"
   750:                "in the sequence indices. defaults must have the same length\n"
   751:                "as indices and is used to provide default values in case\n"
   752:                "the lookup fails.")
   753: {
   754:     register int n = 0;
   755:     register int index;
   756:     PyObject *t = 0;
   757:     PyObject *w;
   758:     PyObject *indices;
   759:     PyObject *defaults = 0;
   760: 
   761:     Py_Get3Args("OO|O",w,indices,defaults);
   762: 
   763:     n = PyObject_Length(indices);
   764:     if (n < 0)
   765:         Py_Error(PyExc_TypeError,
   766:                  "second argument must be a sequence");
   767: 
   768:     t = PyList_New(n);
   769:     if (!t)
   770:         goto onError;
   771: 
   772:     if (defaults)
   773:         for (index = 0; index < n; index++) {
   774:             register PyObject *x;
   775:             register PyObject *v;
   776: 
   777:             v = PySequence_GetItem(indices,index);
   778:             if (!v) {
   779:                 PyErr_Format(PyExc_IndexError,
   780:                              "index value nr. %i not accessible",
   781:                              index);
   782:                 goto onError;
   783:             }
   784:             x = PyObject_GetItem(w,v);
   785:             Py_DECREF(v);
   786:             if (!x) {
   787:                 /* Use default value */
   788:                 PyErr_Clear();
   789:                 x = PySequence_GetItem(defaults,index);
   790:                 if (!x) {
   791:                     PyErr_Format(PyExc_IndexError,
   792:                               "default value for index nr. %i not accessible",
   793:                                  index);
   794:                     goto onError;
   795:                 }
   796:             }
   797:             PyList_SET_ITEM(t,index,x);
   798:         }
   799:     else
   800:         for (index = 0; index < n; index++) {
   801:             register PyObject *x;
   802:             register PyObject *v;
   803: 
   804:             v = PySequence_GetItem(indices,index);
   805:             if (!v) {
   806:                 PyErr_Format(PyExc_IndexError,
   807:                              "index value nr. %i not accessible",
   808:                              index);
   809:                 goto onError;
   810:             }
   811:             x = PyObject_GetItem(w,v);
   812:             Py_DECREF(v);
   813:             if (!x) {
   814:                 PyErr_Format(PyExc_IndexError,
   815:                              "default value for index nr. %i not accessible",
   816:                              index);
   817:                 goto onError;
   818:             }
   819:             PyList_SET_ITEM(t,index,x);
   820:         }
   821: 
   822:     return t;
   823: 
   824:  onError:
   825:     Py_XDECREF(t);
   826:     return NULL;
   827: }
   828: 
End C section to interscript/core/mxTools.c[11]