def convertToAPDescr(descr, byteorder): """ Convert a NRA `descr` descriptor into a true array protocol description. """ # Build a descriptor compliant with array protocol # (http://numeric.scipy.org/array_interface.html) i = nriterators.getIter(descr) if not i: return try: item = i.next() while item: if isinstance(item[1], str): # parse the formats into repeats and formats try: (_repeat, _dtype) = format_re.match(item[1].strip()).groups() except TypeError, AttributeError: raise ValueError('format %s is not recognized' % _fmt[i]) _dtype = _dtype.strip() # String type needs special treatment if _dtype[0] in ('a', 'S'): _dtype = '|S' + _dtype[1:] else: if _dtype in revfmt: # _dtype is in long format _dtype = cbyteorder[byteorder] + revfmt[_dtype] elif _dtype in revfmt.values(): # _dtype is already in short format _dtype = cbyteorder[byteorder] + _dtype else: # This should never happen raise ValueError, \ "Fatal error: format %s not recognized." % (_dtype) # Return the column if _repeat in ['', '1', '()']: # scalar case yield (item[0], _dtype) else: yield (item[0], _dtype, eval(_repeat)) else: l = [] for j in convertToAPDescr(item[1], byteorder): l.append(j) yield (item[0], l) item = i.next() except StopIteration: pass
def convertFromAPDescr(array_descr): """ Convert a true description of the array protocol in one suited for NRA. """ # Get an iterator for the array descrition i = nriterators.getIter(array_descr) if not i: return try: item = i.next() while item: if isinstance(item[1], str): _dtype = item[1].strip() _dtype = _dtype[1:] # remove the byteorder if _dtype in revfmt.values(): _dtype = _dtype elif _dtype[0] == 'S': # String type needs special treatment _dtype = 'a' + _dtype[1:] elif _dtype[0] == "V": raise NotImplementedError( "Padding fields are not " "supported yet. Try to provide objects without " "padding fields.") elif _dtype[0] == "U": raise NotImplementedError( "Unicode fields are not " "supported yet. Try to provide objects without " "unicode fields.") else: # All the other fields are not supported raise ValueError("Fatal error: format %s not supported." % (_dtype)) # Return the column if len(item) <= 2: # scalar case yield (item[0], _dtype) else: shape = item[2] yield (item[0], "%s%s" % (shape, _dtype)) else: l = [] for j in convertFromAPDescr(item[1]): l.append(j) yield (item[0], l) item = i.next() except StopIteration: pass
def convertToAPDescr(descr, byteorder): """ Convert a NRA `descr` descriptor into a true array protocol description. """ # Build a descriptor compliant with array protocol # (http://numeric.scipy.org/array_interface.html) i = nriterators.getIter(descr) if not i: return try: item = i.next() while item: if isinstance(item[1], str): # parse the formats into repeats and formats try: (_repeat, _dtype) = format_re.match(item[1].strip()).groups() except TypeError, AttributeError: raise ValueError('format %s is not recognized' % _fmt[i]) _dtype = _dtype.strip() # String type needs special treatment if _dtype[0] in ('a', 'S'): _dtype = '|S'+_dtype[1:] else: if _dtype in revfmt: # _dtype is in long format _dtype = cbyteorder[byteorder]+revfmt[_dtype] elif _dtype in revfmt.values(): # _dtype is already in short format _dtype = cbyteorder[byteorder]+_dtype else: # This should never happen raise ValueError, \ "Fatal error: format %s not recognized." % (_dtype) # Return the column if _repeat in ['','1','()']: # scalar case yield (item[0], _dtype) else: yield (item[0], _dtype, eval(_repeat)) else: l = [] for j in convertToAPDescr(item[1], byteorder): l.append(j) yield (item[0], l) item = i.next() except StopIteration: pass
def convertFromAPDescr(array_descr): """ Convert a true description of the array protocol in one suited for NRA. """ # Get an iterator for the array descrition i = nriterators.getIter(array_descr) if not i: return try: item = i.next() while item: if isinstance(item[1], str): _dtype = item[1].strip() _dtype = _dtype[1:] # remove the byteorder if _dtype in revfmt.values(): _dtype = _dtype elif _dtype[0] == 'S': # String type needs special treatment _dtype = 'a'+_dtype[1:] elif _dtype[0] == "V": raise NotImplementedError, """ \ Padding fields are not supported yet. Try to provide objects without padding fields. """ elif _dtype[0] == "U": raise NotImplementedError, """ \ Unicode fields are not supported yet. Try to provide objects without unicode fields. """ else: # All the other fields are not supported raise ValueError, \ "Fatal error: format %s not supported." % (_dtype) # Return the column if len(item) <= 2: # scalar case yield (item[0], _dtype) else: shape = item[2] yield (item[0], "%s%s" % (shape, _dtype)) else: l = [] for j in convertFromAPDescr(item[1]): l.append(j) yield (item[0], l) item = i.next() except StopIteration: pass