def testGetFieldDescr(self): """Check the getFieldDescr function. """ fieldName = 'info' infoDescr = [fd for fd in nriterators.getFieldDescr(fieldName, self.descr)] self.assertEqual(infoDescr, self.infoDescr)
def testGetFieldDescr(self): """Check the getFieldDescr function. """ fieldName = 'info' infoDescr = [ fd for fd in nriterators.getFieldDescr(fieldName, self.descr) ] self.assertEqual(infoDescr, self.infoDescr)
def field(self, fieldName): """ Get field data as an array. `fieldName` can be the name or the index of a field in the record array. If it is not nested, a ``NumArray`` or ``CharArray`` object representing the values in that field is returned. Else, a `NestedRecArray` object is returned. `fieldName` can be used to provide the name of sub-fields. In that case, it will consist of several field name components separated by the string ``'/'``. For instance, if there is a nested field named ``x`` with a sub-field named ``y``, the last one can be accesed by using ``'x/y'`` as the value of `fieldName`. """ # fieldName can be an integer, get the corresponding name if isinstance(fieldName, int): fieldName = self.descr[fieldName][0] # The descr list of the field whose content is being extracted fieldDescr = [ item for item in nriterators.getFieldDescr(fieldName, self.descr) ] if fieldDescr == []: raise ValueError("there is no field named ``%s``" % (fieldName, )) fieldDescr = fieldDescr[0][1] # Case 1) non nested fields (bottom level) if isinstance(fieldDescr, str): # The field content is returned as numarray or chararray return self._flatArray.field(fieldName) # Case 2) nested fields (both top and intermediate levels) # We need fully qualified names to access the flat array fields fieldNames = [ name for name in nriterators.getNamesFromDescr(fieldDescr) ] flatNames = [name for name in nriterators.flattenNames(fieldNames)] # This is the flattened name of the original first bottom field. startField = '%s/%s' % (fieldName, flatNames[0]) # Get the requested fields from the flat array and build a nested one. newFlatArray = _narrowRecArray(self._flatArray, startField, flatNames) return NestedRecArray(newFlatArray, fieldDescr)
def field(self, fieldName): """ Get field data as an array. `fieldName` can be the name or the index of a field in the record array. If it is not nested, a ``NumArray`` or ``CharArray`` object representing the values in that field is returned. Else, a `NestedRecArray` object is returned. `fieldName` can be used to provide the name of sub-fields. In that case, it will consist of several field name components separated by the string ``'/'``. For instance, if there is a nested field named ``x`` with a sub-field named ``y``, the last one can be accesed by using ``'x/y'`` as the value of `fieldName`. """ # fieldName can be an integer, get the corresponding name if isinstance(fieldName, int): fieldName = self.descr[fieldName][0] # The descr list of the field whose content is being extracted fieldDescr = [ item for item in nriterators.getFieldDescr(fieldName, self.descr)] if fieldDescr == []: raise ValueError("there is no field named ``%s``" % (fieldName,)) fieldDescr = fieldDescr[0][1] # Case 1) non nested fields (bottom level) if isinstance(fieldDescr, str): # The field content is returned as numarray or chararray return self._flatArray.field(fieldName) # Case 2) nested fields (both top and intermediate levels) # We need fully qualified names to access the flat array fields fieldNames = [ name for name in nriterators.getNamesFromDescr(fieldDescr)] flatNames = [ name for name in nriterators.flattenNames(fieldNames)] # This is the flattened name of the original first bottom field. startField = '%s/%s' % (fieldName, flatNames[0]) # Get the requested fields from the flat array and build a nested one. newFlatArray = _narrowRecArray(self._flatArray, startField, flatNames) return NestedRecArray(newFlatArray, fieldDescr)