Beispiel #1
0
def as_ansi(obj, props=(), **propkw):   #as_ansi_array???
    ''' 
    Convert the obj to an array of AnsiStr objects, applying the properties.
    Parameters
    ----------
    obj         :       If input is unsized - return its AnsiStr representation
                        If input is 0 size - return empty AnsiStr object
                        Else return array of AnsiStr objects
    '''
    precision   = propkw.pop('precision', 2)
    ndmin       = propkw.pop('ndmin', 0)
    pretty      = propkw.pop('pretty', True)
    
    obja = np.array(as_sequence(obj), dtype=object)
    #try:
        #obja = np.atleast_1d(obj)
    #except Exception as err:
        #print(err)
        #from IPython import embed
        #embed()
        
    #print()
    
    #reshape complex dtype arrays to object arrays
    if obja.dtype.kind == 'V':  #complex dtype as in record array
        #check if all the dtypes are the same.  If so we can change view
        dtypes = next(zip(*obja.dtype.fields.values()))
        dtype0 = dtypes[0]
        if np.equal(dtypes, dtype0).all():
            obja = obja.view(dtype0).reshape(len(obja), -1)
    #else:
    
    #view as object array
    #obja = obja.astype(object)
    
    if isinstance(props, dict):
        propkw.update(props)
        props = ()
    else:
        props = np.atleast_1d(props)    #as_sequence( props, return_as=tuple)
    
    #deal with empty arrays     #???????
    if not len(obja): #???????
        return AnsiStr(str(obj)) 
    
    fun = lambda s: AnsiStr(as_str(s, precision, pretty)).set_property(*props, **propkw)
    out = np.vectorize(fun, (AnsiStr,))(obja)
    
    if len(out)==1 and out.ndim==1 and ndmin==0:
        out = out[0] #collapse arrays of shape (1,) to item itself if ndmin=0 asked for
    
    return out
Beispiel #2
0
 def apply_props(obj, props=()):
     if isinstance(props, dict):
         return as_superstrings(obj, **props)
     else:
         props = as_sequence(props, return_as=tuple)
         return as_superstrings(obj, props)