Created
December 17, 2014 15:29
-
-
Save pzwang/91f0cd7e8bb9ee84553a to your computer and use it in GitHub Desktop.
improved fieldparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fieldparse.py | |
import collections | |
def parse(lines,types,names): | |
if not isinstance(lines, collections.Iterable) or isinstance(lines, str): | |
raise TypeError("lines must be an iterable container of strings") | |
if not isinstance(types, collections.Sequence) or \ | |
not isinstance(names, collections.Sequence): | |
raise TypeError("types and names must be sequences") | |
if len(types) != len(names): | |
raise ValueError("types and names must have the same number of items") | |
if not all(isinstance(x, collections.Callable) for x in types): | |
raise TypeError("types must be callables") | |
records = [] | |
for line in lines: | |
# Strip whitespace and check for blank lines | |
line = line.strip() | |
if not line: continue | |
# Split into fields | |
fields = line.split() | |
if len(fields) != len(names): | |
raise RuntimeError("Wrong number of columns in input") | |
# Apply conversions to each field | |
cfields = [converter(value) for converter,value in zip(types,fields)] | |
# Create a dictionary from the data | |
record = dict(zip(names,cfields)) | |
records.append(record) | |
return records |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment