fdutils/files.py
def csv_data_class(filepath, index_by, headers=(), headers_mapping=None, csv_has_headers=True):
    """ function to convert a csv into a dictionary with the keys being defined by index_by and the values
        being a SimpleNamespace instance

    Args:
        filepath (str):
        index_by (str):
        headers (list or tuple):
        headers_mapping (dict): a dictionary to map the headers name to new names
        csv_has_headers (bool):

    Returns:

    """
    import types
    import csv

    if not csv_has_headers and not headers:
        raise Exception('There were no headers provided and you say that csv does not have headers')

    results = {}

    with open(filepath, newline='') as csvf:
        reader = csv.reader(csvf)
        csv_headers = []
        while csv_has_headers and not csv_headers:
            try:
                csv_headers = next(reader)
            except StopIteration:
                raise Exception("There does not seem to be anything in this csv file")

        headers = headers or csv_headers

        if headers_mapping:
            headers = [headers_mapping.get(h, h) for h in headers]

        for row in reader:
            d = dict(zip(headers, row))
            key = d.pop(index_by)
            if key in results:
                raise Exception('We cannot have duplicate indexes')
            results[key] = types.SimpleNamespace(**d)

    return results
 function to convert a csv into a dictionary with the keys being defined by index_by and the values
        being a SimpleNamespace instance

    Args:
        filepath (str):
        index_by (str):
        headers (list or tuple):
        headers_mapping (dict): a dictionary to map the headers name to new names
        csv_has_headers (bool):

    Returns:

    
function to convert a csv into a dictionary with the keys being defined by index_by and the values
being a SimpleNamespace instance
function to convert a csv into a dictionary with the keys being defined by index_by and the values being a SimpleNamespace instance
def csv_data_class(filepath, index_by, headers=(), headers_mapping=None, csv_has_headers=True):
    import types
    import csv
    if not csv_has_headers and not headers:
        raise Exception('There were no headers provided and you say that csv does not have headers')
    results = {}
    with open(filepath, newline='') as csvf:
        reader = csv.reader(csvf)
        csv_headers = []
        while csv_has_headers and not csv_headers:
            try:
                csv_headers = next(reader)
            except StopIteration:
                raise Exception("There does not seem to be anything in this csv file")
        headers = headers or csv_headers
        if headers_mapping:
            headers = [headers_mapping.get(h, h) for h in headers]
        for row in reader:
            d = dict(zip(headers, row))
            key = d.pop(index_by)
            if key in results:
                raise Exception('We cannot have duplicate indexes')
            results[key] = types.SimpleNamespace(**d)
    return results
def csv_data_class ( filepath , index_by , headers = ( ) , headers_mapping = None , csv_has_headers = True ) : import types import csv if not csv_has_headers and not headers : raise Exception ( 'There were no headers provided and you say that csv does not have headers' ) results = { } with open ( filepath , newline = '' ) as csvf : reader = csv . reader ( csvf ) csv_headers = [ ] while csv_has_headers and not csv_headers : try : csv_headers = next ( reader ) except StopIteration : raise Exception ( "There does not seem to be anything in this csv file" ) headers = headers or csv_headers if headers_mapping : headers = [ headers_mapping . get ( h , h ) for h in headers ] for row in reader : d = dict ( zip ( headers , row ) ) key = d . pop ( index_by ) if key in results : raise Exception ( 'We cannot have duplicate indexes' ) results [ key ] = types . SimpleNamespace ( ** d ) return results
function to convert a csv into a dictionary with the keys being defined by index_by and the values
being a SimpleNamespace instance
function to convert a csv into a dictionary with the keys being defined by index_by and the values being a SimpleNamespace instance
""" function to convert a csv into a dictionary with the keys being defined by index_by and the values
        being a SimpleNamespace instance

    Args:
        filepath (str):
        index_by (str):
        headers (list or tuple):
        headers_mapping (dict): a dictionary to map the headers name to new names
        csv_has_headers (bool):

    Returns:

    """
None
filepath
None
index_by
None
headers
None
headers_mapping
a dictionary to map the headers name to new names
a dictionary to map the headers name to new names
csv_has_headers
None