def to_csv(objects, filename, digits=5, warnings=True):
    """
    Export the flatten indicators of one or several users to CSV.

    Parameters
    ----------
    objects : list
        List of objects to be exported.
    filename : string
        File to export to.
    digits : int
        Precision of floats.

    Examples
    --------
    This function can be used to export the results of
    :meth`bandicoot.utils.all`.
    >>> U_1 = bc.User()
    >>> U_2 = bc.User()
    >>> bc.to_csv([bc.utils.all(U_1), bc.utils.all(U_2)], 'results_1_2.csv')

    If you only have one object, you can simply pass it as argument:
    >>> bc.to_csv(bc.utils.all(U_1), 'results_1.csv')
    """

    if not isinstance(objects, list):
        objects = [objects]

    data = [flatten(obj) for obj in objects]
    all_keys = [d for datum in data for d in datum.keys()]
    field_names = sorted(set(all_keys), key=lambda x: all_keys.index(x))

    with open(filename, 'w') as f:
        w = csv.writer(f)
        w.writerow(field_names)

        def make_repr(item):
            if item is None:
                return None
            elif isinstance(item, float):
                return repr(round(item, digits))
            else:
                return str(item)

        for row in data:
            row = dict((k, make_repr(v)) for k, v in row.items())
            w.writerow([make_repr(row.get(k, None)) for k in field_names])

    if warnings:
        print("Successfully exported {} object(s) to {}".format(len(objects),
              filename))
def to_csv ( objects , filename , digits = 5 , warnings = True ) : if not isinstance ( objects , list ) : objects = [ objects ] data = [ flatten ( obj ) for obj in objects ] all_keys = [ d for datum in data for d in datum . keys ( ) ] field_names = sorted ( set ( all_keys ) , key = lambda x : all_keys . index ( x ) ) with open ( filename , 'w' ) as f : w = csv . writer ( f ) w . writerow ( field_names ) def make_repr ( item ) : if item is None : return None elif isinstance ( item , float ) : return repr ( round ( item , digits ) ) else : return str ( item ) for row in data : row = dict ( ( k , make_repr ( v ) ) for k , v in row . items ( ) ) w . writerow ( [ make_repr ( row . get ( k , None ) ) for k in field_names ] ) if warnings : print ( "Successfully exported {} object(s) to {}" . format ( len ( objects ) , filename ) )
Export the flatten indicators of one or several users to CSV.

    Parameters
    ----------
    objects : list
        List of objects to be exported.
    filename : string
        File to export to.
    digits : int
        Precision of floats.

    Examples
    --------
    This function can be used to export the results of
    :meth`bandicoot.utils.all`.
    >>> U_1 = bc.User()
    >>> U_2 = bc.User()
    >>> bc.to_csv([bc.utils.all(U_1), bc.utils.all(U_2)], 'results_1_2.csv')

    If you only have one object, you can simply pass it as argument:
    >>> bc.to_csv(bc.utils.all(U_1), 'results_1.csv')