Skip to content
Snippets Groups Projects
Verified Commit b693f4b2 authored by Owen Graham's avatar Owen Graham
Browse files

Remove `Columns` class; do the work in `DataSpec`

parent 1cc21a71
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ width.
```py
import math
import sys
from asccol import DataSpec, Columns, parse_data
import asccol
def simple_time(s):
"""Convert a HHMM string to an (H, M) tuple."""
......@@ -24,9 +24,9 @@ def filtered_float(s):
# 10 minute data
# See https://amrc.ssec.wisc.edu/data/ftp/pub/aws/q10/q10readme.txt
Q10 = DataSpec(
Q10 = asccol.DataSpec(
leading=2, # lines to delete
cols=Columns(
cols=(
('year', int, 4), # keyword, type (converter function), width
('jdate', int, 4),
('month', int, 3),
......@@ -41,7 +41,7 @@ Q10 = DataSpec(
),
)
for row in parse_data(sys.stdin, Q10):
for row in asccol.parse_data(sys.stdin, Q10):
print(row)
```
......
......@@ -6,31 +6,14 @@ Column = namedtuple('Column', ('name', 'type', 'width'))
class DataSpec:
"""Details about how to parse an aligned data file.
Arguments:
cols (Columns): columns' name, type, and width
leading (int): number of leading lines to ignore
"""
"""Details about how to parse an aligned data file."""
def __init__(self, cols, leading=0):
self.cols = cols
self.cols = tuple(map(Column._make, cols))
self.leading = leading
self.namedtuple = namedtuple('row', (col.name for col in cols))
class Columns(tuple):
"""Details about how to parse aligned data."""
def __new__(cls, *data):
columns = map(Column._make, data)
return tuple.__new__(cls, columns)
def __repr__(self):
contents = ', '.join(map(repr, self))
return f'{type(self).__name__}({contents})'
def parse_data(stream, dataspec):
"""Read lines from `stream` and yield `namedtuple` data."""
for line_num, line in enumerate(stream, start=1):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment