view lemuriformes/read.py @ 18:56596902e9ae default tip

add some setup + tests
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:57:03 -0800
parents 4793f99b73e0
children
line wrap: on
line source

"""
deserialization
"""

import csv
import json
from .cast import isstring


def dictreader(csv_file):
    """read csv file into list of dicts"""

    with open(csv_file) as f:
        reader = csv.DictReader(f)
        return [row for row in reader]


def read_list_of_dicts(path, format):
    """
    read a list of dicts (not enforced)

    format -- should be 'csv' or 'json'
    """

    assert format in ('csv', 'json')
    assert isstring(path)
    if format == 'csv':
        return dictreader(path)

    with open(path) as f:
        return json.load(f)