From b693f4b25c453f42a0e03386ce0363013c4d38df Mon Sep 17 00:00:00 2001 From: Owen Graham <ohgraham1@madisoncollege.edu> Date: Tue, 24 May 2022 16:49:20 -0500 Subject: [PATCH] Remove `Columns` class; do the work in `DataSpec` --- README.md | 8 ++++---- asccol.py | 21 ++------------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index be0c221..897b275 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/asccol.py b/asccol.py index e1db65d..9090672 100644 --- a/asccol.py +++ b/asccol.py @@ -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): -- GitLab