Skip to main content

Parse input streams with headers and footers.

Project description

Overview

The header_detail_footer module provides a way to parse input iterables (usually text files) that contain header rows, an unknown number of data rows (called the details), and footer rows. The number of header and footer rows must be specified when parsing begins. There can be zero or more header rows, and independently zero or more footer rows. The detail rows are all of the rows between the header and footer. If a file consists of just headers and footers, there will be zero detail rows.

The module API consists of a single function, parse(), and several exceptions.

Note that the contents of each input “row” are never inspected: they are just iterated over and returned by the parser. They are often strings, but they could be any object.

Typical usage

This code shows a simple usage of the parse() function:

>>> from header_detail_footer import parse
>>> header, details, footer = parse(['header', 'row 1', 'row 2', 'footer'])
>>> header
'header'
>>> list(details)
['row 1', 'row 2']
>>> footer()
'footer'

The parse() function

The parse() function takes 1 required parameter, the input iterable. There are two optional parameters, header_rows and footer_rows. Both default to 1. They represent the number of header and footer rows present in the input, respectively.

parse() returns a 3-tuple: (header, details, footer). header is the header row(s), if any; details is an iterator returning each detail row; and footer is a callable returning the footer row(s), if any.

For header and footer(), they return a single row from the input if header_rows or footer_rows is 1, respectively. Otherwise, including the case of 0 rows, they contain a list:

>>> header, details, footer = parse(['row 1', 'row 2', 'footer 1', 'footer 2'],
...                                 header_rows=0, footer_rows=2)
>>> header
[]
>>> list(details)
['row 1', 'row 2']
>>> footer()
['footer 1', 'footer 2']

The returned footer callable need never be called. If footer is ever called, details must have been exhausted, otherwise a ValueError is raised:

>>> header, detail, footer = parse('abc')
>>> footer()
Traceback (most recent call last):
    ...
ValueError: called footer() before details were exhausted

Exceptions

HeaderError

Raised by parser() if the input does not contain enough rows for the header:

>>> header, details, footer = parse(['row 1'], header_rows=3)
Traceback (most recent call last):
    ...
HeaderError: too few rows for header

FooterError

Raised by parser() if the input does contain enough rows for the header, but not enough rows for the footer:

>>> header, details, footer = parse(['row 1', 'row 2', 'row 3'],
...                                 header_rows=2, footer_rows=2)
Traceback (most recent call last):
    ...
FooterError: too few rows for footer

Change log

2.0 2013-11-15 Eric V. Smith

  • Changed API to return a callable only for footer, since that’s the only thing that needs to be delayed after details are exhausted.

  • Changed nomenclature: now refers to “rows” instead of “lines”.

1.0 2013-11-15 Eric V. Smith

  • First stable version.

0.1 2013-11-14 Eric V. Smith

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

header-detail-footer-2.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page