Example #1
0
 def expand(d,level):#,nrows=nrows,ncols=ncols):
     #self.debug('\texpand(%s(%s),%s)'%(type(d),d,level))
     for k,v in sorted(d.items() if hasattr(d,'items') else d):
         zero = data['nrows']
         data[(data['nrows'],level)] = k
         if fun.isDictionary(v): 
             data['ncols']+=1
             expand(v,level+1)
         else:
             if not fun.isSequence(v): v = [v]
             for t in v:
                 data[(data['nrows'],level+1)] = t
                 data['nrows']+=1
Example #2
0
def values2text(table,order=None,sep='\t',eof='\n',header=True):
    """ 
    Input is a dictionary of {variable:[(time,values)]}; it will convert the dictionary into a text table with a column for each variable
    It assumes that values in diferent columns have been already correlated
    """
    start = time.time()
    if not order or not all(k in order for k in table): keys = list(sorted(table.keys()))
    else: keys = sorted(table.keys(),key=order.index)
    value_to_text = lambda s: str(s).replace('None','').replace('[','').replace(']','')
    is_timed = fun.isSequence(table.values()[0][0]) and len(table.values()[0][0])==2
    csv = '' if not header else (sep.join((['date','time'] if is_timed else [])+keys)+eof)
    for i,t in enumerate(table.values()[0]):
        if is_timed:
            row = [fun.time2str(t[0]),str(t[0])]+[value_to_text(table[k][i][-1]) for k in keys]
        else:
            row = [value_to_text(table[k][i]) for k in keys]
        csv+=sep.join(row) + eof
    #print('Text file generated in %d milliseconds'%(1000*(time.time()-start)))
    return csv
Example #3
0
def test2str(obj, meth='', args=[], kwargs={}):
    """
    Executes a method providing a verbose output.
    For usage examples see fandango.device.FolderDS.FolderAPI.__test__()
    """
    fs = str(obj) if not meth else '%s.%s' % (obj, meth)
    r = 'Testing %s(*%s,**%s)\n\n' % (fs, args, kwargs)
    v = None
    try:
        f = getattr(obj, meth) if meth and isString(meth) else (meth or obj)
        v = f(*args, **kwargs)
        if isMapping(v):
            s = '\n'.join(map(str, v.items()))
        elif isSequence(v):
            s = '\n'.join(map(str, v))
        else:
            s = str(v)
    except:
        s = traceback.format_exc()
    r += '\n'.join('\t%s' % l for l in s.split('\n')) + '\n\n'
    return r, v