wn.util

Wn utility classes.

wn.util.synset_id_formatter(fmt='{prefix}-{offset:08}-{pos}', **kwargs)

Return a function for formatting synset ids.

The fmt argument can be customized. It will be formatted using any other keyword arguments given to this function and any given to the resulting function. By default, the format string expects a prefix string argument for the namespace (such as a lexicon id), an offset integer argument (such as a WNDB offset), and a pos string argument.

Parameters
  • fmt (str) – A Python format string

  • **kwargs – Keyword arguments for the format string.

Return type

Callable

Example

>>> pwn_synset_id = synset_id_formatter(prefix='pwn')
>>> pwn_synset_id(offset=1174, pos='n')
'pwn-00001174-n'
class wn.util.ProgressHandler(*, message='', count=0, total=0, refresh_interval=0, unit='', status='', file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)

An interface for updating progress in long-running processes.

Long-running processes in Wn, such as wn.download() and wn.add(), call to a progress handler object as they go. The default progress handler used by Wn is ProgressBar, which updates progress by formatting and printing a textual bar to stderr. The ProgressHandler class may be used directly, which does nothing, or users may create their own subclasses for, e.g., updating a GUI or some other handler.

The initialization parameters, except for file, are stored in a kwargs member and may be updated after the handler is created through the set() method. The update() method is the primary way a counter is updated. The flash() method is sometimes called for simple messages. When the process is complete, the close() method is called, optionally with a message.

Parameters
  • message (str) –

  • count (int) –

  • total (int) –

  • refresh_interval (int) –

  • unit (str) –

  • status (str) –

  • file (TextIO) –

kwargs

A dictionary storing the updateable parameters for the progress handler. The keys are:

  • message (str) – a generic message or name

  • count (int) – the current progress counter

  • total (int) – the expected final value of the counter

  • unit (str) – the unit of measurement

  • status (str) – the current status of the process

close()

Close the progress handler.

This might be useful for closing file handles or cleaning up resources.

Return type

None

flash(message)

Issue a message unrelated to the current counter.

This may be useful for multi-stage processes to indicate the move to a new stage, or to log unexpected situations.

Parameters

message (str) –

Return type

None

set(**kwargs)

Update progress handler parameters.

Calling this method also runs update() with an increment of 0, which causes a refresh of any indicator without changing the counter.

Return type

None

update(n=1, force=False)

Update the counter with the increment value n.

This method should update the count key of kwargs with the increment value n. After this, it is expected to update some user-facing progress indicator.

If force is True, any indicator will be refreshed regardless of the value of the refresh interval.

Parameters
Return type

None

class wn.util.ProgressBar(*, message='', count=0, total=0, refresh_interval=0, unit='', status='', file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)

A ProgressHandler subclass for printing a progress bar.

Example

>>> p = ProgressBar(message='Progress: ', total=10, unit=' units')
>>> p.update(3)
Progress: [#########                     ] (3/10 units)

See format() for a description of how the progress bar is formatted.

Parameters
  • message (str) –

  • count (int) –

  • total (int) –

  • refresh_interval (int) –

  • unit (str) –

  • status (str) –

  • file (TextIO) –

FMT = '\r{message}{bar}{counter}{status}'

The default formatting template.

close()

Print a newline so the last printed bar remains on screen.

Return type

None

flash(message)

Overwrite the progress bar with message.

Parameters

message (str) –

Return type

None

format()

Format and return the progress bar.

The bar is is formatted according to FMT, using variables from kwargs and two computed variables:

  • bar: visualization of the progress bar, empty when total is 0

  • counter: display of count, total, and units

>>> p = ProgressBar(message='Progress', count=2, total=10, unit='K')
>>> p.format()
'\rProgress [######                        ] (2/10K) '
>>> p = ProgressBar(count=2, status='Counting...')
>>> p.format()
'\r (2) Counting...'
Return type

str

update(n=1, force=False)

Increment the count by n and print the reformatted bar.

Parameters
Return type

None