Skip to content

Commit

Permalink
Don't print progress bar in TestSuite.process()
Browse files Browse the repository at this point in the history
It is now handled by the caller of the method via its `callback`
parameter, and delphin.commands.process does the progress bar by
default.

Fixes #304
  • Loading branch information
goodmami committed Jul 19, 2020
1 parent 3c51ca7 commit 50745ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@

## [v1.3.1]

### Added

* `delphin.commands.process` now has a `report_progress` parameter,
defaulting to `True`, for printing a progress bar ([#304])
* `delphin.itsdb.TestSuite.process()` now has a `callback` parameter,
called for each response during processing, which can be used to
implement a progress bar (among other things) ([#304])

### Fixed

* `delphin.repp` better anticipates unmatched capture groups ([#301])
* `delphin.dmrs.from_mrs()` no longer crashes on bad HCONS ([#303])

### Changed

* `delphin.itsdb.TestSuite.process()` no longer prints a progress bar,
leaving it instead to the caller via the `callback ` parameter;
since the progress bar is a side effect this is not a breaking
change ([#304])


## [v1.3.0]

Expand Down Expand Up @@ -1407,3 +1422,4 @@ information about changes, except for
[#296]: https://github.com/delph-in/pydelphin/issues/296
[#301]: https://github.com/delph-in/pydelphin/issues/301
[#303]: https://github.com/delph-in/pydelphin/issues/303
[#304]: https://github.com/delph-in/pydelphin/issues/304
26 changes: 21 additions & 5 deletions delphin/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import logging
import warnings

from progress.bar import Bar as ProgressBar

from delphin import exceptions
from delphin import tsdb, itsdb, tsql
from delphin.lnk import Lnk
Expand Down Expand Up @@ -543,7 +545,7 @@ def _red(s):
def process(grammar, testsuite, source=None, select=None,
generate=False, transfer=False, full_forest=False,
options=None, all_items=False, result_id=None, gzip=False,
stderr=None):
stderr=None, report_progress=True):
"""
Process the [incr tsdb()] profile *testsuite* with *grammar*.
Expand Down Expand Up @@ -593,6 +595,9 @@ def process(grammar, testsuite, source=None, select=None,
gzip (bool): if `True`, non-empty tables will be compressed
with gzip
stderr (file): stream for ACE's stderr
report_progress (bool): print a progress bar to stderr if
`True` and logging verbosity is at WARNING or lower;
(default: `True`)
"""
from delphin import ace

Expand Down Expand Up @@ -645,11 +650,22 @@ def process(grammar, testsuite, source=None, select=None,
full=True, gzip=True, quiet=True)
tmp = itsdb.TestSuite(dir)

process_kwargs = {'selector': (relation, column),
'source': tmp,
'gzip': gzip}
bar = None
if (report_progress
and len(tmp[relation])
and not logger.isEnabledFor(logging.INFO)
):
bar = ProgressBar('Processing', max=len(tmp[relation]))
process_kwargs['callback'] = lambda _: bar.next()

with processor(grammar, cmdargs=options, **kwargs) as cpu:
target.process(cpu,
selector=(relation, column),
source=tmp,
gzip=gzip)
target.process(cpu, **process_kwargs)
if bar:
bar.finish()



def _interpret_selection(select, source):
Expand Down
27 changes: 11 additions & 16 deletions delphin/itsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing import (
Union, Iterable, Sequence, Tuple, List, Dict, Any,
Iterator, Optional, IO, overload, cast as typing_cast
Iterator, Optional, IO, overload, Callable, cast as typing_cast
)
from pathlib import Path
import tempfile
Expand All @@ -15,8 +15,6 @@
import collections
import itertools

from progress.bar import Bar as ProgressBar

from delphin import util
from delphin import tsdb
from delphin import interface
Expand Down Expand Up @@ -819,13 +817,18 @@ def process(self,
source: tsdb.Database = None,
fieldmapper: FieldMapper = None,
gzip: bool = False,
buffer_size: int = 1000) -> None:
buffer_size: int = 1000,
callback: Callable[[interface.Response], Any] = None,
) -> None:
"""
Process each item in a [incr tsdb()] test suite.
The output rows will be flushed to disk when the number of new
rows in a table is *buffer_size*.
The *callback* parameter can be used, for example, to update a
progress indicator.
Args:
cpu (:class:`~delphin.interface.Processor`): processor
interface (e.g., :class:`~delphin.ace.ACEParser`)
Expand All @@ -842,6 +845,8 @@ def process(self,
buffer_size (int): number of output rows to hold in memory
before flushing to disk; ignored if the test suite is all
in-memory; if `None`, do not flush to disk
callback: a function that is called with the response for
each item processed; the return value is ignored
Examples:
>>> ts.process(ace_parser)
>>> ts.process(ace_generator, 'result:mrs', source=ts2)
Expand All @@ -868,13 +873,6 @@ def process(self,

key_names = [f.name for f in source.schema[input_table] if f.is_key]

bar = None
if not logger.isEnabledFor(logging.INFO):
with tsdb.open(source.path, input_table) as fh:
total = sum(1 for _ in fh)
if total > 0:
bar = ProgressBar('Processing', max=total)

for row in source[input_table]:
datum = row[index[input_column]]
keys = [row[index[name]] for name in key_names]
Expand All @@ -885,18 +883,15 @@ def process(self,
'Processed item {:>16} {:>8} results'
.format(tsdb.join(keys), len(response['results']))
)
if bar:
bar.next()
if callback:
callback(response)

for tablename, data in fieldmapper.map(response):
_add_row(self, tablename, data, buffer_size)

for tablename, data in fieldmapper.cleanup():
_add_row(self, tablename, data, buffer_size)

if bar:
bar.finish()

tsdb.write_database(self, self.path, gzip=gzip)


Expand Down

0 comments on commit 50745ee

Please sign in to comment.